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

By 

•

Updated on

•

10 min read

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

Say you have moved a website to a new server and want to check that everything works before you switch the public DNS records. Or you want a short, readable name for a machine on your local network. In both cases, you can tell your computer where a domain name points without touching DNS at all, by adding a line to the hosts file.

The hosts file maps domain names (hostnames) to IP addresses. It is a plain-text file used by all major 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

The first line is the one every system ships with: it maps localhost to the IPv4 loopback address so that programs can reach the machine by name. The 127.0.1.1 line, found on Debian and Ubuntu desktop installs, maps the machine’s own hostname. When you change the hostname , update this line as well.

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. This is the usual convention in blocklists. On Linux, a connection to 0.0.0.0 still reaches the local machine, so if a web server on your computer listens on all interfaces, it will answer the blocked request.

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.

Hosts File and DNS Lookup Order

The hosts file is checked before DNS because the system is configured that way, not because of anything in the file itself. On Linux, the lookup order is set by the hosts: line in /etc/nsswitch.conf. To see it, run:

Terminal
grep ^hosts /etc/nsswitch.conf

On Ubuntu 26.04, the output looks like this:

output
hosts:          files mdns4_minimal [NOTFOUND=return] dns

The sources are tried from left to right. files is the /etc/hosts file, mdns4_minimal handles .local names through multicast DNS, and dns sends the query to the configured DNS servers. Because files comes first, a successful lookup in /etc/hosts stops the search before DNS.

IPv4 and IPv6 behavior depends on the resolver and how the application requests addresses. With glibc using files followed by direct DNS queries, an IPv6-only lookup can continue to DNS if the hosts file contains only an IPv4 entry. When systemd-resolved handles the lookup with hosts file support enabled, it answers locally for names in /etc/hosts without querying upstream DNS for their addresses. Do not assume that an IPv4 entry overrides IPv6 resolution on every system; add the intended IPv6 address on a separate line if you need to override both.

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.test
127.0.0.1   api.myapp.test

The .test top-level domain is reserved for this kind of use and will never be registered publicly. Avoid .local, which is reserved for multicast DNS and can cause slow lookups, especially on macOS.

Blocking websites Map unwanted domains to 0.0.0.0 to redirect them away from their real servers:

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: press Ctrl+O, press Enter to confirm the filename, then press Ctrl+X in nano.

  4. The change takes effect as soon as the file is saved. On systems that use systemd-resolved (Ubuntu 18.04 and later), the resolver rereads /etc/hosts on its own. If an old answer is still cached, flush the DNS cache:

    Terminal
    sudo resolvectl flush-caches

    On older systems using nscd, restart the service instead:

    Terminal
    sudo systemctl restart nscd

If you only need to add a single entry, you do not have to open an editor at all. Pipe the line to the tee command with the -a option, which appends it to the end of the file:

Terminal
echo "192.168.1.10 server.example.com" | sudo tee -a /etc/hosts

Make sure to use -a. Without it, tee overwrites the whole file and removes the existing entries, including localhost.

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 File → Open.

  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 File → Save.

Windows picks up the change automatically, so a separate DNS flush step is usually not needed. If the old IP address sticks, open Command Prompt as administrator and run ipconfig /flushdns. 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 and exit: press Ctrl+O, press Enter to confirm the filename, then press 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, check the IPv4 entry from the example with:

Terminal
getent ahostsv4 server.example.com

For the example entry, the output looks like this:

output
192.168.1.10     STREAM server.example.com
192.168.1.10     DGRAM
192.168.1.10     RAW

Each row shows the same IP address for a different socket type. The getent ahostsv4 command uses the system’s configured name resolution sources and requests IPv4 addresses, so you can check that the result matches your entry. Plain getent hosts tries IPv6 first and can return a DNS answer on systems where the IPv4 entry does not suppress IPv6 lookups.

On Linux or macOS, you can also test with:

Terminal
ping -c 1 server.example.com

The first line of the output shows the IP address that the hostname resolved to. To compare your local override with public DNS, use dig with an explicit external resolver. Replace example.com with the public domain you are testing:

Terminal
dig @1.1.1.1 example.com A

The @1.1.1.1 argument sends the query to Cloudflare’s public resolver, and A requests IPv4 records. You can also specify a DNS server with nslookup . Without an explicit server, these tools use the configured resolver, which may be the local systemd-resolved stub and return your hosts file override.

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.

“E212: Can’t open file for writing” in Vim
You opened /etc/hosts in Vim without sudo, so Vim cannot save it. To save without losing your edits, type :w !sudo tee %, enter your password, and then quit with :q!. Next time, open the file with sudo vim /etc/hosts. See how to save a file in Vim for more saving options.

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, and 0.0.0.0 is the more common choice in blocklists because it is not a real destination address. Neither one is guaranteed to fail: on Linux, a connection to either address reaches the local machine, so a local web server listening on all interfaces will answer. If nothing is listening, the connection is refused right away in both cases.

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

Remove temporary hosts file entries after testing a migration so your computer follows future DNS changes. If an application still uses the old address, clear its cached result before checking again.

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 1000+ Linux tutorials with 20+ years of experience turning complex Linux tasks into clear, reliable guides.

View author page