Hosts File Guide: Speed Up & Block Ads
In this article
The hosts file is a small text file on your computer that maps domain names to IP addresses before your browser even asks the internet. By editing it, you can block ads, trackers, and malicious sites at the system level, speed up your browsing by skipping DNS lookups for known sites, and even redirect domains to a local server for testing. This guide shows you how to edit it safely on Windows, macOS, and Linux. You'll need administrator access to your computer and a plain text editor (like Notepad on Windows, TextEdit on macOS, or nano in a terminal).
What You'll Need
- Administrator or root access to your computer (to save the file).
- A plain text editor (Notepad, TextEdit in plain-text mode, or nano/vim in a terminal).
- The IP address and domain name you want to map (e.g.,
127.0.0.1for localhost, or a real site's IP). - Optional: a list of known ad/tracker domains (you can find reputable ones online, but verify the source).
Step-by-Step
- Locate the hosts file. The path is the same on all systems:
/etc/hosts. On Windows, it's atC:\Windows\System32\drivers\etc\hosts. On macOS and Linux, it's/etc/hosts. - Open the file with admin rights. On Windows: right-click Notepad and select "Run as administrator," then open the hosts file from within Notepad (you'll need to change the file filter to "All Files"). On macOS/Linux: open a terminal and type
sudo nano /etc/hosts(or usesudoedit). - Understand the format. Each line is an IP address, a space or tab, then the domain name. Lines starting with
#are comments. Example:127.0.0.1 localhost. - Add your entries. To block a site, map it to
0.0.0.0or127.0.0.1. For example, to blockads.example.com, add:0.0.0.0 ads.example.com. To speed up a frequently visited site, you can map it to its actual IP address (find it withping example.comin a terminal), but only do this if you're sure the IP is stable. - Save the file. On Windows, use File > Save. If you get a permission error, you didn't open Notepad as administrator. On macOS/Linux, in nano, press Ctrl+O to save, then Ctrl+X to exit.
- Clear your DNS cache. So the changes take effect immediately: Windows:
ipconfig /flushdnsin Command Prompt. macOS:sudo dscacheutil -flushcache; sudo killall -HUP mDNSResponder. Linux:sudo systemctl restart nscd(orsudo systemd-resolve --flush-cacheson systemd systems). - Test it. Open your browser and try the domain you changed. If you blocked it, you should get a connection error or a blank page. If you redirected it, you should see the new site.
Common Mistakes
- Using the wrong IP. Blocking with
0.0.0.0is safer than127.0.0.1because some services treat localhost as valid. For blocking, use0.0.0.0. - Forgetting the trailing dot. Some systems require a trailing dot for fully qualified domain names (e.g.,
example.com.). It doesn't hurt to add it. - Editing without admin rights. You'll get a "permission denied" error. Always run your editor as administrator or use sudo.
- Adding too many entries. A huge hosts file can slow down your system because every DNS lookup checks the file. Keep it under a few thousand lines.
- Mistyping a domain. A typo can block a legitimate site or redirect you to the wrong place. Double-check each line.
Tips
- Back up the original file. Before editing, copy the hosts file to
hosts.backupso you can restore it if something goes wrong. - Use comments to organize. Add
# Blocked adsabove your entries to keep it readable. - For testing, use a local server. Map a domain to
127.0.0.1and run a local web server to test your site before deploying. - Consider a hosts-file manager. Tools like SwitchHosts (free, open-source) let you toggle between different hosts configurations without manual editing.
- On mobile, it's different. iOS and Android don't let you edit the hosts file without rooting/jailbreaking, which we don't recommend. Instead, use a DNS-based blocker like NextDNS or a browser extension.
Troubleshooting
If your changes don't take effect:
- Check the file saved correctly. Reopen it and verify the entries are there and not commented out.
- Clear your DNS cache again. Sometimes the cache doesn't flush on the first try. Wait a minute and repeat the flush command.
- Restart your browser. Browsers may cache DNS results. Close and reopen it, or try a different browser.
- Check for a proxy or VPN. If you're using a VPN or proxy, it may bypass the hosts file. Disconnect and test again.
- Verify the IP is correct. For a redirect, ping the domain to confirm the IP you used is still valid. If it changed, update the hosts file.
- If nothing works, restore the backup. Copy your backup back to the original location, then re-edit carefully.
Frequently Asked Questions
Can the hosts file block all ads?
No, it only blocks domains you list. Many ads come from a huge number of domains, so you'd need a very large list. It also can't block ads served from the same domain as the content (e.g., YouTube ads).
Will editing the hosts file violate any terms of service?
It's your system file, so it's not illegal, but some services (like streaming platforms) may detect it and refuse to work if you block their domains. Use it at your own discretion.
Does the hosts file work on all operating systems?
Yes, Windows, macOS, and Linux all read the hosts file. The location is the same (/etc/hosts), but the editing process differs slightly.
Can I use the hosts file to speed up my internet?
Yes, for sites you visit frequently, mapping them to their IP bypasses DNS lookup, which can save a few milliseconds. But the effect is minimal for most users. The bigger benefit is blocking trackers and ads, which reduces data usage and page load time.
What happens if I map a domain to the wrong IP?
You'll get a connection error or be sent to the wrong site. If you're not sure of the IP, use a DNS lookup tool like nslookup example.com to find it.
How do I undo my changes?
Simply remove the lines you added and save the file, or restore your backup. Then clear your DNS cache again.