Take the 2-minute tour ×
Server Fault is a question and answer site for professional system and network administrators. It's 100% free, no registration required.

Which IP should hostname machine.example.com resolve to in /etc/hosts?

  • To localhost (127.0.0.X),
  • to a local network (10.X.X.X or 192.168.X.X),
  • or to the external server IP visible from outside (X.X.X.X)?
share|improve this question
add comment

5 Answers

up vote 4 down vote accepted

It all depends on your needs!

In a normal case i would suggest using the IP that causes the least effort. For example:

127.0.0.1 will cause traffic to only move within the TCP stack.

If you were to put in your LAN address, potentially traffic could be sent out from your nic, then returned by your router/switch.

If you put in the public IP address, the traffic could potentially leave your nic, through your router, then returned from your ISP back to your router, then back to your nic.

In reality the various pieces of kit should be intelligent enough to not make things go that far, but they potentially could.

On the other hand, it can be advantageous for a machine to be able to contact itself as if it were a public server, but in my experience thats only for development purposes, where for example a basic LAMP stack for web dev has the DB on the same machine as the web server, but in the production case it would be seperate. It can be good to make sure everything acts more like the production case by making it talk over non-localhost IP's...

share|improve this answer
1  
I think most OSes in these days are smart enough to detect traffic, directed to a local address, be it a localhost, LAN or externally accessible IP so that it never leave machine. –  FractalizeR Apr 20 '11 at 9:54
1  
I must agree, I doubt there are many OS's around that would make such a dumb decision, but I work for a company that makes some specialised networking kit, and it's something that has to be added to the stack to make it do the intelligent thing. I guess i'm just assuming a worst case, and then letting anything better help me out! –  BParker Apr 20 '11 at 13:35
    
Actually, further to that, i wouldn't often even use the hostname file, i would rather let DNS resolve a FQDN to the public IP, local name to local LAN IP and localhost to 127.0.0.1 loopback –  BParker Apr 20 '11 at 13:36
add comment

Since your machine have a public IP, it is good to point hostname to it.

share|improve this answer
add comment

In general I agree with @BParker in that there are scenarios where each is more useful, and that generally making it a localhost alias would be the most effective use of resources baring some kind of test environment.

However I would also like to suggest that the most manageable solution is to not have this entry at all and rely on DNS. Unless you configure your DNS to do special tricks for local replies, this will generally give you the world routeable address for a machine, but it gives you the flexibility of managing network services without looking for stray entries on every machine. If you have services that connect to the best of machine.domain.com or service.domain.com names, you can change where services are hosted and move machines around without having to either reprogram your software or make changes to scattered /etc/hosts files.

share|improve this answer
    
Is that performance-wise? For how long will local machine cache the hostname IP it received from external DNS server? –  FractalizeR Apr 20 '11 at 9:55
    
Long enough :-) That is of course configurable, but any self respecting system will have caches and timeouts long enough to reduce to re-lookup overhead to a negigable value. –  Caleb Apr 20 '11 at 9:57
    
thanks :) –  FractalizeR Apr 20 '11 at 9:59
1  
If you are making enough requests to various places that the overhead of DNS is a concern, you should probalby be running bind locally or at least make sure you have a specialized caching looking agent like nscd installed and configured. –  Caleb Apr 20 '11 at 10:01
add comment

This used to be written at the top of the hosts file for some linux distribution:

# By the way, Arnt Gulbrandsen <agulbra@nvg.unit.no> says that 127.0.0.1
# should NEVER be named with the name of the machine.  It causes problems
# for some (stupid) programs, irc and reputedly talk. :^)

IP addresses are really bound to interfaces, not machines, so according to the theory there should be one different name per interface - use a private domain or subdomain to put the internal addresses.

Any half-decent network stack will recognize as such an address that has been configured on a local interface, and loop back packets going to it well before it hits the network interface driver, let alone the network itself; 127.0.0.1 shouldn't be faster at all. If you are not convinced, ping 127.0.0.1, then your local address, and compare the RTTs. On my system they are both equally fast, and an order of magnitude faster than pinging the local gateway.

share|improve this answer
add comment

Common sense:

if you have an external domain, what good wuold localhost or local network do? None.

Even internally, if I enter machine.example.com I am not niterested in localhost.

share|improve this answer
    
I don't see any bad about that also. What's the advantage to have it set to external IP? –  FractalizeR Apr 20 '11 at 8:11
1  
The fact taht if it is an external server it CAN BE REACHED? IT is like publishing your phone number in an advertising and givign it thei nternal nnumber, no the compelte public number. Public IP = put public IP into dns. Itnernal IP = pub internal IP in dns. NEVER put localhost there. –  TomTom Apr 20 '11 at 8:36
add comment

Your Answer

 
discard

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.