The hosts file is a plain text file containing domain name to IP address mappings that your computer checks before asking DNS. It’s used to test a site on a new server without changing DNS, or to block a particular domain. In this article we explain where the hosts file is on Windows, macOS and Linux, how to edit it and common mistakes.
Where Is the Hosts File?
| Operating system | File path |
|---|---|
| Windows 10 / 11 | C:WindowsSystem32driversetchosts |
| macOS | /etc/hosts (actually /private/etc/hosts) |
| Linux | /etc/hosts |
| Android | /system/etc/hosts (can only be changed with root access) |
On Windows the file has no extension; its name is just hosts.
The Hosts File Format
Each line contains the IP address first, then one or more spaces and the domain name. Lines starting with # are comments and are ignored:
# To test the new server
203.0.113.25 example.com
203.0.113.25 www.example.com
# To block a domain
0.0.0.0 unwanted-site.comWildcards (*.example.com) can’t be used in the hosts file; you need to write each subdomain on a separate line.
How to Edit the Hosts File on Windows
- Search for Notepad in the Start menu, right-click it and choose Run as administrator.
- Use File → Open to go to the
C:WindowsSystem32driversetcfolder. - Set the file type to All Files (*.*) and open the hosts file.
- Add your lines at the bottom and save.
- Flush the DNS cache by typing
ipconfig /flushdnsin Command Prompt.
If you don’t open Notepad as administrator, you’ll get an “Access denied” error. Some security software protects the hosts file; you may need to turn this protection off temporarily while editing.
Editing on macOS
- Open Terminal, type
sudo nano /etc/hostsand enter your Mac password. - Add your lines, save with Ctrl + O and exit with Ctrl + X.
- Flush the cache:
sudo dscacheutil -flushcache; sudo killall -HUP mDNSResponder
Editing on Linux
Open and edit the file with sudo nano /etc/hosts. On distributions that use systemd-resolved, you can flush the cache with sudo resolvectl flush-caches.
What Is the Hosts File Used For?
- Site migration testing: checking the site on the new server from your own computer before changing the domain’s DNS records.
- Development environments: opening projects running on a local server with the real domain name.
- Blocking: shutting off access to certain domains by pointing them to 0.0.0.0.
- Naming devices on the network: reaching a device on the local network by name instead of IP.
Is the Hosts File Read Before DNS?
Yes. When resolving a domain name, the operating system checks the hosts file by default first and then the DNS server. That’s why an entry added to the hosts file takes effect regardless of what DNS says. If your computer opens a different server for a site than everyone else sees, check your hosts file. To compare DNS records, see our guide how to do a DNS lookup.
Common Mistakes
- Saving the file as .txt: if the file name becomes hosts.txt, the system won’t read it.
- Putting # at the start of the line: it stays a comment and doesn’t work.
- Forgetting to delete the line after testing: you’ll keep seeing the old server even after DNS changes.
- Browser cache: if the change doesn’t show, close and reopen the browser or try a private window. In some browsers the hosts file is still respected when encrypted DNS (DoH) is on, but you may need to clear the browser’s own DNS cache.
The Hosts File and Security
Some malware adds lines to the hosts file to redirect banking or security sites to fake servers, or to block antivirus updates. If you see lines you don’t recognise, scan your computer with up-to-date security software.
Restoring the Default Hosts File
The default Windows hosts file contains only comment lines. Deleting all the lines you added and saving is enough to return the file to its default state. Then run ipconfig /flushdns.
Frequently Asked Questions
I can’t see the hosts file. What should I do?
Set the file type to “All Files” in Notepad’s open window. If the file really doesn’t exist, you can create a new file named hosts with no extension in the same folder.
The change I made to the hosts file isn’t working. Why?
You may not have saved it with administrator rights, flushed the DNS cache or put a space between the IP and the domain name. Also try restarting the browser.
Can ads be blocked with the hosts file?
It’s possible with ready-made lists that point ad domains to 0.0.0.0, but keeping the lists up to date is hard. DNS-based blockers are a more practical method.
Categories
-
CentOS 7 guides
-
cPanel 20 guides
-
CyberPanel 18 guides
-
Operating Systems 81 guides
-
Juniper Networks 18 guides
-
Control Panels 50 guides
-
Linux 17 guides
-
MySQL 29 guides
-
Network 95 guides
-
Plesk Panel 12 guides
-
Virtual Servers 273 guides
-
SEO 3 guides
-
Cyber Security 8 guides
-
Technology 162 guides
-
VMware 36 guides
-
WordPress 20 guides
-
Artificial Intelligence 22 guides