Nss-userhosts: a name service switch module allowing unprivileged users to have

fanf21 pts0 comments

felixs/nss-userhosts: A glibc Name Service Switch (NSS) extension library to allow unprivileged users to have their own /etc/hosts-like entries. - Codeberg.org

Codeberg has changed its Terms of Use. View complete changes.

LLM-generated content is being restricted. View change or read the blog post

Cryptocurrency projects are no longer allowed. View change

Dismiss

This website requires JavaScript.

felixs/nss-userhosts

Watch

Star

Fork

Code

Issues

Pull requests

Activity

A glibc Name Service Switch (NSS) extension library to allow unprivileged users to have their own /etc/hosts-like entries.

5 commits

1 branch

0 tags

25 KiB

Rust

87.9%

Shell

5.5%

Dockerfile

4.2%

Just

2.4%

main

Find a file

HTTPS

Download ZIP<br>Download TAR.GZ<br>Download BUNDLE

Open with VS Code

Open with VSCodium

Open with Intellij IDEA

Repository files (latest commit first)

Filename<br>Latest commit message<br>Latest commit date

Felixs

d8e14e2853

Fix install script

2026-08-22 17:40:00 +02:00

packaging

Fix install script

2026-08-22 17:40:00 +02:00

src

Comment currently not useful code, fix uses of print macros

2026-08-22 14:51:36 +02:00

.gitignore

First working & tested nsswitch module, no functionality yet

2026-08-21 00:24:39 +02:00

Cargo.lock

First fully functional version

2026-08-22 14:34:12 +02:00

Cargo.toml

First fully functional version

2026-08-22 14:34:12 +02:00

Containerfile

Fix repo up for publishing

2026-08-22 17:25:24 +02:00

justfile

Fix install script

2026-08-22 17:40:00 +02:00

LICENSE.txt

Fix repo up for publishing

2026-08-22 17:25:24 +02:00

README.md

Fix install script

2026-08-22 17:40:00 +02:00

README.md

A glibc Name Service Switch (NSS) extension library to allow<br>unprivileged users to have their own /etc/hosts-like entries.

Features

The tool is in an early stage but usable. Currently it supports:

parsing $HOME/.config/userhosts/*.conf files in hosts format and combining all records

both IPv4 and IPv6 records

Not supported (yet?) is:

resolving addresses to names

making use of the XDG specification to find the correct directory

Limitations:

This only works with software that actually uses the glibc resolver

How to use it

Installation

After cloning the repository, build it:

cargo build --release

the resulting artifact libnss_userhosts.so needs to be renamed to libnss_userhosts.so.2 be correctly found by glibc

copy the library into your library loader path, /usr/lib/<br>(in my tests, /usr/local/lib didn't work unfortunately)

If you run Arch you can also build the package: cd packaging && makepkg

Configuration

Now you need to tell glibc to use it. This is done in /etc/nsswitch.conf.<br>Modify the line starting with hosts such that userhosts is in the front, e.g. like so:

hosts: userhosts mymachines resolve [!UNAVAIL=return] files myhostname dns<br>You are now ready to define your own hosts. Create the directory:<br>mkdir --parents $HOME/.config/userhosts and add a first hosts, e.g:

echo "192.168.1.79 catfeeder" > $HOME/.config/userhosts/pets.conf

To test, you can use getent like so:

getent ahosts catfeeder

Hacking

If something doesn't work, try running your program with the<br>environment variable ENV NSS_USERHOSTS_LOG true set. This will print<br>debug messages of the module to stderr.

I usually test it using unprivileged containers and podman. There's<br>a justfile for that, so just test should get you started if you have<br>podman and just installed. The Containerfile can be easily adapted<br>for further tests.

Bugs & support

You can open a pull request or file an issue on<br>Codeberg but I don't<br>have a lot of time to work on this. But I'll try to look at it in<br>a timely manner.

userhosts hosts glibc unprivileged library like

Related Articles