How to Edit the Hosts File (Windows, Mac, Linux)

Follow these steps in order to safely edit your hosts file.

  1. Understand What the Hosts File Does

    The hosts file is a plain text file that maps domain names directly to IP addresses, and it is the very first place your operating system checks before ever querying a DNS server. Adding an entry lets you skip DNS lookup entirely and connect straight to a chosen IP for that domain β€” useful for local development setups or blocking access to specific sites.

  2. Find the Hosts File for Your OS

    On Windows it lives at "C:\Windows\System32\drivers\etc\hosts". On Mac and Linux it is at "/etc/hosts". It has no file extension and is plain text, so it can be opened directly with Notepad or a terminal editor.

  3. Open Your Editor with Administrator Rights

    The hosts file is a protected system file, so it cannot be saved with regular user permissions. On Windows, run Notepad "as administrator" before opening the file; on Mac and Linux, open it from the terminal with an elevated command like "sudo nano /etc/hosts" so the save actually goes through.

  4. Add Entries in the Correct Format

    Each line follows the format "IP address [space] domain name". For example, typing "127.0.0.1 myproject.local" routes any request to myproject.local back to your own machine (127.0.0.1). A line starting with "#" is treated as a comment and ignored, so be careful not to accidentally delete the default entries already in the file.

  5. Save and Verify the Change

    After saving, open a browser and visit the domain to confirm it resolves to the IP you specified. Browsers and operating systems sometimes cache older DNS results, so if the change does not take effect immediately, try restarting your browser or flushing your DNS cache and testing again.

  6. Use Case 1: Local Development Domain Mapping

    If you want to test a website you are building under an address that resembles its real production domain, registering something like "127.0.0.1 myapp.test" lets you type a realistic-looking URL in your browser while it actually connects to your local server.

  7. Use Case 2: Blocking Specific Sites

    Registering a domain as "0.0.0.0 domain.com" or "127.0.0.1 domain.com" makes connections to that site fail. This is sometimes used to block large lists of ad-serving domains, though once the list grows large it becomes cumbersome to manage, so many people pair it with a dedicated ad-blocking tool instead.

  8. Caution: Malware Can Tamper with This File

    Some malware secretly modifies the hosts file to redirect banking or major website domains to a fake IP address for phishing purposes. If a particular site keeps behaving strangely or your browser throws unexpected security warnings, checking the hosts file for unfamiliar entries is a good first troubleshooting step.

  9. Flush the DNS Cache on Each System

    On Windows, open a Command Prompt and run "ipconfig /flushdns". On macOS, the commonly used command is "sudo dscacheutil -flushcache; sudo killall -HUP mDNSResponder", though it can differ between versions. On Linux, if your system uses systemd-resolved, "resolvectl flush-caches" clears it, and distributions without a local cache need no flush at all. Browsers also keep their own short-lived caches, so closing the browser fully is a reliable extra step.

  10. Check the Result With the Right Tool

    Use "ping myproject.local" to confirm that the name resolves to the IP you wrote, because ping goes through the system resolver, which reads the hosts file. Tools like "nslookup" and "dig" query DNS servers directly and skip the hosts file by default, so they will show the public record and make it look as though your edit did nothing. Knowing which tool reads the file avoids a lot of false alarms.

  11. Add Both IPv4 and IPv6 When Needed

    If a site also has an IPv6 address, your computer may reach it through that route even when you only edited the IPv4 line. For a complete block or redirect, add a matching line with an IPv6 address, for example "::1 domain.com" for the local machine. The default file already contains a line for "localhost" in both formats, which should stay.

  12. Several Names on One Line, and What Hosts Cannot Do

    You can put multiple host names after the same IP, separated by spaces, such as "127.0.0.1 app.test api.test". The hosts file has limits: it does not support wildcards such as "*.example.com", so each subdomain needs its own line. It also maps names only to IP addresses, not to port numbers, so a local site on port 3000 is still reached as "http://app.test:3000". For wildcards or per-port routing, a local DNS tool or a reverse proxy is the better fit.

  13. Back Up and Comment Your Changes

    Copy the original file to a backup name such as "hosts.bak" before the first edit. Put a comment line starting with "#" above each entry you add, noting what it is for and the date. A file that has been edited by several people or tools over the years becomes difficult to clean up without these notes.

Common mistakes when editing the hosts file

The most frequent issue is saving the file without administrator/root privileges, which silently fails on some editors or throws a permissions error on others β€” always confirm your editor was launched with elevated rights first. Another common mistake is leaving a stray typo or extra domain on an existing line, which can accidentally block or redirect a site you never intended to touch, so it is worth reviewing the whole file rather than just the line you added. Writing the domain name before the IP address, or running the two together without a space, will make the line be ignored.

Reverting your changes

If a new entry causes unexpected problems, you can either delete the specific line you added or, on most systems, restore the default hosts file content (a handful of loopback and localhost entries) and save. Keeping a quick backup copy of the original file before your first edit makes it easy to revert cleanly if something goes wrong.

When the hosts file is the wrong tool

The hosts file affects only the computer it sits on, so it will not help when you want the whole home network to behave the same way; a router with local DNS records or a network-wide DNS filter handles that. It also becomes hard to manage once you have more than a few dozen entries. And if you want to see how a site would behave after migration to a new server, the hosts file is an ideal temporary tool: point the real domain to the new server's IP on your computer only, test, and remove the line. Remember to delete such lines afterwards, since an old entry quietly keeps sending you to a server that may later be turned off.

Security notes

Because the file overrides DNS, anyone who can edit it can silently redirect your traffic. Keep the default permissions, avoid running unknown scripts with administrator rights, and be wary of tutorials that tell you to paste long blocks of entries from unknown sources. Security software on some systems also alerts you when the file changes, which is a useful signal if you did not make the change yourself.

Frequently Asked Questions

Why does editing the hosts file not seem to have any effect?

The most common cause is not opening the editor with administrator or root privileges, so the save silently fails. Also check that your browser or OS is not still using a cached DNS result from before the change β€” restarting the browser or flushing the DNS cache usually resolves this.

Is it safe to use the hosts file to block ads?

It is a legitimate and widely used technique, but maintaining a large list of ad-domain entries by hand becomes tedious over time and can occasionally break unrelated services that share a domain. For heavier ad-blocking needs, a dedicated ad-blocking extension or DNS-based blocker is usually easier to maintain.

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

Usually not. Changes are read on the next lookup, so closing and reopening the browser and flushing the DNS cache are normally enough.

Can I use the hosts file on a phone?

Not in the usual way. Phones lock the system file by default, and editing it requires rooting or jailbreaking, which carries security risks. A DNS-based blocking app or a local DNS setting is the safer route.

Why does the site still open after I blocked it?

Check for a cached lookup, an extra IPv6 address for the same site, a browser using its own secure DNS setting, or a typo in the domain name. Also confirm the file saved by opening it again.