I'm about to set up a new Ubuntu 10.04 server which will act as a web server. The server will host a couple of domains, including a few subdomains. Does the '/etc/hosts' need to contain all domains and subdomains, or is it enough just to let it contain the standard localhost line and the public IP address line like below?               localhost                       web00.example.com          web00.example.com               web00

I presume that the consequence of not having '/etc/hosts' to list the domains which the web server hosts is that DNS lookups can't be done internally but have to be handed over to the external DNS server?


I have many servers that host multiple websites and have never setup /etc/hosts to include the domains being hosted. If your websites do reference themselves quite a bit though their might be some performance gained by negating the DNS lookups. Its never been a problem for me though and its defiantly not NEEDED but might not be a bad idea.

You may also work around any potential firewall problems with traffic connecting to the public interface if you setup /etc/hosts to point the domains at but of course the virtual hosts would have to be setup to listen on as well for that to work.

  • Thanks! I'll mark this as correct as the answer was supported with a good explanation. – sbrattla Oct 7 '11 at 6:48

It has to be there in /etc/hosts unless you want it to be handed over to the external DNS server.


Does '/etc/hosts' need to contain all domains hosted by the server?

No. You only want to do that when you test your web server on itself.


I know others have answered, and an answer has been accepted. However, since the OP sounds new at this, there are some pitfalls that should be mentioned.

/etc/hosts is just one source of resolver information. The over-the-network DNS service is another. There are more possibilities, and how resolution occurs is deteremined your nsswitch setup. So you have to balance the need for resolution against the cost of maintenance. (eg, does your web server need to resolve a hostname to figure out what IP address to bind at startup? If so, and DNS is down, your web startup fails. Perhaps you adjust your web config to not rely on DNS. What about other services that need to resolve names. and on and on. Welcome to system administration.)

Putting lots of entries in /etc/hosts is tedious, but stable (because it doesn't rely on DNS running). So some admins do put everything needed for the system, into the /etc/hosts. Some admins use huge /etc/hosts files and manage it with Puppet/et al and don't rely on DNS at all. And that's where things like Sun's old "yellow pages" (yp) files come in, have a central file shared via YP that has your resolver information.

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.