Hosts File Location and Editing Guide for Windows, macOS, and Linux

The hosts file is one of the oldest and most useful network configuration files on any operating system. It maps hostnames to IP addresses before your system even contacts a DNS server. For developers, this means you can point myapp.local to 127.0.0.1, block a domain entirely, or test a site on a new server before DNS changes go live. All without affecting any other machine on your network.
This guide covers the exact file location for Windows, macOS, and Linux, how to edit it on each platform, the correct syntax, and the most common reasons your changes might not take effect.
What Is the Hosts File and Why It Matters#
When your computer resolves a hostname like example.com to an IP address, it does not start with a DNS server. It first checks the local hosts file. If it finds a matching entry, it uses that IP address immediately and skips the DNS lookup entirely.
This resolution order is the same across all major operating systems:
- Browser or application cache
- Operating system DNS cache
- Local hosts file
- DNS server
The hosts file takes precedence over public DNS. That is what makes it powerful for developers. You can map any domain to any IP address, but only on the machine where you make the change. Other devices on your network, or other users on the same machine, will not see the override unless they edit their own hosts file.
Common developer use cases include:
- Local development: Map a custom domain like
myapp.testto127.0.0.1so your local server responds to a real-looking URL. - Testing before DNS propagation: Point a domain to a staging server while the production DNS record is still pointing elsewhere.
- Blocking domains: Redirect ad servers or tracking domains to
127.0.0.1or0.0.0.0to prevent requests from leaving your machine. - Internal servers: Give friendly names to machines on your local network without setting up a DNS server.
Windows Hosts File Location and Editing#
On Windows 11 and Windows 10, the hosts file is located at:
C:\Windows\System32\drivers\etc\hosts
The file has no extension. It is simply named hosts, not hosts.txt. This matters when opening or saving it in a text editor, because Windows may otherwise create a new text file instead of modifying the real one.
Editing with Notepad as Administrator
The standard approach on Windows is to open Notepad with elevated privileges and then open the hosts file from the etc folder.
- Open the Start menu and search for
Notepad. - Right-click Notepad and select Run as administrator.
- In Notepad, go to File > Open.
- Navigate to
C:\Windows\System32\drivers\etc. - Change the file type dropdown from Text Documents (*.txt) to All Files (.).
- Select
hostsand click Open. - Add your entries at the bottom of the file.
- Save the file and close Notepad.
After saving, flush the DNS cache so Windows picks up the changes immediately:
ipconfig /flushdns
Editing with PowerShell
You can also open the hosts file directly from PowerShell by running:
notepad C:\Windows\System32\drivers\etc\hosts
This launches Notepad but may not automatically elevate it to administrator. If you get a permission error when saving, close Notepad and reopen it using the Run as administrator method described above.
Editing with VS Code
Visual Studio Code can edit the hosts file, but it must be launched with administrator privileges. Right-click the VS Code shortcut and select Run as administrator, then open the file from C:\Windows\System32\drivers\etc\hosts.
Common Windows Pitfalls
- Do not use
C:\Windows\SysWOW64. The hosts file belongs underSystem32, even on 64-bit Windows. - If the hosts file is missing entirely, you can recreate it. Open Notepad as administrator, add your entries, and save it to
C:\Windows\System32\drivers\etc\hostsusing the All Files filter. - Windows Defender or third-party security tools sometimes monitor the hosts file and may revert changes. Check your security software if your edits disappear.
macOS Hosts File Location and Editing#
On macOS, the hosts file is located at:
/etc/hosts
This path is a symbolic link that points to /private/etc/hosts. Both paths refer to the same file. You can use either one in Terminal.
Editing with sudo nano
The most reliable method on macOS is to edit the file from Terminal using the nano text editor with root privileges.
- Open Terminal (search for it in Spotlight or find it in Go > Utilities).
- Run the following command:
sudo nano /etc/hosts
- Enter your administrator password when prompted.
- Use the arrow keys to navigate to the bottom of the file.
- Add your entries in the format
IP_ADDRESS hostname. - Press
Ctrl + Oto save, thenEnterto confirm, thenCtrl + Xto exit.
Flushing DNS on macOS
After editing the hosts file on macOS, flush the DNS cache with:
sudo dscacheutil -flushcache && sudo killall -HUP mDNSResponder
This command works on macOS Ventura, Sonoma, and Sequoia. Apple has changed the DNS flush command across versions, but this combined command covers all recent releases.
macOS-Specific Tips
On macOS Sequoia, some users report that the hosts file may have file flags set that prevent editing. If you see "Operation not permitted" when saving, run:
sudo chflags nouchg,noschg /etc/hosts
Then try the nano edit again.
The default macOS hosts file looks like this:
##
## Host Database
#
## localhost is used to configure the loopback interface
## when the system is booting. Do not change this entry.
##
127.0.0.1 localhost
255.255.255.255 broadcasthost
::1 localhost
Do not remove these base entries unless you have a specific reason to.
Linux Hosts File Location and Editing#
On Linux, the hosts file is located at:
/etc/hosts
This is the same path on Ubuntu, Debian, Fedora, Arch, and virtually every other distribution. The file is owned by root and has permissions 644 (readable by all, writable only by root).
Editing with sudo nano
Open Terminal and run:
sudo nano /etc/hosts
Add your entries at the bottom of the file. Save with Ctrl + O, confirm with Enter, and exit with Ctrl + X.
Editing with vim
If you prefer vim:
sudo vim /etc/hosts
Press i to enter insert mode, add your entries, press Esc, then type :wq and press Enter to save and quit.
Ubuntu-Specific Considerations
Ubuntu uses systemd-resolved as its DNS resolver by default. This introduces a layer that can cause confusion: your hosts file edits are correct, but systemd-resolved may still serve cached or stale results.
After editing /etc/hosts on Ubuntu, flush the resolver cache with:
sudo resolvectl flush-caches
You can verify that the cache is empty by running:
resolvectl statistics
The default Ubuntu hosts file includes a mapping for the machine hostname to 127.0.1.1 rather than 127.0.0.1. This is intentional. Ubuntu uses 127.0.1.1 to separate the hostname from the loopback address so that applications do not confuse the two. Do not change or remove this line unless you understand the implications.
A typical Ubuntu 24.04 hosts file:
127.0.0.1 localhost
127.0.1.1 ubuntu-desktop
## The following lines are desirable for IPv6 capable hosts
::1 ip6-localhost ip6-loopback
ff02::1 ip6-allnodes
ff02::2 ip6-allrouters
Other Linux Distributions
On distributions using nscd (Name Service Cache Daemon) instead of systemd-resolved, flush the cache with:
sudo nscd --invalidate=hosts
On distributions with no DNS caching service, hosts file changes take effect immediately.
Hosts File Syntax and Format#
The hosts file uses a simple, plain-text format. Each line contains an IP address followed by one or more hostnames separated by spaces or tabs.
Basic Entry Format
IP_ADDRESS hostname [alias1] [alias2]
Here are the rules:
- IPv4 entries use standard dotted notation:
127.0.0.1 example.com - IPv6 entries use colon notation:
::1 localhost - Comment lines start with
#and are ignored by the system. - Blank lines are ignored and can be used to separate groups of entries.
- Multiple hostnames can appear on one line:
192.168.1.10 myserver myserver.local
Example Entries
## Local development
127.0.0.1 myapp.test
127.0.0.1 api.myapp.test
## Block tracking domains
0.0.0.0 analytics.example.com
0.0.0.0 tracking.example.com
## Internal server
192.168.1.50 nas.local nas
When blocking domains, you can use either 127.0.0.1 or 0.0.0.0. Using 0.0.0.0 is slightly faster because the connection attempt fails immediately rather than connecting to the local machine and then being rejected.
Practical Use Cases for Developers#
Local Development with Custom Domains
Instead of accessing your local server at http://localhost:3000, you can map a domain to it:
127.0.0.1 myapp.test
Then visit http://myapp.test in your browser. This is useful for testing features that depend on the Host header, such as cookie scoping or multi-tenant applications.
Testing Before DNS Changes Go Live
If you are migrating a site to a new hosting provider, you can point the domain to the new server on your machine while the rest of the world still sees the old IP:
203.0.113.50 example.com www.example.com
This lets you verify the site works on the new server before updating the actual DNS record.
Blocking Domains
You can block access to specific domains by mapping them to 127.0.0.1:
127.0.0.1 ads.example.com
127.0.0.1 tracker.example.com
This is a lightweight alternative to a DNS-based ad blocker. It works at the system level, so every application on your machine will be affected.
Overriding DNS for Internal Servers
If you have servers on your local network, you can give them friendly names without running a DNS server:
192.168.1.20 devserver
192.168.1.21 staging
192.168.1.30 nas.local
Common Issues and Troubleshooting#
Changes Not Taking Effect
The most common reason hosts file changes do not work is that the DNS cache has not been flushed. After editing the hosts file, always flush the cache for your operating system:
| Operating System | Command |
|---|---|
| Windows | ipconfig /flushdns |
| macOS | sudo dscacheutil -flushcache && sudo killall -HUP mDNSResponder |
| Ubuntu | sudo resolvectl flush-caches |
| Other Linux (nscd) | sudo nscd --invalidate=hosts |
Browser DNS Over HTTPS Bypassing the Hosts File
Modern browsers may use DNS over HTTPS (DoH) or DNS over TLS (DoT), which bypass the hosts file entirely. If your changes are not working in Chrome, Edge, or Firefox, check the browser's DNS settings:
- Chrome: Go to
chrome://settings/securityand disable Use secure DNS. - Firefox: Go to
about:preferences#privacy, scroll to DNS over HTTPS, and set it to Off or Default.
After disabling DoH, restart the browser.
Permission Errors
On Windows, you must run your text editor as administrator. On macOS and Linux, you must use sudo. If you try to save the hosts file without elevated privileges, the save will fail silently or show an error.
Verifying Your Changes
Use ping or nslookup to confirm that the hosts file entry is being used:
ping myapp.test
nslookup myapp.test
If ping resolves to the IP address you specified in the hosts file, the entry is working. If it resolves to a different IP, the DNS cache has not been flushed or a browser DoH setting is overriding the hosts file.
Resetting the Hosts File to Default#
If your hosts file becomes corrupted or you want to start fresh, you can restore the default content for your operating system.
Windows Default Hosts File
## Copyright (c) 1993-2009 Microsoft Corp.
#
## This is a sample HOSTS file used by Microsoft TCP/IP for Windows.
#
## This file contains the mappings of IP addresses to hostnames. Each
## entry should be kept on an individual line. The IP address should
## be placed in the first column followed by the corresponding host name.
## The IP address and the host name should be separated by at least one
## space.
#
## Additionally, comments (such as these) may be inserted on individual
## lines or following the machine name denoted by a '#' symbol.
#
## For example:
#
## 102.54.94.97 rhino.acme.com # source server
## 38.25.63.10 x.acme.com # x client host
## localhost name resolution is handled within DNS itself.
# 127.0.0.1 localhost
# ::1 localhost
macOS Default Hosts File
##
## Host Database
#
## localhost is used to configure the loopback interface
## when the system is booting. Do not change this entry.
##
127.0.0.1 localhost
255.255.255.255 broadcasthost
::1 localhost
Linux Default Hosts File
127.0.0.1 localhost
127.0.1.1 <hostname>
## The following lines are desirable for IPv6 capable hosts
::1 ip6-localhost ip6-loopback
ff02::1 ip6-allnodes
ff02::2 ip6-allrouters
On Linux, replace <hostname> with your machine's actual hostname. You can find it by running hostname in Terminal.
Before making any changes, always back up the current hosts file:
## Windows (in PowerShell as admin)
Copy-Item C:\Windows\System32\drivers\etc\hosts C:\Windows\System32\drivers\etc\hosts.backup
## macOS or Linux
sudo cp /etc/hosts /etc/hosts.backup
Summary#
The hosts file gives you direct control over hostname resolution on your local machine. It is the first place your operating system looks when translating a domain name to an IP address, which makes it a reliable tool for local development, DNS testing, and domain blocking.
The key steps are straightforward: find the hosts file for your operating system, open it with administrator or root privileges, add your entries, save the file, and flush the DNS cache. If your changes do not take effect, check your DNS cache, browser DoH settings, and file permissions.
Keep a backup of the default hosts file content for your operating system. That way, you can always restore it if something goes wrong.
Information last checked on 2026-07-18.