Skip to main content

A simple hosts file manager

Project description

hostsd - A simple hosts file manager

Support Private.coffee! PyPI PyPI - Python Version PyPI - License Latest Git Commit

hostsd is a simple hosts file manager that allows you to separate your hosts file into multiple files and easily enable or disable them. It's useful for development environments where you need to manage lots of hosts entries, or for managing ad-blocking hosts files.

Dependencies

  • Python 3.8 or later (earlier versions may work but are untested)
  • Linux or macOS (should work on Windows too but is untested)

Installation

pip install hostsd

Now, to ensure that your existing /etc/hosts contents are retained, you can copy the file to /etc/hosts.d/00-original:

sudo mkdir /etc/hosts.d
sudo cp /etc/hosts /etc/hosts.d/00-original

Usage

To write the contents of /etc/hosts.d/* to /etc/hosts:

sudo hostsd

You can also specify the input and output paths:

hostsd -i /etc/hosts.d -o /etc/hosts

Excluding files

There are several ways to exclude files and directories from being included in the output:

  1. You can disable a file or directory by adding a .disabled extension:
mv /etc/hosts.d/10-my-file /etc/hosts.d/10-my-file.disabled
  1. Hidden files and directories are ignored as well:
mv /etc/hosts.d/regular-file /etc/hosts.d/.hidden-file
  1. You can include the magic string HOSTSD_IGNORE within a file to exclude it from the output, for example as a comment:
echo "# HOSTSD_IGNORE" >> /etc/hosts.d/10-my-file
  1. Any binary files are automatically ignored.

Note that files are not currently checked for validity, so your hosts.d directory should only contain valid hosts file entries or files that are excluded using one of the methods above.

Running hostsd automatically

You can run hostsd automatically using a cron job or a systemd timer. Here's an example of a systemd timer (assuming you have installed hostsd globally, for simplicity):

# /etc/systemd/system/hostsd.timer
[Unit]
Description=Update hosts file every minute

[Timer]
OnBootSec=1min
OnUnitActiveSec=1min

[Install]
WantedBy=timers.target
# /etc/systemd/system/hostsd.service
[Unit]
Description=Update hosts file

[Service]
Type=oneshot
ExecStart=/usr/bin/hostsd
sudo systemctl enable hostsd.timer
sudo systemctl start hostsd.timer

This will run hostsd every minute. So you can just drop a new file in /etc/hosts.d and it will be picked up automatically.

You could even combine this with a git repository and you have a simple way to manage your hosts files across multiple machines or share them with others, without needing to set up and manage a full DNS server.

License

hostsd is licensed under the MIT license. See LICENSE for the full license text.

Project details


Download files

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

Source Distribution

hostsd-0.1.4.tar.gz (4.9 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

hostsd-0.1.4-py3-none-any.whl (6.1 kB view details)

Uploaded Python 3

File details

Details for the file hostsd-0.1.4.tar.gz.

File metadata

  • Download URL: hostsd-0.1.4.tar.gz
  • Upload date:
  • Size: 4.9 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.1.0 CPython/3.11.2

File hashes

Hashes for hostsd-0.1.4.tar.gz
Algorithm Hash digest
SHA256 dde747ca06abe367cc9e76e8f5b91d48acaab0096c6e4fca22149339fe59a430
MD5 998627cae503c41b47166ca619c08385
BLAKE2b-256 277afce0fe78d6864ceaf48085775d2b061afd5e93af7ea3784145be01c15383

See more details on using hashes here.

File details

Details for the file hostsd-0.1.4-py3-none-any.whl.

File metadata

  • Download URL: hostsd-0.1.4-py3-none-any.whl
  • Upload date:
  • Size: 6.1 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.1.0 CPython/3.11.2

File hashes

Hashes for hostsd-0.1.4-py3-none-any.whl
Algorithm Hash digest
SHA256 5d568f131f4183052989c2cedc57065301a10d37aa8e5f2510f19a7d9c38f5fd
MD5 10a9e299c77760bf4654b904b7a60974
BLAKE2b-256 a958886a33211dd8178e6d23a0dc9b2b85aac62fb2c2afb06f42a988fea34190

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page