X
X
X
X

Knowledge Base

HomepageKnowledge BaseGeneralHow to Use the Hosts File to Test Y...

How to Use the Hosts File to Test Your Website on a New Server Before Changing DNS

In short: The hosts file is a plain text file that maps domain names to IP addresses and that your computer checks before it asks DNS. When you move a site to a new server, one line in this file lets you open and test the site from the new server on your own computer only, without changing DNS records and without affecting visitors.

This is most useful when migrating a website to a new host: once files and the database are on the new server, you confirm everything works before you point the domain there.

What is the hosts file and how does it work?

Each line of the hosts file holds an IP address followed by one or more names. According to the Linux hosts(5) manual page, the format is "IP address, canonical name, optional aliases", with fields separated by spaces or tabs. Everything after a "#" is a comment.

Programs that use the system resolver, such as browsers and ping, read this file before they ask DNS. If there is a match, the IP address in DNS is ignored on that computer. The change affects only the computer you edit; every other visitor keeps reaching the site wherever DNS points.

What do you need before you start?

  • The new server's IP address: it is listed in the service details of your new hosting account. If you cannot find it, open a support ticket.
  • The site already on the new server: files and database copied, and the domain set up on the new account.
  • Administrator rights: an administrator account on Windows, sudo rights on Linux and macOS.

The examples below use 203.0.113.10, an address reserved for documentation, as the new server and example.com as the domain. Replace them with your own IP address and domain.

Hosts file diagram: Windows and Linux/macOS hosts paths, 203.0.113.10 example.com lines, test traffic to the new server and the bypassed DNS path

How do you edit the hosts file on Windows?

1. Find Notepad in the Start menu, right-click it and select Run as administrator. As Microsoft's documentation explains, an editor that is not running as administrator fails with an access error when you save the file.

2. In Notepad, open C:\Windows\System32\drivers\etc\hosts. The file has no extension, so you may need to change the filter in the open dialog to show all files.

3. Add these two lines at the end of the file:

203.0.113.10 example.com
203.0.113.10 www.example.com

4. Save the file, then clear the DNS cache in Command Prompt with ipconfig /flushdns.

The hosts file on Linux and macOS

On Linux and macOS the file is /etc/hosts and is edited with sudo. On Linux, open it with, for example, sudo nano /etc/hosts, add the same two lines and save. On distributions that use systemd-resolved, clear the cache with resolvectl flush-caches. On macOS, open the file with sudo in Terminal and add the same lines; if the change does not show, quit the browser completely and open it again.

How do you check that the change works?

SystemCommandWhat you should see
Windowsping example.comThe new server's IP address in the reply lines
Linuxgetent hosts example.comThe new server's IP address at the start of the line
macOSping example.comThe new server's IP address in the reply lines

Do not use nslookup or dig for this check: they send the query straight to a DNS server, ignore the hosts file and show you the old address. When you do want to check the DNS side, our guide to checking DNS with dig and nslookup helps. If the IP address is right, open the site in your browser and try the pages, forms and admin area.

What if you get a certificate warning over HTTPS?

If the new server has no certificate for your domain yet, the browser may show a security warning. Automatic certificates such as Let's Encrypt use the HTTP-01 challenge, which fetches a validation file through your domain on port 80; your hosts file does not affect that check. That is why an automatic certificate is usually issued after DNS points to the new server. For background, see what an SSL certificate is.

What should you do after testing?

If everything works, point the domain's DNS record to the new server. After the switch, delete the lines you added to the hosts file or turn them into comments by putting "#" in front. If you forget them, your computer keeps opening the site from the old address when the server IP changes in the future. A DNS change can take time to reach everyone; our guide on DNS propagation explains why.

Frequently Asked Questions

Does a hosts file change affect other visitors?

No. The hosts file applies only to the computer where you edit it. Other visitors keep reaching the site wherever its DNS records point.

Why do I get an access error when saving the hosts file?

The file requires administrator rights. On Windows, run Notepad as administrator; on Linux and macOS, open the file with sudo.

The site still opens from the old server after my change. Why?

Your computer or browser may still hold the old record in its DNS cache. On Windows run ipconfig /flushdns, quit and reopen the browser, and check the IP address with ping.

When should I remove the line from my hosts file?

Remove it once DNS points to the new server and the site opens from there for everyone. That way your computer follows future IP changes like everyone else.

Can't find the information you are looking for?

Create a Support Ticket
Did you find it useful?
(27 times viewed / 0 people found it helpful)

Powered by WISECP
Top