Server Fault is a question and answer site for system and network administrators. It's 100% free, no registration required.

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

Let's say I have a Linux server with an external IP of 123.456.789.012 and a local IP of If it's /etc/hosts file looks like, for example,


When an application on the server generates traffic to send using, how does the server know whether to use the local or external IP address (since the same host name shows two IP addresses)?

Or, does it need something additional than what I've presented here to decide this?

Or, does it just sent it out as and let the receiving end deal with it (if so, how to make sure traffic intended for local network indeed goes to local network)?

share|improve this question
up vote 4 down vote accepted

From hosts(5):

For each host a single line should be present

So duplicating the same hostname for different IPs is a no-no; behaviour is not guaranteed if you do.

HOWEVER, when I put testhost testhost 

in /etc/hosts, getent hosts testhost returns both lines.

NOTE that this will of course be highly specific to the OS and libc versions used.

share|improve this answer

Your machine, computer1, has an updated hosts file:


Any traffic sent to from computer1 needs to be resolved to an IP address, and the place that has precedence over all other sources (e.g. DNS) is your /etc/hosts file. The first matching for the hostname in /etc/hosts is used. So, in your example, will always resolve to 123.456.789.012.

share|improve this answer
How do you know the first match is used ? – adaptr Mar 27 '12 at 15:46
I verified experimentally, but I accept that the more correct answer is that it is undefined. You most certainly should not rely on it matching the first entry. – Lunar_Lamp Mar 27 '12 at 15:55

It doesn't.

You should either use seperate internal/external domain names, or use DNS to provide different responses based on network location.

share|improve this answer

Linux man page clearly says that "one line per IP address", so what you are doing is undefined. You can get the first one in the file, the last, or random.

It is better for your application just to use the ip in this case, maybe based on some criteria.

share|improve this answer
sorry, i pasted less than i intended. it also says: For each host a single line should be present. But after reviewing this question more and reading comments/answers from others, I realize that this may be os and even version-dependent. Mine is ubuntu lucid. – johnshen64 Mar 27 '12 at 16:03

You would have a local dns server & a remote DNS server. So on the machine in hosts you just have the IP you want it to use.

share|improve this answer

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

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