Where Is the Hosts File? Editing It on Windows, Mac and Linux

Sunucun 4 min read

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.

Illustration: Linux

Where Is the Hosts File?

Operating systemFile path
Windows 10 / 11C: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.com

Wildcards (*.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

  1. Search for Notepad in the Start menu, right-click it and choose Run as administrator.
  2. Use File → Open to go to the C:WindowsSystem32driversetc folder.
  3. Set the file type to All Files (*.*) and open the hosts file.
  4. Add your lines at the bottom and save.
  5. Flush the DNS cache by typing ipconfig /flushdns in 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

  1. Open Terminal, type sudo nano /etc/hosts and enter your Mac password.
  2. Add your lines, save with Ctrl + O and exit with Ctrl + X.
  3. 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

Latest guides