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: "", he gets "dev.user1" and if user "user2" tries to get the name for the same IP, he gets "dev.user2".

  • Will need more information such as what problem you are trying to solve, in the text body of this question. – SDsolar Aug 13 '17 at 2:48

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 network, like ,,, 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: u1.localhost u1 u2.localhost u2
    # And if you wanted QA servers on the same host, add them to 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.

  • I am using RHEL 5.3 and my /etc/hosts file contains the following lines. localhost.localdomain localhost ::1 localhost6.localdomain6 localhost6 I have a php code that will return the internet host name for based on the /etc/hosts file. <?php $hostname = gethostbyaddr(''); echo $hostname; ?> The script it returns "localhost.localdomain" Now, let us say a user u1 logs into the system and wants to personalize his hosts file like /home/u1/hosts with the following content. u1.localhost u1 The php code should return "u1.localhost". – Supratik Jul 12 '10 at 7:51
  • 2
    gethostbyaddr() will return the hostname associated with the given IP. How it does this on a Linux system is dictated by /etc/nsswitch.conf. The default on a Red Hat box will be: hosts: files dns So it will look at /etc/hosts and then use DNS. Nothing you do will be able to provide a different answer dependent upon username. It just doesn't work that way. You still haven't told us what problem you're trying to solve. Why do you want users to have a customisable answer to a lookup of (or any other IP)? – Stephen Nelson-Smith Jul 12 '10 at 9:05
  • In the PHP code we include environment specific configurations. We check using gethostbyaddr() to find out whether the PHP file is running in production server or it is in a Test server. We include environment specific files based on the return value of gethostbyaddr(). Now in Test Server if there are multiple developers working on the same project. I want PHP to include user specific configuration. If the user logs into the system then gethostbyaddr() should return different value depending upon the users logged into the system. Can you please suggest me a possible solution to this problem. – Supratik Jul 12 '10 at 11:03
  • Don't use ip lookups for this. It sounds to me like you either have the users start up a web server running php as themselves, or you have a perpetually running web server running as user nobody. If it's the former, then you can find out what user the apache server is running as with posix_getuid. If you have one web server shared by developers, then you will have to have the script check to see who is logged in (by running the who command perhaps). I would consider what you want to happen if two different developers are logged on at the same time and see if that suggests a different solution. – Mark Jul 13 '10 at 4:11

Why don't you assign some virtual ips to your own server instead at eth0:0, eth0:1 etc?

Say assign to dev.user1, and to dev.user2. When you access the projects via different ips, would gethostbyaddr() return the hostname accordingly?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.