How can I have a user specific hosts file

No, you cannot have a per-user /etc/hosts file, or anything like /home/user1/.hosts , etc.

You are using gethostbyaddr which is hardcoded to follow the instructions in nsswitch.conf, which itself tells gethostbyaddr to look in /etc/hosts .

You might be able to do something like add additional loopback IPs on the 127.0.0.0/8 network, like 127.0.0.2 , 127.0.0.3, 127.1.2.3, and then assign a local hostname to one of these local IPs. We did this at one job, but I remember that this really confused our engineers.

Also, if I remember right some loadbalancers actually do this internally.

Here's an example /etc/hosts to illustrate my point:

    127.0.0.1 u1.localhost u1
    127.0.0.2 u2.localhost u2
    # And if you wanted QA servers on the same host, add them to 127.0.8.0/24
    127.0.8.1 qa1.localhost qa1

As @blacklotus suggested earlier, the more common way to do this is to designate part of your local network as a "Developer LAN".


What problem are you trying to solve?

You certainly can't have two different entries in a hosts file, that are somehow toggled depending on which user you are.

If you tell us what you're trying to do, rather than ask us about your specific implementation, we may be able to help more.