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)?
Which IP should hostname machine.example.com
resolve to in /etc/hosts
?
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...
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
orservice.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.This used to be written at the top of the hosts file for some linux distribution:
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.
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.