Key takeaways
- The hosts file is a plain text file on your computer that maps domain names to IP addresses, taking priority over DNS.
- Editing it lets you test a site on a new server before changing your DNS records, so visitors never see a broken migration.
- You need administrator or root privileges to edit the file on any operating system.
- After editing, flush your DNS cache and verify your changes with the ping command or a browser IP extension.
One of the most powerful tools available to anyone working on their site during a migration is their computer’s “hosts” file. The hosts file is used to map domain names to IP addresses, and can be used as an alternative to DNS. It also allows you to specify the IP address to which a website resolves on your computer, regardless of what may be published in the site’s DNS zone file.
What is the hosts file?
The hosts file is a plain-text file that serves as a miniature local DNS system, mapping domain names to IP addresses.
For instance: When you enter a website into your browser, such as example.com:
- The hosts file is first examined by your system.
- It utilizes that IP if it detects a match.
- If not, it queries a DNS server (such as your ISP or Google DNS).
- Thus, DNS can be fully overridden by the hosts file.
Example:
| 127.0.0.1 localhost 192.168.1.10 mytestsite.local |
This implies:
| localhost points to your own computer mytestsite.local will resolve to 192.168.1.10 |
Where is the hosts file located?
To edit the file on any system, you need to have admin or root access. Only your local machine is affected by the changes. You might need to clear the DNS cache after modifying (particularly on Windows/macOS).
- Windows: C:\Windows\System32\drivers\etc\hosts
- Mac (macOS): /private/etc/hosts
- Linux: /etc/hosts
Why edit your hosts file?
Editing your hosts file is useful when you want to control how your computer resolves domain names before DNS is even consulted. That gives you a lot of power for testing, debugging, and blocking. Some of the use cases are:
1. Test a migrated website safely before changing the DNS
This is the most common practical application. You don’t want users to view the website that’s been migrated to a new server just yet. Instead of changing DNS globally, you do this locally:
| NEW_SERVER_IP yourdomain.com |
Here, your computer will load the site from the new server while all others will still see the live site.
Without the hosts file, you’d need to change the DNS, which may cause downtime in some cases. Alternatively, you may use temporary URLs, but this may break certain sites, like Magento (base URL issues)
Using the hosts file, you can test the actual domain and configuration without affecting the live site.
2. Block websites locally
You can redirect unwanted domains to your own machine:
| 127.0.0.1 example.com |
This can help in blocking malicious or unwanted domains by mapping them to 0.0.0.0 or 127.0.0.1
3. Investigate server or DNS problems
You can temporarily override DNS to see if the server is operational if a domain isn’t resolving properly.
4. Get around the cache or CDN
If your website uses Cloudflare, you can test whether slowness is caused by the backend and run the site without Cloudflare caching. It can also be used to debug firewall, bot, or rate-limit issues by pointing straight to your origin server IP. You can do this by pointing directly to your origin server IP:
| ORIGIN_IP yourdomain.com |
5. Establish domains for local development
Instead of using complicated URLs like “localhost/project”, you can use the hosts file and test.
| 127.0.0.1 myproject.local |
How to edit the hosts file on Windows
- Search for Notepad, right-click, and select “Run as Administrator”
- Choose Yes on the User Account Control prompt
- In Notepad, go to File > Open and navigate to C:\Windows\System32\drivers\etc\hosts
- Add your entry at the bottom of the file in the format: IP_ADDRESS domain.com www.domain.com
- Save the file
- Flush your DNS cache: open an elevated command prompt and run ipconfig /flushdns
How to edit the hosts file on Mac
- Open Terminal (Command+Space, search “Terminal”)
- Run: sudo nano /private/etc/hosts
- Enter your password when prompted
- Navigate to the bottom of the file with arrow keys and add your entry
- Press Control+O to save, then Enter, then Control+X to exit
- Flush your DNS cache: dscacheutil -flushcache; sudo killall -HUP mDNSResponder
One-line alternative
| echo “1.1.1.1 test.com www.test.com” | sudo tee -a /private/etc/hosts >/dev/null |
How to edit the hosts file on Linux
Using vim:
- Run: sudo vim /etc/hosts
- Press i to enter insert mode, navigate to the bottom, add your entry
- Press Escape, then type :wq to save and exit
Using the command line (append method): echo “1.1.1.1 test.com www.test.com” | sudo tee -a /etc/hosts >/dev/null
Flush DNS cache (commands vary by distribution — list the main variants):
- Ubuntu/Debian: sudo service dns-clean restart
- NSCD-based: sudo systemctl restart nscd.service
Example hosts file entries
| # This is a comment (ignored by the system) # Default localhost mappings 127.0.0.1 localhost 255.255.255.255 broadcasthost: :1 localhost 123.123.123.123 yourdomain.com www.yourdomain.com # Point domain to NEW server before DNS switch |
1. Comments (#)
This is a comment. Anything starting with # is ignored. Used for notes/documentation
2. IP address
| 127.0.0.1 IPv4 loopback (Localhost) ::1 IPv6 loopback 123.123.123.123 Example external server IP |
3. Separator (space or tab)
| 127.0.0.1 localhost |
One or more spaces or tabs
4. Domain name (hostname)
The domain you want to map should not include http:// or https://
A note on Firefox and DNS over HTTPS
Mozilla Firefox may use DNS over HTTPS (DoH), which can ignore your local hosts file and resolve domains through a secure external DNS service instead. As a result even if you edit your hosts file Firefox still shows the old site. The solutions here would be:
- Try using another browser temporarily. Browsers like Google Chrome, Microsoft Edge etc. typically respect the hosts file by default.
- Disable DoH in Firefox:
- Open Firefox
- Click the menu (☰) → Settings
- Go to Privacy & Security
- Scroll down to DNS over HTTPS
- Uncheck “Enable DNS over HTTPS” or set it to “Off” depending on version
- Reload your site, your hosts file mapping should now apply.
Once your migration testing is done, re-enable DNS over HTTPS for better privacy/security.
How to verify your changes worked
After editing the hosts file, the obvious question is: “Did it actually take effect?”. Here’s how to confirm it properly (without getting misled by DNS tools or caching).
- Use ping yourdomain.com in Terminal or Command Prompt and confirm the IP matches what you added
- Note: nslookup and dig query DNS directly and bypass the hosts file, so they are not reliable for this test
- Browser option: install a browser IP extension (list Firefox and Chrome options) to see the IP a site is resolving to
- If the site still looks wrong: clear your browser cache (Control+Shift+Delete / Command+Shift+Delete), try a private/incognito window, or try a different browser
You may also try these steps.
- Use a private browsing window to view the site
- View the site in another browser
- Log out of your computer and then log back in
Testing a migrated site
Now that you can see the site on its new server, you must thoroughly test it to determine whether everything works as expected. It’s common to see some issues and error messages when testing a migrated site. Typically only minor adjustments to the server configuration, such as enabling an Apache module or adjusting a PHP directive, are needed to resolve them.
What to check once the hosts file points to the new server:
- Visit every link on the home page
- Log into the CMS (WordPress, Magento, etc.)
- Test the shopping cart and checkout if applicable
- Submit any forms on the site
- Test file uploads
- Check any third-party integrations
How to report issues to your migration team
- Note the full URL of the page
- Note the specific error message or problem
- Provide that information to the person performing your migration. If Liquid Web is handling the migration, simply paste that information into your migration ticket to ensure that the proper adjustments are made as quickly as possible
Remember, only your local machine sees the new server while you test with the hosts file. Other visitors and external services still see the live site until DNS changes publicly.
How to add comments to your hosts file
You can add comments to your hosts file with #. Comments help you label temporary entries or turn an entry off without deleting it.
Example:
# Migration test for yourdomain.com
123.123.123.123 yourdomain.com www.yourdomain.com
To disable the entry, place # at the beginning of the line:
# 123.123.123.123 yourdomain.com www.yourdomain.com
That keeps the entry in the file for reference while preventing your computer from using it.
Troubleshooting hosts file changes
If the hosts file edit doesn’t work, start with the most common issues.
- Changes are not taking effect. Flush your DNS cache, restart the browser, or try a private browsing window. You can also try another browser to rule out browser cache or browser-specific DNS behavior.
- You see “access denied” when saving. Confirm that you opened the editor as Administrator on Windows or used sudo on macOS or Linux.
- Antivirus software blocks the save. Some security tools may protect the hosts file from changes. Temporarily allow the change, save the file, and turn protection back on when you finish.
- Firefox doesn’t respect the hosts file change. Firefox DNS over HTTPS can bypass the hosts file. See the Firefox section above for instructions on disabling it temporarily.
- WSL on Windows doesn’t use the Windows hosts file change. Windows Subsystem for Linux can have its own /etc/hosts behavior inside the Linux environment. Changes in the Linux guest may only apply inside WSL, not across Windows browsers.
Alternative option: skip the hosts file entirely
If you cannot modify your hosts file successfully, you can preview the site through an external service.
SkipDNS is an online service that allows you to preview a website without changing the DNS or hosts file. It creates a preview link you can share with anyone who needs to review the site before DNS changes go live.
You can create a single free link that expires later. SkipDNS paid plans provide a set number of links that don’t expire along with other features depending on your plan.
DNS host file FAQs
DNS host file next steps
Editing the DNS hosts file gives you a local way to control where a domain resolves before public DNS changes. That makes it useful for site migrations, development work, and troubleshooting.
Start by adding one test entry at the bottom of your hosts file, flushing your DNS cache, and verifying the result with ping or a browser IP check before testing the site. If you’re migrating to a new server, Liquid Web can help you test with confidence before DNS goes live.
Explore Liquid Web’s managed VPS, dedicated servers, and migration services to find hosting built for sites that need a smoother move.


Sapta Upendran