The localhost file lives in a different place depending on your operating system
The localhost file (also called the hosts file) is a simple text document your computer checks before asking the internet for a web address. It sits on your own machine, not on the internet. When you type a web address into your browser, your computer looks in this file first to see if it has a local answer — if it does, it uses that instead of going online.
Where this file lives depends on what operating system you use. On Windows, it's in C:\Windows\System32\drivers\etc\hosts. On Mac and Linux, it's in /etc/hosts. The file has no file extension — it's just called "hosts", not "hosts.txt".
Most people never need to touch this file. Your computer manages it automatically. But if you're setting up a local web server, testing a website before it goes live, or blocking certain websites on your own machine, you may need to open it and add entries yourself.
Key Takeaways
- On Windows, the hosts file is at C:\Windows\System32\drivers\etc\hosts; on Mac and Linux, it's at /etc/hosts.
- You need administrator or root permissions to edit the hosts file — your operating system protects it to prevent accidental changes.
- Each line in the hosts file maps an IP address to a domain name, telling your computer to use a local address instead of looking online.
- Changes to the hosts file take effect immediately, but your browser may need to be closed and reopened to see the change.
How to find the hosts file on Windows
On Windows, open File Explorer and navigate to C:\Windows\System32\drivers\etc. You should see a file called "hosts" with no extension. If you don't see it, make sure you're viewing hidden files — in File Explorer, click the View tab and check "Hidden items".
Do not try to open it by double-clicking. Right-click the hosts file, select "Open with", and choose Notepad or another plain text editor. If you see a dialog asking for permission, click "Yes" — Windows is confirming that you want to make changes to a protected system file.
If you cannot save changes after editing, you may not have administrator permissions. Close the file without saving, right-click Notepad itself, select "Run as administrator", then use File > Open to navigate to the hosts file again.
How to find the hosts file on Mac and Linux
On Mac, open Terminal (in Applications > Utilities) and type sudo nano /etc/hosts, then press Enter. You'll be asked for your password — type it (you won't see the characters appear, which is normal). The hosts file will open in the nano text editor.
On Linux, the command is the same: sudo nano /etc/hosts. The sudo part means "run this as the administrator", which is required because the hosts file is protected. After you finish editing, press Ctrl+X, then Y, then Enter to save and exit.
If you prefer a graphical editor instead of the terminal, you can also open the file with a text editor like gedit (Linux) or TextEdit (Mac), but you'll still need to use "Open as Administrator" or the equivalent for your system.
What the localhost file actually contains
The hosts file is just plain text. Each line has an IP address, followed by spaces or tabs, followed by a domain name. The most common entry is 127.0.0.1 localhost, which tells your computer that "localhost" refers to itself.
When you add a new entry, you're telling your computer: "When I type this web address, use this IP address instead of asking the internet." For example, if you're testing a website on your own machine before uploading it to a real server, you might add 127.0.0.1 mysite.local. Then when you type mysite.local into your browser, it connects to your local web server instead of looking for a real website.
Lines that start with a # are comments — your computer ignores them. You can use comments to label what each entry does, which is helpful if you come back to the file months later.
Why you might need to edit the hosts file
The most common reason is testing a website locally. If you're a web developer or designer, you build and test on your own machine before the site goes live. The hosts file lets you use a realistic domain name (like mysite.local) instead of an IP address, which makes testing more accurate.
Some people also use the hosts file to block websites on their own machine. By pointing a domain to a non-existent IP address like 0.0.0.0, you can prevent your browser from reaching that site. This is a basic parental control or a way to reduce distractions, though it's not foolproof — someone determined can bypass it.
Occasionally, malware or unwanted software modifies the hosts file to redirect you to fake websites or block legitimate ones. If you notice websites not loading correctly or being redirected unexpectedly, checking the hosts file is a reasonable troubleshooting step.
What happens after you edit the hosts file
Changes take effect almost immediately — you don't need to restart your computer. However, your browser may have cached the old answer, so close the browser completely and reopen it. If you're still not seeing the change, try clearing your browser's DNS cache or restarting your computer.
If you make a mistake in the hosts file, your computer may not be able to reach certain websites. The syntax is strict: the IP address and domain name must be separated by at least one space or tab, and each entry must be on its own line. If you accidentally break the file, you can delete the line you added and save again — the file will work normally once you remove the bad entry.
Keep a backup of the original hosts file before you start editing, especially if you're making multiple changes. On Windows, you can copy the file to your Desktop. On Mac and Linux, you can use the terminal command sudo cp /etc/hosts /etc/hosts.backup to create a copy.
Frequently Asked Questions
Can I use the hosts file to block ads or trackers?
Yes, but it's limited. You can point ad domains to a non-existent IP address, and some people maintain long lists of known ad servers for this purpose. However, this approach only works on your own machine and doesn't block ads on mobile devices or other computers on your network. Browser extensions or a network-level ad blocker (like Pi-hole) are usually more effective.
What if I accidentally delete the hosts file?
Your computer will still work — the hosts file is optional. Windows and Mac both have a default version that gets recreated if it's missing. If you deleted it and want to restore it, you can create a new one with just the line 127.0.0.1 localhost, or restore from your backup if you made one.
Does editing the hosts file affect other people on my network?
No. The hosts file only affects your own computer. Other devices on your network have their own hosts files and won't see your changes. If you want to block or redirect websites for everyone on your network, you'd need to change settings on your router or use a network-level tool.
Why do I get a permission error when I try to save?
The hosts file is protected because changes to it can break your internet connection or create security problems. On Windows, you need to open your text editor as administrator before opening the file. On Mac and Linux, you need to use sudo in the terminal. If you're still getting an error, your user account may not have administrator permissions on that computer.
Can malware hide itself in the hosts file?
Malware can modify the hosts file to redirect you to fake websites or block security tools, but it can't hide inside the file itself — the file is just text that your computer reads. If you suspect malware, checking the hosts file is a reasonable step, but you should also run a full antivirus scan and consider having a security professional review your system.