How can I have a user specific hosts file

8,904

Solution 1

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

Solution 2

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.

Solution 3

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

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

Share:
8,904

Related videos on Youtube

Supratik
Author by

Supratik

Updated on September 17, 2022

Comments

  • Supratik
    Supratik almost 2 years

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

    • SDsolar
      SDsolar almost 7 years
      Will need more information such as what problem you are trying to solve, in the text body of this question.
  • Supratik
    Supratik almost 14 years
    I am using RHEL 5.3 and my /etc/hosts file contains the following lines. 127.0.0.1 localhost.localdomain localhost ::1 localhost6.localdomain6 localhost6 I have a php code that will return the internet host name for 127.0.0.1 based on the /etc/hosts file. <?php $hostname = gethostbyaddr('127.0.0.1'); 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. 127.0.0.1 u1.localhost u1 The php code should return "u1.localhost".
  • upasaka
    upasaka almost 14 years
    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 127.0.0.1 (or any other IP)?
  • Supratik
    Supratik almost 14 years
    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.
  • Mark
    Mark almost 14 years
    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.