The hosts file is a plain text file on your computer that maps domain names to IP addresses, and its location depends on your operating system

Your computer checks the hosts file before it queries a DNS server. If you add an entry there — say, pointing example.com to 192.168.1.100 — your browser will go to that IP address instead of the real one. The file is useful for testing websites before they go live, blocking ads or malicious domains, or running local development servers. But finding it requires knowing where your operating system stores it, and editing it requires administrator access.

The hosts file has no file extension and is named exactly hosts — nothing before or after. It lives in a system folder that is hidden by default on most machines, which is why many people think it does not exist when they first look for it.

Key Takeaways

  • On Windows, the hosts file is at C:\Windows\System32\drivers\etc\hosts and requires administrator access to edit.
  • On Mac, it is at /etc/hosts and you can edit it with Terminal using the nano command or by changing file permissions in Finder.
  • On Linux, it is at /etc/hosts and you can open it with any text editor after using sudo to gain root access.
  • The file contains one entry per line in the format IP address followed by a space or tab, then the domain name.
  • You must save the file as plain text with no extension, and changes take effect immediately without restarting.

Finding the hosts file on Windows

On Windows, open File Explorer and navigate to C:\Windows\System32\drivers\etc\. The hosts file sits in the etc folder. If you do not see it, you may need to show hidden files: click the View tab at the top, then check the box next to Hidden items.

Do not try to edit the file by double-clicking it. Instead, right-click the hosts file, select Open with, and choose Notepad. If Notepad does not appear in the list, click Choose another app and find Notepad in the full list. Then click Open as administrator at the bottom of the window — this is required because the file is protected by Windows.

If you do not see an "Open as administrator" option, close Notepad, right-click the hosts file again, and select Run as administrator from the context menu. Then use File > Open inside Notepad to navigate to the hosts file and open it from there.

Finding the hosts file on Mac

On Mac, the hosts file is at /etc/hosts, but it is not visible in Finder by default. The fastest way to edit it is through Terminal. Open Terminal (search for it in Spotlight with Command+Space), then type sudo nano /etc/hosts and press Enter. You will be asked for your password — type it and press Enter again.

Nano is a simple text editor that opens inside Terminal. Add your entries at the bottom of the file, one per line, in the format IP_address domain_name — for example, 127.0.0.1 mysite.local. When you are done, press Control+X, then Y to confirm you want to save, then Enter to keep the filename.

If you prefer not to use Terminal, you can also edit the file in a graphical text editor. Open Finder, press Command+Shift+G to open the Go to Folder dialog, type /etc/hosts, and click Go. Then right-click the hosts file, select Get Info, scroll to Sharing & Permissions at the bottom, and change your user from Read only to Read & Write. Close the info window, then double-click the hosts file to open it in your default text editor.

Finding the hosts file on Linux

On Linux, the hosts file is at /etc/hosts. Open a terminal and type sudo nano /etc/hosts to edit it with nano, or sudo gedit /etc/hosts if you prefer a graphical editor like gedit. You will be prompted for your password.

Add your entries one per line in the format IP_address domain_name. You can add comments by starting a line with a hash symbol (#). When you are done in nano, press Control+X, then Y, then Enter. In gedit, just save the file normally with Control+S.

Some Linux distributions use different text editors by default. If nano or gedit is not installed, try sudo vi /etc/hosts instead, though vi has a steeper learning curve. Alternatively, use sudo cat /etc/hosts to view the file without editing, which helps you understand its current format before you make changes.

The format and syntax of the hosts file

Each line in the hosts file contains an IP address, followed by one or more spaces or tabs, followed by one or more domain names. For example:

127.0.0.1 localhost 192.168.1.100 mysite.local www.mysite.local 0.0.0.0 ads.example.com

The first entry, 127.0.0.1 localhost, is usually already there and points to your own computer. Lines starting with a hash symbol (#) are comments and are ignored. You can add comments to explain what each entry does, which is useful if you come back to the file months later.

IP addresses can be IPv4 (like 192.168.1.1) or IPv6 (like ::1). You can list multiple domain names on the same line, separated by spaces, if they all point to the same IP address. Changes take effect immediately — you do not need to restart your computer or your browser, though some browsers cache DNS results, so you may need to clear your browser cache or restart it to see the change.

Common entries and what they do

127.0.0.1 localhost points to your own computer. This entry is always present and should not be removed.

0.0.0.0 domain.com blocks access to a domain by pointing it to a non-routable address. This is useful for blocking ads, trackers, or malicious sites. When you try to visit the domain, your browser will show a connection error instead of loading the site.

192.168.1.100 mysite.local points a local domain name to a specific IP address on your network. This is common in development when you want to test a website running on another machine or a local server before it goes live.

::1 localhost is the IPv6 equivalent of 127.0.0.1 and may already be in your file. Both can coexist without conflict.

Troubleshooting when changes do not take effect

If you edited the hosts file but your browser still goes to the wrong site, first verify that you saved the file correctly. Open it again and confirm your entry is there and spelled correctly — domain names are case-insensitive, but typos will break the entry.

Clear your browser's DNS cache. In Chrome, go to chrome://net-internals/#dns, click Clear host cache, and refresh the page. In Firefox, go to about:config, search for network.dnsCacheExpiration, and set it to 0, then back to 3600. In Safari, open Terminal and type sudo dscacheutil -flushcache.

On Windows, you can also flush the system DNS cache by opening Command Prompt as administrator and typing ipconfig /flushdns. On Mac, type sudo dscacheutil -flushcache in Terminal. On Linux, the cache behavior depends on your system configuration, but restarting your browser usually works.

If you still have trouble, double-check that you have administrator or root access and that the file was saved as plain text with no file extension. Some text editors add .txt automatically — if your file is named hosts.txt, rename it to hosts with no extension.

Frequently Asked Questions

Can I edit the hosts file without administrator access?

No. The hosts file is protected because changes to it affect how your entire computer resolves domain names. You must have administrator access on Windows, be able to use sudo on Mac and Linux, or have file permissions changed to allow editing. If you do not have this access, you cannot edit the file.

Will editing the hosts file affect other people on my network?

No. The hosts file is local to your computer only. Changes you make affect only your machine. Other computers on your network will not see your entries and will continue to resolve domains normally.

What happens if I delete the hosts file by accident?

Your computer will still work, but it will lose any custom entries you added. On Windows, you can restore it from a system backup or copy a fresh one from another Windows machine. On Mac and Linux, the system usually has a backup, or you can recreate a minimal hosts file with just the localhost entry.

Can I use the hosts file to block websites?

Yes. Point the domain to 0.0.0.0 or 127.0.0.1, and your browser will fail to connect when you try to visit it. However, this only works on your own computer — it does not block the site for other people on your network. For network-wide blocking, you would need to configure your router or use a DNS-level filter.

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

No. Changes take effect immediately. You may need to clear your browser's DNS cache or restart your browser to see the change, but you do not need to restart your computer.