Skip to main content

Anonip is a tool to anonymize IP-addresses in log-files.

Project description


PyPI Python versions Build Status Coverage Black License

Digitale Gesellschaft

Formerly Swiss Privacy Foundation


Anonip is a tool to anonymize IP addresses in log files.

It masks the last bits of IPv4 and IPv6 addresses. That way most of the relevant information is preserved, while the IP-address does not match a particular individuum anymore.

The log entries get directly piped from Apache to Anonip. The unmasked IP addresses are never written to any file.

With the help of cat, it's also possible to rewrite existing log files.

For usage with nginx see here:


  • Masks IP addresses in log files
  • Configurable amount of masked bits
  • The column containing the IP address can freely be chosen
  • Works for both access.log- and error.log files

Officially supported python versions

  • 2.7
  • 3.5
  • 3.6
  • 3.7


If you're using python version >=3.3, there are no external dependencies.

For python versions <3.3:


usage: [-h] [-4 INTEGER] [-6 INTEGER] [-i INTEGER] [-o FILE]
                 [-c INTEGER [INTEGER ...]] [-l STRING] [-r STRING] [-p] [-d]

Anonip is a tool to anonymize IP-addresses in log files.

optional arguments:
  -h, --help            show this help message and exit
  -4 INTEGER, --ipv4mask INTEGER
                        truncate the last n bits (default: 12)
  -6 INTEGER, --ipv6mask INTEGER
                        truncate the last n bits (default: 84)
  -i INTEGER, --increment INTEGER
                        increment the IP address by n (default: 0)
  -o FILE, --output FILE
                        file to write to
  -c INTEGER [INTEGER ...], --column INTEGER [INTEGER ...]
                        assume IP address is in column n (1-based indexed;
                        default: 1)
  -l STRING, --delimiter STRING
                        log delimiter (default: " ")
  -r STRING, --replace STRING
                        replacement string in case address parsing fails
  -p, --skip-private    do not mask addresses in private ranges. See IANA
                        Special-Purpose Address Registry.
  -d, --debug           print debug messages
  -v, --version         show program's version number and exit


In the Apache configuration (or the one of a vhost) the log output needs to get piped to anonip:

CustomLog "|/path/to/ [OPTIONS] --output /path/to/log" combined

That's it! All the IP addresses will be masked in the log now.


cat /path/to/orig_log | /path/to/ [OPTIONS] --output /path/to/log

As a python module

Read from stdin:

from anonip import Anonip

anonip = Anonip()
for line in

Manually feed lines:

from anonip import Anonip

data = ['', '', '']
anonip = Anonip()

for line in data:

Python 2 or 3?

For compatibility reasons, anonip uses the shebang #! /usr/bin/env python. This will default to python2 on all Linux distributions except for Arch Linux. The performance of anonip can be improved by running it with python3. If python3 is available on your system, you should preferrably invoke anonip like this:

python3 -m anonip [OPTIONS]


python3 /path/to/ [OPTIONS]


In most cases IP addresses are personal data as they refer to individuals (or at least their Internet connection). IP addresses - and the data associated with them - may therefore only be lawfully processed in accordance with the principles of the applicable data protection laws.

Storage of log files from web servers, for example, is only permitted within close time limits or with the voluntary consent of the persons concerned (as long as the information about the IP address is linkable to a person).

Anonip tries to avoid exactly that, but without losing the benefit of those log files.

With the masking of the last bits of IP addresses, we're still able to distinguish the log entries up to a certain degree. Compared to the entire removal of the IP-adresses, we're still able to make a rough geolocating as well as a reverse DNS lookup. But the otherwise distinct IP addresses do not match a particular individuum anymore.

Project details

Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Files for anonip, version 1.0.0
Filename, size File type Python version Upload date Hashes
Filename, size anonip-1.0.0-py2.py3-none-any.whl (7.6 kB) File type Wheel Python version py2.py3 Upload date Hashes View
Filename, size anonip-1.0.0.tar.gz (7.5 kB) File type Source Python version None Upload date Hashes View

Supported by

AWS AWS Cloud computing Datadog Datadog Monitoring DigiCert DigiCert EV certificate Facebook / Instagram Facebook / Instagram PSF Sponsor Fastly Fastly CDN Google Google Object Storage and Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Salesforce Salesforce PSF Sponsor Sentry Sentry Error logging StatusPage StatusPage Status page