How to Edit Your Hosts File on Linux, Windows, and macOS

By 

Updated on

7 min read

How to Edit Your Hosts File on Linux, Windows, and Mac

The hosts file is used to map domain names (hostnames) to IP addresses. It is a plain-text file used by all operating systems including Linux, Windows, and macOS.

The hosts file has priority over DNS. When you type in the domain name of a website you want to visit, the operating system first checks its hosts file for the corresponding domain. If there is no matching entry, it queries the configured DNS servers to resolve the name. This affects only the computer on which the change is made, not how the domain is resolved worldwide.

In this article, we will provide instructions about how to modify the hosts file on Linux, macOS, and Windows.

Quick Reference

TaskValue
Hosts file path (Linux / macOS)/etc/hosts
Hosts file path (Windows)C:\Windows\System32\drivers\etc\hosts
Open on Linux / macOSsudo nano /etc/hosts
Flush DNS cache (Linux, systemd-resolved)sudo resolvectl flush-caches
Flush DNS cache (macOS)sudo dscacheutil -flushcache && sudo killall -HUP mDNSResponder
Entry formatIP_ADDRESS hostname [aliases]
Block a website0.0.0.0 ads.example.com
Comment a line# This is a comment

Hosts File Format

Entries in the hosts file have the following format:

txt
IPAddress DomainName [DomainAliases]

The IP address and the domain name must be separated by at least one space or tab. A line can include one or more space-separated aliases after the domain name. Lines starting with # are comments and are ignored.

A sample hosts file looks like this:

txt
# Static table lookup for hostnames.
# See hosts(5) for details.

127.0.0.1   localhost
127.0.1.1   linuxize.desktop linuxize

# Custom entries
192.168.1.10   server.example.com server
::1             localhost ip6-localhost ip6-loopback

Key points about the format:

  • IPv4 and IPv6: the file supports both. Use ::1 for the IPv6 loopback address.
  • Multiple aliases: one IP address can have multiple hostnames on the same line: 192.168.1.10 server server.local dev.
  • Blocking: map a domain to 0.0.0.0 to block it: 0.0.0.0 ads.example.com. Using 0.0.0.0 is faster than 127.0.0.1 for blocking because the connection is rejected immediately rather than routed to the local machine.

The hosts file changes take effect immediately for new connections, except where DNS entries are cached by the browser or another application.

To undo any change, open the file and remove the lines you added.

Common Use Cases

Testing a website before DNS propagation When migrating a website to a new server, you can point the domain to the new server’s IP in your local hosts file to verify it works before updating the public DNS records. Only your machine uses the new IP; everyone else still sees the old server.

Local development Map a friendly local domain to 127.0.0.1 so your development environment is accessible at a readable address:

txt
127.0.0.1   myapp.local
127.0.0.1   api.myapp.local

Blocking websites Map unwanted domains to 0.0.0.0 to prevent connections entirely:

txt
0.0.0.0   ads.example.com
0.0.0.0   tracker.example.com

Overriding DNS for a specific host Force a hostname to resolve to a specific IP regardless of what DNS returns, useful for internal servers, testing load balancers, or working around DNS propagation delays.

Modify Hosts File in Linux

On Linux, the full path to the file is /etc/hosts.

The instructions below apply to all Linux distributions, including Ubuntu, Debian, Fedora, RHEL, and Linux Mint.

  1. Open the hosts file in your terminal using your preferred text editor :

    Terminal
    sudo nano /etc/hosts

    Enter your sudo password when prompted.

  2. Scroll to the end of the file and add your new entries. For example, to point server.example.com to 192.168.1.10:

    txt
    192.168.1.10   server.example.com
  3. Save the file and exit (Ctrl+O, then Ctrl+X in nano).

  4. If your system uses systemd-resolved (Ubuntu 18.04 and later), flush the DNS cache to apply changes immediately:

    Terminal
    sudo resolvectl flush-caches

    On older systems using nscd, restart the service instead:

    Terminal
    sudo systemctl restart nscd

Modify Hosts File in Windows

On Windows, the full path to the file is C:\Windows\System32\drivers\etc\hosts. The instructions below apply to Windows 10 and Windows 11.

  1. Press the Windows key, type Notepad, right-click the result, and select Run as administrator.

  2. In Notepad, click FileOpen.

  3. In the File name field, paste the following path and click Open:

    txt
    C:\Windows\System32\drivers\etc\hosts
  4. Scroll to the end of the file and add your new entry. For example:

    txt
    192.168.1.10   server.example.com
  5. Save the file with FileSave.

Windows does not require a separate DNS flush step; changes take effect for new connections immediately. If your browser has cached the old IP, close and reopen it.

Modify Hosts File in macOS

On macOS, the full path to the file is /etc/hosts. The instructions below apply to all recent macOS versions.

  1. Open the hosts file in Terminal:

    Terminal
    sudo nano /etc/hosts

    Enter your administrator password when prompted.

  2. Scroll to the end of the file and add your new entry:

    txt
    192.168.1.10   server.example.com
  3. Save the file (Ctrl+O, then Ctrl+X).

  4. Flush the DNS cache so the change takes effect immediately:

    Terminal
    sudo dscacheutil -flushcache
    sudo killall -HUP mDNSResponder

Verify the Hosts File Change

After editing the hosts file, verify that the hostname resolves to the IP address you entered. On Linux, you can use:

Terminal
getent hosts server.example.com

If the entry is active, the command prints the IP address followed by the hostname.

On Linux or macOS, you can also test with:

Terminal
ping -c 1 server.example.com

If the hostname still resolves to the old address, flush the DNS cache and restart the browser or application you are testing. Some applications cache DNS independently of the operating system.

Troubleshooting

Changes are not taking effect
The most common cause is a cached DNS response in the browser or OS. Flush the DNS cache using the commands in the relevant section above, then close and reopen your browser. Some applications maintain their own DNS cache and may require a restart.

“Permission denied” when saving the file
The hosts file requires administrator privileges to edit. On Linux and macOS, always open it with sudo. On Windows, make sure Notepad is running as administrator before opening the file.

The domain still resolves to the old IP in the browser
Browsers cache DNS independently of the OS. After editing the hosts file and flushing the OS cache, open a new browser window or clear the browser’s DNS cache. In Chrome, go to chrome://net-internals/#dns and click Clear host cache.

IPv6 connections bypass the hosts file entry
If you add an IPv4 entry but the application prefers IPv6, it may still connect via the original DNS. Add a matching IPv6 entry using ::1 or the target IPv6 address on a separate line with the same hostname.

FAQ

Does editing the hosts file affect other users on the network?
No. The hosts file only affects the computer on which it is edited. Other devices on the network continue to use DNS as normal.

What is the difference between 127.0.0.1 and 0.0.0.0 for blocking?
Both work, but 0.0.0.0 is generally preferred for blocking. Mapping to 127.0.0.1 routes the request to your local machine’s web server (which may or may not be running), while 0.0.0.0 causes the connection to fail immediately, which is faster and avoids any unintended local server responses.

How do I undo a hosts file change?
Open the file with sudo nano /etc/hosts (Linux/macOS) or Notepad as administrator (Windows), delete the lines you added, save the file, and flush the DNS cache.

Do I need to restart the computer after editing the hosts file?
No. Changes take effect immediately for new connections. You only need to flush the DNS cache if the old result is cached, and restart the browser if it has cached the DNS response independently.

Can I add multiple domains on the same line?
Yes. You can add multiple aliases (space-separated) after the primary hostname on the same line: 192.168.1.10 server server.local dev.example.com. All aliases resolve to the same IP address.

Conclusion

The hosts file is the fastest way to override DNS resolution on your local machine, useful for website testing, local development, and blocking unwanted domains. On Linux and macOS the file lives at /etc/hosts; on Windows at C:\Windows\System32\drivers\etc\hosts. After editing, flush the DNS cache to ensure changes take effect immediately.

Tags

Linuxize Weekly Newsletter

A quick weekly roundup of new tutorials, news, and tips.

About the authors

Dejan Panovski

Dejan Panovski

Dejan Panovski is the founder of Linuxize, an RHCSA-certified Linux system administrator and DevOps engineer based in Skopje, Macedonia. Author of 800+ Linux tutorials with 20+ years of experience turning complex Linux tasks into clear, reliable guides.

View author page