Drophost - A Rust utility for managing your hosts file on Linux

What is Drophost?

Drophost is a little utility written in Rust that provides a drop-in interface for managing your /etc/hosts file. It can combine together multiple hosts files and has a system which allows you to add custom logic to the hosts file generation process.

Why does this exist?

I've encountered a few situations where it can be useful to tinker with the /etc/hosts file. Some people use it for ad blocking, others use it for aliases or to prevent proprietary software from phoning home. While the default syntax does allow for some organization (by using comments). It's still just a single file that contains everything you need it to do. This can make it difficult to manage when you have a lot of entries like I do. This is why I made Drophost, it provides a way to organize your hosts file into multiple files and then combine them together into a single file automatically.

How to use it?

Installation

Installing the executable

The Drophost executable can be installed from crates.io (opens in a new tab) using cargo:

cargo install drophost

If you are on Arch Linux or any other Arch-based distribution, you can install it from the AUR using your favorite AUR helper:

yay -S drophost

I'm also planning on creating an ebuild for Gentoo, but I am going to have to create my own overlay for that. I'll update this page when that happens.

Using the AUR will also automatically install the service file for the next step.

Setting up the service

Make sure you don't run Drophost yet! Otherwise you may lose your current /etc/hosts file!

systemd

On systems using systemd, you can install the drophost.service file found in the Drophost repository (opens in a new tab) to /etc/systemd/system/drophost.service. This will allow you to start and stop the service using systemctl. These commands will do that for you:

sudo wget https://raw.githubusercontent.com/KodiCraft/drophost/main/drophost.service -O /etc/systemd/system/drophost.service
sudo systemctl enable drophost.service

OpenRC

On systems using OpenRC, you can install the drophost file found in the Drophost repository (opens in a new tab) to /etc/init.d/drophost. This will allow you to start and stop the service using rc-service and add it to the default runlevel. These commands will do that for you:

sudo wget https://raw.githubusercontent.com/KodiCraft/drophost/main/drophost -O /etc/init.d/drophost
sudo rc-update add drophost default

Other

For other init systems, you have to configure launching Drophost yourself. The -d flag will run Drophost in daemon mode, which will make it run in the background. The -p <PIDFILE> argument allows you to specify a file to write the PID to. This is useful for init systems that require a PID file to be specified.

Configuration

When configuring drophost for the first time, it can be useful to use the --backup flag. This will make Drophost create a backup of your current /etc/hosts inside of its own configuration directory. This means that all of your current hosts entries will be preserved, and you can easily restore them if you need to.

sudo drophost --backup

Once this initial configuration is done, you can start the drophost service. The service files provided will automatically update the hosts file every time the /etc/hosts.d directory is modified. This means that you can just drop in files into the directory and they will be automatically added to the hosts file.

sudo systemctl start drophost.service
sudo rc-service drophost start

Drophost will read all of the files in the /etc/hosts.d directory and parse them before generating the central file that your system uses. This means that you can add as many files as you want to this directory and they will all be combined together. The files are read in alphabetical order, so you can use numbers to control the order in which they are read. For example, if you want to make sure that myhosts is read before otherhosts, you can name them 10-myhosts and 20-otherhosts.

Drophost's files are formatted in the same way as the /etc/hosts file. Each line is a single entry, which follows the syntax:

<IP> <HOSTNAME> 

Drophost also implements additional features that allow you to add custom logic to the hosts file generation process. These features are documented further on the Drophost repository (opens in a new tab).