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.

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.

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.

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.