Under Linux, the /etc/hosts file can be used to override dns definitions, i.e. to point an hostname to a different ip.

I need to test a new VPS. So I added a line in the /etc/hosts file: www.mywebsite.com

I flushed all caches and went to www.mywebsite.com , but it still shows the old site. In the new site, I should see the default apache file "It Works!"

I used ping to test the ip and it shows the new ip.

I used wget to retrieve the index.html file, but it retrieves the file from the old server.

I bypassed my router to check if it¡s something related with it, but it isn't.

I booted Windows 7 and edited the hosts file and saw the same behaviour.

I use DNS Flusher Firefox add on to clear DNS cache. I also check using Midori browser private browsing (it doesn't cache DNSs).

I added another line: daniweb.com

This time, I see the this website.

If I add: www.mywebsite.com

I see a not found message from localhost.

I checked using Windows and saw the same behaviour.

Why can't I point a hostname to a new IP ?

Thanks for your help!

Recommended Answers

All 9 Replies

/etc/hosts is not used to override DNS settings. That would be /etc/resolv.conf, which in many systems is reset from network settings on reboot. It is better to use the network manager tool to reset your DNS server entries. That way, when you reboot, the DNS servers you prefer are made active in /etc/resolv.conf.

Just some additional info to clarify some things that may help.

What I'm reading here is that when you say "override" you really mean that what ever host name to IP mapping you have locally, you want that to take precedence over the records defined in the DNS zone.

So im not very striong on linux, but this question really seems more related to tcp/ip anyways ..So when you look at the hostname resolution process, entries in the hosts file will be loaded into the clients local DNS cache (at least this is how it works in Windows).

When a DNS query is performed, the local DNS cache is checked prior to querying a DNS server and since a match will occur, name resolution will stop. So this, I assume, is the "override" you are referring to.

The problem you are having with the validation part is that you are validating using your browser to see if the hosts file entries worked.

The problem is that there are a lot if other factors there.

If you want to validate that your hosts file is working, just open a command prompt and ping the hostname. If you get the expected result, hostname resolution is working.

If you open a browser and do not see the expected results, it could be browser cache related, etc...)

Thanks for your reply rubberman.

It's not my intention to user another DNS server, just to point one adress to another ip of my choice.

Can this be done using the network manager tool?

Thanks JorgeM,

You are right. I want my local IP mapping to take precedence over the records defined in the DNS zone.

I pinged the hostname and the get the expected result, i.e. the IP is the new one.

Regarding the browser. I tied using Firefox, Chrome, Midori and also wget command. In all cases I cleared the cache and reloaded the page. Midori private browsing doesn't save nameserver cache. Firefox DNS Flusher add on clears the ns cache and loads the page using the new ip. Wget linux command doens't have a nameserver cache as far as I know.

I've done it before to test remote servers, but I just cannot understand why this time it isn't working.

I am transfering a website to a new server. I need to test this server, to check if the CMS runs fine on it. To do this, I need to open the website from a browser.

Thanks again for your comments.

So you have validated that the DNS process works as expected. The issue may or may not be browser related. Are you sure the new test site is configured correctly? Accept new host headers? If load balanced, all web servers are configured?

Hi, I have still nothing installed on the new server. I should see the default "It works!" Apache message. But instead, I'm seeing the old site. And this issue is not related with the server, because I added: daniweb.com www.daniweb.com

into /etc/hosts and I still see this page after flushing DNS.


The etc hosts entry should override the information being pulled for DNS from any nameserver. Based on my own mistakes have you checked the speling in /etc/hosts and on the Location entry?

Do you have any sites running on the server or is this the first site you are adding?

Since it is Linux you are probably using Apache for the web server. Have you check to make sure Apache is running?
How is the site defined in the /etc/httpd/conf/httpd.conf file (probably under Virtual Hosts). It should lokk something like this:

NameVirtualHost *:80

<VirtualHost *:80>
    ServerAdmin webmaster@dummy-host.example.com
    DocumentRoot /www/docs/dummy-host.example.com
    ServerName dummy-host.example.com
    ErrorLog logs/dummy-host.example.com-error_log
    CustomLog logs/dummy-host.example.com-access_log common

You could try using telnet or lynx to connect and see what response you are getting:

telnet <ipaddress> 80

Lynx is a text based browser that runs under Linux.

If you simply ping from the server with:

ping www.mysite.com

what do you get back? You should see the ipaddress that is being pinged.

Hope this helps.

Thanks rch,

Yes, I've checked the spelling on /etc/hosts.

I do not have anything else running on the server, so I should see an error message or the default apache welcome page, but instead I'm seeing the old website.

I can connect using telnet but see no message and If I ping the address, the ip is the one of the new server.

Does anyone know how to point an address to a different ip using a proxy server (I'm using Ubuntu).


Additional info: I added www.mywebsite.com in the /etc/hosts file of a remote server and wget www.mywebsite.com (from the remote server) shows me the contents of the new server. so the problem is local

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, learning, and sharing knowledge.