Linux – How to have a user specific hosts file

centoshostshosts-filelinux

Is it possible to configure user specific hosts file instead of a common /etc/hosts.

For example if user "user1" tries to get the name for the IP: "127.0.0.1", he gets "dev.user1" and if user "user2" tries to get the name for the same IP, he gets "dev.user2".

Best Answer

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".