The Hosts File: A Local Override for Names
Find and safely edit the hosts file on Windows, macOS and Linux to test servers, block names and spot unwanted changes.
Key takeaways
- Each line in the hosts file pairs an IP address with one or more names, and it applies only to the device holding the file.
- Editing it requires administrator rights, and wildcards are not supported.
- Tools such as nslookup and dig query DNS servers directly, so they ignore your hosts entries.
- Remove temporary entries as soon as testing ends, and treat unexplained lines as a warning sign.
The hosts file is a plain text file on your computer that maps names to IP addresses. Your operating system normally consults it before asking DNS, so a line in it overrides whatever the rest of the internet says, on that one machine only.
That makes it handy for testing a new web server before you change public DNS, for giving a lab device a friendly name or for blocking a few hostnames. It's also a place where forgotten test entries and unwanted software can quietly redirect traffic, so it's worth knowing how to read it.
Where to find it
The file is simply called hosts, with no extension.
| System | Location |
|---|---|
| Windows | C:\Windows\System32\drivers\etc\hosts |
| macOS | /etc/hosts |
| Linux | /etc/hosts |
Mobile operating systems generally don't let you edit this file without modifying the system in ways that weaken its security. For phone testing, a local DNS server or a development proxy is the usual route.
Before DNS existed, early networked computers relied on a shared hosts file that listed every machine by hand. As the network grew, keeping one list current became impractical, and the distributed system described in DNS explained replaced it. The local file survived as an override.
Reading the format
Each entry is one line: an address, some white space, then one or more names. Anything after a # is a comment.
127.0.0.1 localhost
::1 localhost
192.168.1.50 nas.home.arpa nas
203.0.113.25 www.example.com example.com # new web server test The first two lines are standard. 127.0.0.1 and ::1 are loopback addresses that refer to the machine itself, as explained in loopback and 127.0.0.1.
Some rules catch people out. There are no wildcards, so *.example.com won't work. You can't specify a port or a path. A name must be listed exactly, so overriding example.com leaves www.example.com untouched unless you add it too.
Editing it on Windows
You need administrator rights. Open the Start menu, type Notepad, right-click it and choose to run it as administrator. Then use File, Open, paste the full path and set the file filter to show all files so hosts appears.
Make your change and save. Watch that Notepad doesn't save a second file named hosts.txt beside the original. If File Explorer hides extensions, that mistake is hard to see.
From an elevated PowerShell window you can view the file and clear the resolver cache.
Get-Content C:\Windows\System32\drivers\etc\hosts
ipconfig /flushdns Security software sometimes protects the file and reverts changes. If your edit vanishes, check that software's alerts before trying again.
Editing it on macOS and Linux
Open the file with a text editor under sudo.
sudo nano /etc/hosts Save and exit. Changes are usually picked up right away. If they aren't, flush caches. On macOS run sudo dscacheutil -flushcache and sudo killall -HUP mDNSResponder. On Linux with systemd-resolved, run sudo resolvectl flush-caches.
On Linux, the order in which sources are consulted is set in /etc/nsswitch.conf. A line such as hosts: files dns means the file comes first. Some cloud images and network tools regenerate /etc/hosts at boot, so look for a comment at the top that says the file is managed.
Checking that an override works
This is where many people get confused. nslookup, dig and host send questions straight to DNS servers. They will keep showing the public answer even when your override is working.
Use something that goes through the system's normal name resolution instead.
# Linux
getent hosts www.example.com
# macOS
dscacheutil -q host -a name www.example.com
# macOS, Linux or Windows
ping www.example.com On Windows PowerShell, Resolve-DnsName does consider the hosts file unless you tell it otherwise. In any case, the address that ping prints is what matters, whether or not the target replies.
Testing a new server before cutover
Say you've copied a website to a new server at 203.0.113.25, and public DNS still points to the old one. Add the line shown earlier, flush caches and restart your browser. You now see the new server under the real domain name, which means virtual host settings, redirects and links behave as they will in production.
Keep in mind that HTTPS still applies. The new server needs a valid certificate for the name, or the browser will warn you. Some certificate methods can't issue one until DNS points at the server, while DNS-based validation can. Automated certificates with ACME explains the difference.
If you only need to test one request, you can skip editing the file. curl accepts a temporary override.
curl -I --resolve www.example.com:443:203.0.113.25 https://www.example.com/ When the test passes, remove the line, then follow your normal DNS change plan.
Blocking names with the hosts file
Pointing a name at 0.0.0.0 or 127.0.0.1 makes connections to it fail on that machine. 0.0.0.0 is often preferred because it fails quickly and doesn't hit any local web server.
0.0.0.0 ads.example.net
0.0.0.0 tracker.example.net This works for a handful of names. It scales poorly: there are no wildcards, every device needs its own copy, very large lists can slow some systems and anyone with administrator rights can undo it. For a household or office, a filtering resolver or a network-wide blocker such as Pi-hole is easier to maintain.
When the file works against you
Because it overrides DNS, the hosts file is attractive to malicious software. Redirecting a bank's name to a hostile server, or blocking security vendors' update servers, are classic examples. HTTPS limits the damage, since the impostor server can't normally present a trusted certificate, but blocked updates are still a real problem.
If one computer reaches the wrong site while others are fine, open the file and look for lines you can't explain. Check for entries pushed far below the visible area by many blank lines. Remove anything unfamiliar, then run a full scan with your security software. A managed work computer may contain legitimate entries added by your IT team, so ask before deleting those.
Naming devices on your own network
On a small network, a few hosts lines can save you from memorizing addresses. Pointing nas at the storage box or printer at the printer lets you type a name in a browser or terminal.
Two conditions make this reliable. The device's address has to stay put, which you arrange with a static address or a DHCP reservation on the router. And every computer that should know the name needs the same line. Once you maintain more than two or three machines, put the names in your router's local DNS settings if it offers them. You change them once and every device sees the result, including phones and televisions that have no editable hosts file.
Troubleshooting an override that won't work
Work through these in order when a new line seems to be ignored.
- Check the file you edited. Confirm the path, and that no
hosts.txtor backup copy received your change instead. - Check the line itself. The address comes first. A name first, or a URL with
https://or a path, won't work. - Check the exact name. The browser may be loading
www.example.comwhile you listed only the bare domain, or a redirect may send you to a different hostname. - Check both address families. If you override only IPv4, behavior can differ between systems when the real name also has IPv6 addresses. Add an IPv6 line if your test server has one.
- Check invisible characters. Copying from a web page or word processor can bring in odd spaces or curly punctuation. Retype the line.
- Restart the program. Browsers keep connections and their own caches, so close every window.
- Check the proxy and VPN. Either one may resolve names somewhere other than your computer.
A quick test with ping or getent tells you whether the operating system sees the entry. If it does and the browser still goes elsewhere, the problem is in the browser or in a proxy.
Sharing and automating changes
Teams sometimes paste hosts lines into chat so several people can test the same server. Send the whole line with a comment saying what it's for and when to remove it. For anything longer-lived, a test hostname in real DNS, such as new.example.com, is cleaner. It works on phones, expires from caches normally and doesn't leave leftovers on a dozen laptops.
Scripts that modify the hosts file should back it up first, add lines between clear start and end comments and remove only their own block. Run them only if you've read what they do, since anything with administrator rights over this file can redirect your traffic.
Things hosts entries can't do
Not every program honors the file. Applications that run their own DNS client, some containers with their own network settings and some browser configurations using encrypted DNS may behave differently. Containers and virtual machines have separate hosts files from the computer running them. A web proxy resolves names on its own side, so your local override has no effect on proxied traffic.
An open connection also survives your edit. Close and reopen the program if it keeps reaching the old address. And remember that the override affects nobody else: sharing a link won't show a colleague what you see unless they add the same line.
Good habits
- Add a comment with the reason and date next to every custom line.
- Keep a backup copy before editing.
- Remove test entries the moment you finish.
- For private names, use a domain set aside for home networks such as
home.arpa, which avoids colliding with real public names. - Check the hosts file early when only one machine misbehaves, before you spend time on flushing DNS caches elsewhere.