Where Is the Hosts File on Windows, Mac, and Linux?

The hosts file is one of the oldest and most quietly powerful files on any computer. It predates the modern DNS system, yet it still runs on every major operating system today — and knowing where to find it can save you real headaches when troubleshooting network issues, blocking domains, or testing a website before it goes live.

What the Hosts File Actually Does

Before diving into locations, it helps to understand why this file matters. When your computer needs to resolve a domain name (like example.com) into an IP address, it doesn't immediately reach out to a DNS server. It checks the hosts file first.

Think of it as a local, manual address book that your OS consults before asking anyone else. If an entry exists in the hosts file, your system uses that mapping — no DNS query needed. This makes it useful for:

  • Blocking websites by pointing domains to a non-routable address (0.0.0.0 or 127.0.0.1)
  • Testing web servers by directing a domain to a staging IP before DNS propagates
  • Overriding DNS for specific domains on a single machine

Because the hosts file takes priority over DNS, a single line of text can redirect or block any domain on that device — regardless of what the broader internet says.

Where to Find the Hosts File by Operating System

Operating SystemHosts File Location
Windows 10 / 11C:WindowsSystem32driversetchosts
Windows 7 / 8C:WindowsSystem32driversetchosts
macOS (all modern versions)/etc/hosts
Linux (Ubuntu, Debian, Fedora, etc.)/etc/hosts
Android (rooted)/system/etc/hosts
iOS (jailbroken)/etc/hosts

Windows

On Windows, the hosts file lives deep inside the system directory: C:WindowsSystem32driversetc. The file is simply named hosts — no file extension.

🔒 You cannot edit it without administrator privileges. To open it for editing, you need to launch your text editor (Notepad, VS Code, etc.) with "Run as administrator" selected, then open the file from within that editor. Double-clicking the file directly will typically open it in read-only mode or fail silently.

A common gotcha: Windows may have User Account Control (UAC) or antivirus software actively watching this file. Some security tools treat hosts file modifications as suspicious and will block or revert changes — something to keep in mind if your edits aren't sticking.

macOS

On macOS, the hosts file is at /etc/hosts — the same path used across most Unix-based systems. You can open Terminal and edit it using a command-line editor like nano:

sudo nano /etc/hosts 

The sudo prefix is required because the file is owned by the system. After saving changes, macOS may require you to flush the DNS cache before your edits take effect. The exact command varies slightly across macOS versions, but it typically involves dscacheutil or killall -HUP mDNSResponder.

Linux

Linux distributions also use /etc/hosts, and the process is nearly identical to macOS. Open a terminal, use sudo with your preferred editor, and save. On most distributions, changes take effect immediately — though applications that have already cached DNS lookups may not pick up the change until restarted.

One difference worth noting: some Linux systems use systemd-resolved, a service that handles DNS caching. In those environments, the hosts file still works, but you may need to restart the resolved service or flush its cache for changes to propagate properly.

What the File Looks Like Inside

The format is straightforward. Each line contains an IP address followed by one or more hostnames, separated by whitespace:

127.0.0.1 localhost ::1 localhost 0.0.0.0 ads.example.com 192.168.1.50 staging.mysite.com 

Lines beginning with # are comments and are ignored by the system. Most hosts files ship with a few default entries (like the localhost mapping) that you generally shouldn't remove.

Factors That Affect How the Hosts File Behaves

Finding the file is only part of the picture. How reliably it works depends on several variables:

  • Editing permissions and tools: Using the wrong approach (no admin rights, wrong editor) is the most common reason edits don't save
  • DNS cache state: Your OS, browser, and some applications cache DNS independently — changes to the hosts file won't affect cached lookups until the cache is cleared or expires
  • Security software: Antivirus and endpoint protection tools frequently monitor or protect the hosts file, which can silently block modifications
  • VPN and proxy software: Active VPNs may bypass the local hosts file entirely by routing DNS through their own servers
  • HTTPS and HSTS: Modern browsers use HTTP Strict Transport Security, which can prevent you from accessing a redirected domain over plain HTTP even if your hosts entry is correct

The hosts file behaves consistently across setups in principle — but the practical experience of editing and applying it varies meaningfully depending on your OS version, security configuration, and network setup. 🖥️

What works cleanly on a bare Linux install may require extra steps on a locked-down Windows enterprise machine or a Mac running third-party security software. Your specific environment is what ultimately determines how straightforward — or layered — the process will be.