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)?

5 Answers 5


It all depends on your needs!

In a normal case i would suggest using the IP that causes the least effort. For example: 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...

  • 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. Apr 20, 2011 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, 2011 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 loopback
    – BParker
    Apr 20, 2011 at 13:36

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


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.

  • Is that performance-wise? For how long will local machine cache the hostname IP it received from external DNS server? Apr 20, 2011 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, 2011 at 9:57
  • 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, 2011 at 10:01

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
# 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; shouldn't be faster at all. If you are not convinced, ping, 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.


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.

  • I don't see any bad about that also. What's the advantage to have it set to external IP? Apr 20, 2011 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, 2011 at 8:36

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.