Where Is the Localhost File on Your Computer?

If you've ever needed to block a website, redirect a domain, or test a local development environment, chances are you've been told to "edit the localhost file" — or more precisely, the hosts file. But finding it isn't always obvious, and its location changes depending on your operating system. Here's exactly where to look and what you need to know before touching it.

What Is the Localhost File (Hosts File)?

The term "localhost file" almost always refers to the hosts file — a plain text file your operating system uses to map hostnames to IP addresses before checking a DNS server. Think of it as a manual override for your internet directory.

When you type a web address into your browser, your computer first checks this file. If the address is listed there, it uses that mapping directly — no DNS lookup needed. This makes it useful for:

  • Blocking websites by pointing their domain to a dead-end IP address
  • Redirecting traffic from one domain to another locally
  • Testing web development projects before they go live on a real server
  • Overriding DNS entries for troubleshooting

The name "localhost" itself refers to the IP address 127.0.0.1 — your own machine. The hosts file always includes this entry by default.

Where Is the Hosts File Located? 🗂️

The location depends entirely on your 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 devices)/system/etc/hosts
iOS (jailbroken devices)/etc/hosts

On Windows, the file has no extension — it's simply named hosts, not hosts.txt. Many users trip over this when trying to open it directly.

On macOS and Linux, the path /etc/hosts is consistent across virtually all distributions and versions.

How to Access the Hosts File

Finding the file and actually opening it are two different challenges, because the hosts file requires elevated permissions on every major operating system.

On Windows

You can't simply double-click the file. You'll need to open a text editor like Notepad as Administrator:

  1. Search for Notepad in the Start menu
  2. Right-click it and choose Run as administrator
  3. Use File > Open and navigate to C:WindowsSystem32driversetc
  4. Change the file filter from "Text Documents" to All Files — otherwise the hosts file won't appear
  5. Select hosts and open it

Alternatively, you can navigate directly to the path in File Explorer, but you'll still need admin rights to save any changes.

On macOS

The hosts file lives at /etc/hosts and requires sudo access to edit. The quickest approach is through Terminal:

sudo nano /etc/hosts 

You'll be prompted for your administrator password. Once you save changes, you may need to flush your DNS cache for them to take effect.

On Linux

Same as macOS — the file is at /etc/hosts and requires root-level access. Use:

sudo nano /etc/hosts 

Or substitute your preferred text editor (vim, gedit, kate, etc.).

What the Hosts File Contains by Default

A fresh hosts file is mostly comments (lines starting with #) and a handful of default entries. The essentials look something like this:

127.0.0.1 localhost ::1 localhost 

The first line maps the hostname localhost to the IPv4 loopback address. The second does the same for IPv6. Everything else is either a comment or something a user or application has added over time.

Why the Location Matters Depending on Your Goal 🔧

Not all hosts file tasks are the same, and the location's relevance shifts depending on what you're trying to do.

For web developers, the hosts file on your local machine lets you point a custom domain name (like myproject.local) to 127.0.0.1 — so you can test a site with a real-looking URL before going live. This works entirely at the OS level, no router configuration needed.

For basic website blocking, pointing a domain to 0.0.0.0 or 127.0.0.1 in the hosts file is a lightweight, no-software-required method. But it only affects the device where the change is made — not other devices on the same network.

For network-wide control, the hosts file on a single machine won't reach. You'd need a router-level solution or a DNS filtering tool like Pi-hole.

For mobile devices, hosts file access on Android and iOS typically requires root or jailbreak access respectively — meaning standard consumer devices can't use this approach without significant modification.

Factors That Change the Experience

Several variables determine how straightforward — or complicated — working with the hosts file will actually be:

  • User permissions: Standard user accounts on Windows, macOS, and Linux can read the file but not save changes to it
  • Security software: Some antivirus programs flag or block changes to the hosts file, treating edits as potential malware behavior
  • OS version: While the file path has stayed consistent for years, GUI methods for accessing it vary by Windows version
  • Virtualization or containers: If you're running Docker, WSL, or a virtual machine, each environment may have its own separate hosts file

The right approach — and the appropriate level of caution — depends on whether you're a developer doing routine local testing, a home user trying to block a distracting site, or an IT professional managing multiple systems.