0

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?

127.0.0.1               localhost                       web00.example.com
173.203.99.130          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?

JdeBP
  • 3,970
  • 17
  • 17
sbrattla
  • 1,456
  • 3
  • 26
  • 48

4 Answers4

6

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 127.0.0.1 traffic connecting to the public interface if you setup /etc/hosts to point the domains at 127.0.0.1 but of course the virtual hosts would have to be setup to listen on 127.0.0.1 as well for that to work.

digitaladdictions
  • 1,465
  • 1
  • 11
  • 29
2

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

nitins
  • 2,527
  • 15
  • 42
  • 65
2

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.

quanta
  • 50,327
  • 19
  • 152
  • 213
1

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.