Hosts file: test a website before changing its DNS

Updated on

What the hosts file is for

During a migration, your website exists in two places: on the old server, which your domain name still points to, and on the new one, where it is waiting. Until the DNS are changed, everyone reaches the old one.

The hosts file lets you see the website as it is on the new server, under its real domain name, before changing the DNS. You tell your computer, and only your computer: "for this domain name, go to this IP address".

  • Only your computer is affected: your visitors keep seeing the old website.
  • Nothing is changed on the website, on the server or in the DNS zone.
  • You go back to normal by removing the line you added.

It is the most reliable way to check a migration before making it public.

Certificate warning: it is expected

During the test, your browser may show a security warning. As long as the domain name does not point to the new server, that server often only has a temporary ("self-signed") certificate, which browsers do not recognise. This is not a fault in the migration: bypass the warning ("Advanced", then "Proceed to the site").

If the browser does not offer to bypass it, the website uses HSTS: an instruction the website gives browsers to accept, for this domain name, only HTTPS connections with a valid certificate. The browser remembers it from one visit to the next. In that case, open the website in a private browsing window: it does not know this instruction and lets you bypass the warning.

At LRob, when we manage the DNS zone of your domain, the final certificate can be installed before the switch: there is then no warning at all.

How it works

When you open a website, your computer first has to find the IP address of the server hosting it. It looks first in a small local text file, the hosts file, and only queries the DNS servers if it finds nothing there.

A line of this file ties an IP address to a name:

203.0.113.10 example.com
203.0.113.10 www.example.com

With these two lines, your browser sends requests for example.com and www.example.com to the server 203.0.113.10, whatever the DNS say.

What you need

  • The IP address of the new server. In Plesk, it is shown on your domain’s card, under "Websites & Domains". You can also ask us for it in a ticket.
  • The names to test: the domain name, its www version, and each subdomain involved (one name per line).
  • An administrator account on your computer: the hosts file is protected.

Edit the hosts file on Windows

  1. Open the Start menu and type Notepad.

  2. Right-click "Notepad", then "Run as administrator".

  3. In Notepad: File, Open, then paste this path into the "File name" field:

    C:\Windows\System32\drivers\etc\hosts
    
  4. Add your lines at the end of the file, with the IP address of the new server and your names:

    203.0.113.10 example.com
    203.0.113.10 www.example.com
    
  5. Save (Ctrl + S).

The change usually takes effect straight away. If it does not, flush the Windows DNS cache from the Command Prompt:

ipconfig /flushdns

Disabling then re-enabling the network adapter (or Wi-Fi) can also be enough.

Edit the hosts file on macOS

  1. Open the Terminal application.

  2. Open the file with administrator rights (your password is asked for):

    sudo nano /etc/hosts
    
  3. Add your lines at the end of the file, with the IP address of the new server and your names:

    203.0.113.10 example.com
    203.0.113.10 www.example.com
    
  4. Save with Ctrl + O then Enter, and quit with Ctrl + X.

The change usually takes effect straight away. If it does not, flush the DNS cache:

sudo dscacheutil -flushcache; sudo killall -HUP mDNSResponder

Edit the hosts file on Linux

  1. Open a terminal.

  2. Open the file with administrator rights:

    sudo nano /etc/hosts
    
  3. Add your lines at the end of the file, with the IP address of the new server and your names:

    203.0.113.10 example.com
    203.0.113.10 www.example.com
    
  4. Save with Ctrl + O then Enter, and quit with Ctrl + X.

The change usually takes effect straight away. If it does not and your system uses systemd-resolved, flush its cache:

resolvectl flush-caches

Check that the change is active

Close your browser completely, then open it again: it also keeps addresses in memory.

To check which server your computer goes to, run this in a terminal or a Command Prompt:

ping example.com

The IP address shown must be the new server’s.

If you still see the old website

  • The browser was not restarted. Close all its windows, or try another browser.
  • The old address is still in memory. Flush the DNS cache, as described above for your system.
  • A name is missing. example.com and www.example.com are two different names: each needs its own line.
  • The website is also reachable over IPv6. Add one more line with the IPv6 address of the new server, for each name.
  • You are going through a proxy or a VPN. It is then the one looking up the website’s address, without reading your hosts file. Turn it off for the test.
  • The file was not saved. Without administrator rights, saving is refused or goes to another file. Open the file again to check that your lines are there.

After the test

Remove the lines you added, or put a # at the start of each one to disable them, then save. Otherwise your computer will keep ignoring the DNS for this domain name, including after a later change of server.

On a phone or a tablet, the hosts file cannot be edited simply: run this test from a computer.