figiel / hosts

a libc wrapper providing per-user hosts file
Creative Commons Zero v1.0 Universal
65 stars 12 forks source link

userhosts

Allow user-defined hosts entries.

userhosts is a LD_PRELOAD library that overrides the fetching of hostnames in applications. userhosts transforms hostnames using a user-defined hosts file before doing the actual lookup.

This allows using your own userhosts file on top of your system /etc/hosts file.

By default userhosts fetches hosts entries from the home directory (~/.hosts). This can be overridden using an environment variable (HOSTS_FILE) to specify a different file.

This library was renamed, the previous name was libhostspriv.so.

Build

make

This results in libuserhosts.so.

Install

You can choose to use userhosts globally for your user account by adding it to your shell, using bashrc:

mkdir ~/bin
cp libuserhosts.so ~/bin
echo 'export LD_PRELOAD=$HOME/bin/libuserhosts.so' >> ~/.bashrc

source ~/.bashrc

You can also use userhosts for individual commands:

LD_PRELOAD=$HOME/bin/libuserhosts.so firefox

Usage

Using the default hosts file in $HOME:

echo "127.0.0.1 somehost" > ~/.hosts
nc -vz somehost 80

Using the HOSTS_FILE environment variable:

echo "127.0.0.1 somehost" > hosts
HOSTS_FILE=$PWD/hosts nc -vz somehost 80

Use hostnames as target to redirect name resolving:

echo "localhost somehost2" > ~/.hosts
nc -vz somehost2 80

Resolving will not happen recursively, so the following will attempt to DNS resolve somehost:

echo "localhost somehost" > ~/.hosts
echo "somehost somehost2" >> ~/.hosts
nc -vz somehost2 80

Limitations

userhosts does not work with suid programs. That includes ping unfortunately.