Server Fault is a question and answer site for system and network administrators. It's 100% free, no registration required.

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

Running Ubuntu 10.04

My server serves 3 different domains using named virtual hosts in Apache2. I'm current using different Named Virtual Servers to 301 redirect www to the non-www equivalent. It's working, but I don't understand the correct entries for my /etc/hosts file and I think that is causing problems for me trying to setup Varnish.

I understand I need the localhost line       localhost localhost.localdomain

Should I also list each domain here? as in       localhost localhost.localdomain

What about the entry for the IP of the server? Do I need the following line?

< IP.Of.Server >

Also, should I be listing AND on each line, so they go into Apache and it can deal with the 301 redir?

share|improve this question
up vote 2 down vote accepted

I'm assuming this is for testing, otherwise you'd be setting up proper DNS records, not your hosts file.

What you want is for every name you want to call your web server with, to resolve to your server's IP address.

If you are testing from the server itself, then you can make everything point to, but of course also making it point to your server's actual IP address would work.

If you are testing from another machine, then of course you want every name to resolve to the server's real IP address.

The syntax is straingthforward:


...and so on.


Of course, what ErikA says is completely right. Modifying the hosts file is not needed for the server to work; it's only useful if/when you need to test it without having proper DNS records in place, or if you want to override them to connect f.e. to a test server instead of a production one.

share|improve this answer
I have proper dns records in place, so I'll just remove everything. Thanks – Dan Grec Aug 11 '11 at 20:25
What should I set as "hostname"? – Dan Grec Aug 11 '11 at 20:25

The only reason you should ever need to use your hosts file is if you don't have a functioning DNS environment or if you need to temporarily override some DNS name for testing purposes.

Modifying the hosts file is not needed for using Name Virtual Servers.

share|improve this answer
I had no idea. I have real DNS mappings set up. So I should just leave the " localhost localhost.localdomain" line and not put anyting else in there? – Dan Grec Aug 11 '11 at 20:23
Yep, that should be just fine. No need to add anything else. – EEAA Aug 11 '11 at 20:26
Normally the hosts file on a computer contains only the mappings for localhost and the computer's actual host name. – Massimo Aug 11 '11 at 20:26

The IP entry should be whatever Apache is listening on. So if you have <VirtualHost>, your hosts entry should start with

If you're listening on all your interfaces, then lucky you! You get to pick one! And will work just fine.

As for the part on the right, it must match what you type in the address bar. So if you want to be able to type in your address bar and have something happen, then you'll need that name in your hosts file telling your computer which IP that means. Apache can only handle the redirect if the traffic gets there to begin with; hence the hosts file entry.

And I'm sure I needn't say that this is only appropriate for a development setup. Host-IP address mappings should be done by a DNS server, not a static file.

share|improve this answer

Your Answer


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.