Marcio Cunha

How to configure the hosts file on Windows and Linux

Learn how static domain name mapping works, where the hosts file is located on Windows and Linux, and how to clear DNS cache for immediate changes.

Marcio Cunha•4 min
Also available in:PortuguêsEspañol
Summary
  • The hosts file acts like a local phone book translating domain names directly to IP addresses before querying external servers.
  • On Windows, the file is located inside the drivers System32 folder, whereas on Unix/Linux systems it resides directly in the etc root directory.
  • Modifying the file requires administrative privileges, either by opening Notepad as administrator or using the sudo command in the terminal.
  • The operating system caches recent lookups, making DNS cache flushing mandatory for new entries to work instantly.
  • Syntax errors or extra spaces can corrupt the mapping, requiring strict attention to the IP format followed by the hostname.

What is the hosts file and why it matters

In practice, the hosts file acts as a private and immutable phone book living inside your own computer. When you type an address into your browser, the system needs to figure out the IP address (the machine's numerical network address) of that site before opening the page. Normally, it asks a Domain Name Server (DNS), which acts like a public internet directory. However, before spending time asking the external network, the operating system checks your local hosts file.

This means that if you create a manual rule in this file, your computer will blindly obey that order and ignore any information coming from the external internet. This behavior is extremely useful for developers testing sites on local servers, system administrators blocking malicious pages by redirecting them to the void, or even for optimizing access to internal network services without relying on complex infrastructures. To better understand how network addresses behave, it is worth checking out our guide on how to set static IP on Linux using NetworkManager.

Where to find and how to edit hosts on Windows

In Microsoft's operating system, the hosts file has no hidden extension and is tucked away inside a protected system folder. The exact path where the file lives is within the Windows network configuration folder, specifically at C:WindowsSystem32driversetchosts. Because this folder belongs to the core operating system, you cannot simply double-click and save changes without superuser permission.

To edit it safely, you must open a text editor, such as Notepad, by right-clicking it and choosing the run as administrator option. Then, open the file by navigating to the correct directory. Each valid line in the file contains an IP address followed by one or more spaces, and right after, the corresponding domain name. For example, if you want to point a test site to your own machine, simply add the line pointing the local IP to your development domain.

Where to find and how to edit hosts on Linux

On Unix and Linux-based systems, the operating logic is exactly the same, but the file location changes to reflect the standard directory tree of those environments. The hosts file is located directly in the etc folder, meaning the absolute path is etc/hosts. Because it sits in a directory protected by strict security permissions, you will need to use the terminal and command-line tools to modify it.

To open and edit the file, you can use terminal-based text editors like nano or vim, always preceded by the command that temporarily elevates your privileges. When opening the file with administrative permissions, you will see some default lines already configured out of the box, such as the mapping for the local address known as localhost. To add new rules, simply scroll down to the bottom of the document and enter the destination IP followed by your desired domain name, then save the file.

The critical role of clearing DNS cache

One of the most common mistakes made by beginners tweaking the hosts file is changing the address, saving the file, refreshing the browser page, and realizing absolutely nothing changed. In practice, this happens because your operating system and the browser itself keep a temporary memory (called cache) of recently visited addresses to speed up browsing. If the computer already knows the IP by heart, it won't even look at the updated hosts file.

To solve this problem and force the system to read the new rules immediately, you need to flush the DNS cache. On Windows, this is done by opening the command prompt and running a specific command to clear the name resolution memory. On Linux, depending on the distribution and installed network services, you might need to restart the network manager or the service responsible for that cache. When failures persist even after network adjustments, following a structured troubleshooting procedure similar to what we cover in how to troubleshoot DNS failures on Linux step by step with dig can be very helpful.

Step-by-step guide to clear DNS cache on major systems

If you have completed the changes in your hosts file and want to ensure the operating system reloads the information without rebooting the entire machine, follow the practical procedures described below according to your current operating system. To explore these architectural concepts further in production, see our related technical analysis on How to test RAM memory with MemTest86 without errors.

  1. On Windows, open the start menu, type cmd, right-click Command Prompt, and select Run as Administrator.
  2. In the terminal opened with elevated privileges, type the command to reset the cache table and press Enter.
    ipconfig /flushdns
  3. On Linux using systemd-resolved, open the terminal and run the official command to clear statistics and the local resolver cache.
    sudo systemd-resolve --flush-caches

Final considerations on using static mapping

The hosts file is a powerful, elegant, and surprisingly simple tool for solving everyday routing problems and local development tests. However, it requires discipline: forgetting old rules in there can cause massive headaches months later when the real site goes live on a production server and your computer insists on opening the old test version.

Use static mapping wisely, keep the file organized with explanatory comments using the hash character, and always remember to clear operational caches whenever you make significant changes to your network naming structure.