Where to Find the Hosts File on Windows, Mac, and Linux
The hosts file is one of the oldest networking files still in active use on modern operating systems. It predates the Domain Name System (DNS) and continues to play a role in how your computer resolves domain names to IP addresses. Knowing where it lives — and what it does — is useful for developers, network administrators, and anyone troubleshooting connectivity or managing local environments.
What the Hosts File Actually Does
Before your computer contacts a DNS server to translate a domain name (like example.com) into an IP address, it checks the local hosts file first. If a matching entry exists, that mapping is used directly — no DNS query needed.
This makes the hosts file a powerful tool for:
- Blocking domains by redirecting them to a non-routable address
- Testing websites locally before they go live on a real server
- Overriding DNS for specific domains during development or troubleshooting
- Redirecting traffic on a per-device basis without touching router settings
Because the hosts file is checked before DNS, any entry you add takes immediate effect on that machine.
Where to Find the Hosts File by Operating System 🖥️
The file location varies depending on your OS. Here's where to look:
| Operating System | Hosts File Location |
|---|---|
| Windows 10 / 11 | C:WindowsSystem32driversetchosts |
| Windows 7 / 8 | C:WindowsSystem32driversetchosts |
| macOS (all modern versions) | /etc/hosts |
| Linux (most distributions) | /etc/hosts |
| Android (rooted devices) | /system/etc/hosts |
| iOS (jailbroken devices) | /etc/hosts |
On Windows, the etc folder is hidden within the system directory and has no file extension — it's simply named hosts. On macOS and Linux, the path /etc/hosts is standard and consistent across nearly all distributions.
How to Open the Hosts File
On Windows
You cannot simply double-click the file. Because it sits inside a protected system directory, you need elevated permissions:
- Search for Notepad in the Start menu
- Right-click and select Run as administrator
- Use File → Open and navigate to
C:WindowsSystem32driversetc - Change the file filter from
.txtto All Files to makehostsvisible - Open the file
Alternatively, you can open Command Prompt as administrator and use a text editor from the command line (e.g., notepad C:WindowsSystem32driversetchosts).
On macOS
The hosts file on macOS requires sudo (superuser) access to edit, but you can view it without elevated privileges:
- Open Terminal and type:
cat /etc/hoststo view contents - To edit:
sudo nano /etc/hosts— this opens the file in the nano editor with write access
On Linux
The process is nearly identical to macOS:
- View:
cat /etc/hosts - Edit:
sudo nano /etc/hostsor use your preferred text editor with sudo
Some Linux desktop environments also let you open a file manager with elevated permissions, but using the terminal is the most reliable method.
What's Already in the Hosts File
On a fresh system, the hosts file contains a few default entries. You'll typically see:
127.0.0.1 localhost ::1 localhost These map the hostname localhost to the loopback address — the address your machine uses to refer to itself. This is foundational for local development environments. Everything else in the file is either added by the OS, security software, or manually by a user or administrator.
Variables That Affect How the Hosts File Behaves 🔧
Not every situation is the same. Several factors influence how effectively you can use or locate the hosts file:
Operating system version and permissions model — Windows, macOS, and Linux all handle file system permissions differently. On Windows, User Account Control (UAC) actively blocks edits without admin rights. On macOS, System Integrity Protection (SIP) doesn't directly restrict the hosts file, but sudo is still required for editing.
Security software — Some antivirus or endpoint protection tools monitor the hosts file for unauthorized changes and may block edits, restore the file automatically, or alert you when modifications are detected. This is because malware sometimes alters the hosts file to redirect traffic.
DNS caching — Editing the hosts file takes effect at the OS level, but your browser or system DNS cache may still hold old entries for a short period. On Windows, running ipconfig /flushdns clears the DNS resolver cache. On macOS, the flush command varies by OS version.
Mobile and locked-down devices — On standard (non-rooted, non-jailbroken) Android and iOS devices, the hosts file is either inaccessible or read-only. Modifying it requires root or jailbreak access, which carries its own security and warranty implications.
Managed and enterprise environments — On corporate machines, group policy or MDM (Mobile Device Management) configurations may restrict access to system files including the hosts file, or may overwrite changes automatically.
The Format of Hosts File Entries
Each entry in the hosts file follows a simple format:
IP_address hostname [optional alias] For example:
127.0.0.1 myproject.local 0.0.0.0 ads.example.com Lines beginning with # are comments and are ignored by the system. There's no practical limit on the number of entries, though extremely large hosts files (tens of thousands of entries) can introduce minor lookup delays on some systems.
When the Hosts File Doesn't Work as Expected
A few situations cause hosts file entries to be ignored or bypassed:
- DNS-over-HTTPS (DoH) — Browsers like Chrome and Firefox can be configured to use encrypted DNS directly, bypassing the OS resolver and therefore the hosts file entirely
- VPN software — Some VPN clients route DNS queries through their own servers, which may not check the local hosts file
- Incorrect formatting — Extra spaces, wrong IP format, or hidden characters from a rich text editor can silently break entries
Whether the hosts file is the right tool for your specific goal — blocking, redirecting, or testing — depends on your OS, your network configuration, what other software is running on your machine, and how your browser handles DNS resolution.