User Agents

Web browsers and everyday web tech, explained

How to Test a DNS Change Early by Editing the Hosts File

· 5 min read · How the Web Works

In this article
  1. What You'll Need
  2. Step-by-Step
  3. Common Mistakes
  4. Tips
  5. Troubleshooting
  6. Frequently Asked Questions

When you switch web hosting or move a domain to a new server, DNS propagation can take hours or even days. Instead of waiting, you can preview the new site immediately by editing your computer's hosts file. This file overrides DNS for specific domains on your machine, letting you see the new server's content before anyone else does. This guide walks you through the process on Windows, macOS, and Linux, including how to revert the change safely.

What You'll Need

  • Administrator or root access to your computer (to edit the hosts file).
  • The IP address of the new server (e.g., from your hosting provider or a ping to the new host).
  • The domain name you want to test (e.g., example.com or www.example.com).
  • A text editor that can save files without adding formatting (e.g., Notepad on Windows, TextEdit in plain-text mode on macOS, or nano/vim on Linux).

Step-by-Step

  1. Find the new server's IP address. This is usually provided by your hosting company. If not, you can find it by pinging the new hostname (e.g., ping newserver.example.com) and noting the IP that responds.
  2. Locate your hosts file.
    • Windows: C:\Windows\System32\drivers\etc\hosts
    • macOS/Linux: /etc/hosts
  3. Open the hosts file with admin privileges.
    • Windows: Right-click Notepad and select "Run as administrator," then open the hosts file from within Notepad.
    • macOS: Open Terminal and run sudo nano /etc/hosts (or use sudo vim).
    • Linux: Use sudo nano /etc/hosts or sudo vim /etc/hosts.
  4. Add a line at the end of the file. The format is: IP_ADDRESS DOMAIN. For example: 203.0.113.5 example.com. Add a separate line for each subdomain you need to test (e.g., 203.0.113.5 www.example.com).
  5. Save the file. In Notepad, choose "Save As," set "Encoding" to UTF-8, and make sure "Save as type" is "All Files" to avoid adding a .txt extension. On macOS/Linux, save with Ctrl+O (nano) or :wq (vim).
  6. Clear your DNS cache. This ensures your system doesn't use a cached old IP.
    • Windows: Open Command Prompt and run ipconfig /flushdns.
    • macOS: Run sudo dscacheutil -flushcache; sudo killall -HUP mDNSResponder.
    • Linux: Depends on the distro; often sudo systemctl restart nscd or sudo systemd-resolve --flush-caches.
  7. Test the domain. Open a browser and visit your domain. You should see the content from the new server. If not, try a hard refresh (Ctrl+F5 on Windows, Cmd+Shift+R on Mac) to bypass browser cache.
  8. Remove the hosts entry when done. Once DNS has fully propagated (or you're finished testing), delete the lines you added and save the file again. Then flush DNS again to revert to normal resolution.

Common Mistakes

  • Using the wrong IP address. Double-check that the IP is correct and points to the server you intend to test.
  • Forgetting the www subdomain. If your site uses www, add both the bare domain and www, otherwise you'll see the old site on one of them.
  • Editing the wrong file. On Windows, ensure you're editing the file in System32, not a copy in your Documents folder.
  • Not running the editor as administrator. You'll get "Access denied" errors if you don't have the right permissions.
  • Leaving the hosts entry in place. This can cause confusion later when DNS changes, and it can break SSL if the IP doesn't match the certificate.

Tips

  • Use a comment to remember what you added. Start a line with # to add a note, like # Temporary for testing new server.
  • Test with a different browser or incognito mode. This avoids cached pages and extensions interfering.
  • If you're testing HTTPS, ensure the new server has a valid SSL certificate for the domain. Otherwise, you'll get a security warning.
  • For quick toggling, keep a backup of your original hosts file. You can copy it to hosts.backup before editing.
  • On Windows, you can use a tool like Notepad++ or PowerShell with admin rights to edit the file more easily.

Troubleshooting

I edited the hosts file but the site still shows the old version. First, flush the DNS cache again. Then, verify that the hosts file was saved correctly (no .txt extension on Windows). Check that the IP is correct and that the domain is spelled exactly as you typed it. Finally, try a different browser or clear your browser cache.

I get a "Permission denied" error when saving. Make sure you're running the editor as administrator (Windows) or using sudo (macOS/Linux). On Windows, if you're using Notepad, right-click and choose "Run as administrator" before opening the file.

I see the new site but images or assets are missing. This usually means the page references absolute URLs (e.g., https://cdn.example.com) that aren't covered by your hosts entry. Add those subdomains to the hosts file as well.

After removing the hosts entry, the site still goes to the new IP. This is normal if DNS has propagated by now. If not, flush DNS again and wait a few minutes. If it still resolves to the old IP, check that you didn't have another hosts entry elsewhere (e.g., in a different file).

The site loads but shows a certificate error. The new server's SSL certificate may not cover the domain or may be for a different hostname. For testing purposes, you can proceed past the warning (if you trust the server), but this isn't recommended for production.

Frequently Asked Questions

Is editing the hosts file safe?

Yes, as long as you only add temporary entries and remove them afterward. It only affects your own computer, not the rest of the internet.

Do I need to restart my computer after editing the hosts file?

No, but you should flush the DNS cache. On some older systems, a restart may be needed, but modern OSes pick up changes immediately after flushing.

Can I test multiple domains at once?

Yes, just add multiple lines to the hosts file, each with the IP and domain. For example, if you're moving a whole site, you might add entries for example.com, www.example.com, and api.example.com.

What if I don't know the new server's IP address?

You can usually find it in your hosting control panel or by pinging the temporary URL your host gave you. If you can't find it, contact your hosting provider.

Will this work for email (MX records)?

No, the hosts file only affects web traffic (HTTP/HTTPS) and other services that use hostnames. Email routing uses MX records, which are not overridden by the hosts file.

How long does DNS propagation actually take?

It varies from minutes to 48 hours, depending on your domain's TTL (time-to-live) settings and the DNS servers involved. Using the hosts file lets you bypass this wait entirely for your own testing.