Marcio Cunha

How to configure the hosts file on Linux to redirect IPs

Learn step by step how the local name resolution file (/etc/hosts) works on Linux, mapping domain names to specific IP addresses.

Marcio Cunha•5 min
Also available in:PortuguêsEspañol
Summary
  • The /etc/hosts file acts as the first line of address resolution before any external network DNS query takes place.
  • Manual editing requires superuser privileges and strict syntax combining the target IP address and domain name.
  • Testing local resolution with the ping command immediately validates whether the operating system prioritized the static mapping.
  • Using the hosts file is ideal for local web development testing and simulating environments prior to public record propagation.
  • Understanding the system resolution priority order prevents persistent conflicts between local entries and remote DNS servers.

What is the hosts file and how it works in practice

When we type a web address in a browser, the computer needs to figure out the actual IP address behind that name. This translation process is known as name resolution. On Linux, before querying any server on the internet, the system checks the local /etc/hosts file first. In practice, this means you can intercept or redirect any domain to wherever you want simply by writing a few lines in a plain text file.

This mechanism acts like a private phone book on your computer. If the name you are looking for is listed there, the system uses that information immediately and never asks the network. Otherwise, it resorts to external servers or utilities like the dig command to find the correct destination. This priority makes the hosts file a powerful tool for developers and system administrators.

The file structure is extremely simple and straightforward. Each valid line contains an IP address, followed by one or more spaces or tabs, and the corresponding domain name. You can also add comments using the hash character (#) at the beginning of the line, which helps document your changes. Before diving in, remember that editing system files requires care to avoid corrupting vital configurations.

How to edit the hosts file with administrative privileges

Because /etc/hosts is a protected operating system file, you cannot save it using a regular user editor. You need superuser (root) privileges to make any modifications. The first step is to open your Linux terminal and choose a command-line text editor, such as nano or vim. In practice, the nano editor is usually the friendliest option for beginners.

To open the file with write permissions, execute the command with the sudo utility. Enter your user password when prompted, and the file will open in the terminal. If you want to check your network socket connections in parallel, you can consult the command to track active TCP connections. Below, see the exact command to safely start editing the hosts file.

sudo nano /etc/hosts

With the file open on your screen, you will see some default lines pre-configured out of the box. The most common one points the address 127.0.0.1 to the name localhost. It is right below these default lines that you should add your own redirection rules, keeping the structural pattern clean and organized.

Step-by-step guide to mapping a domain to a specific IP

Now that the file is open in the editor, let us add our practical mapping. Imagine you are developing a website and want to test the domain mysite.local pointing directly to your local machine server or to a test IP on your internal network, such as 192.168.1.50. Every line you add creates a direct shortcut that the operating system blindly obeys.

Follow this sequence of actions to map correctly and safely:

  1. Open the terminal and access the file with the text editor using elevated permissions.
  2. Scroll to the bottom of the file and add the desired IP followed by the domain, for example: 192.168.1.50 mysite.local.
  3. Save the changes by pressing the editor shortcut keys (such as Ctrl+O and Enter in nano) and exit (Ctrl+X).

Once saved, the changes take effect immediately for almost all system applications. However, some modern web browsers have aggressive internal DNS caches that might insist on querying the real network. If that happens, restarting the browser or clearing the cache usually resolves the issue permanently.

How to test redirection using the ping command

After configuring the hosts file, the next and crucial step is to validate whether the redirection is working as expected. The fastest and most accessible tool for this is the ping command, which sends small data packets to the destination and measures response time. In practice, it will tell us if the domain name was correctly translated to the IP we just registered.

Open the terminal again and run the ping command providing the domain you just mapped. Pay close attention to the IP address that appears in parentheses on the screen output. If the displayed IP is exactly the one you put in the hosts file, it means the mapping was successfully interpreted by the operating system.

ping -c 4 mysite.local

If the response shows the correct IP, your computer now knows exactly where to send traffic when you try to access that name. If the command returns a resolution error indicating the host was unknown, check the syntax of the /etc/hosts file for typos, extra spaces, or missing save permissions. To explore these architectural concepts further in production, see our related technical analysis on How to test EEPROM memory on the bench with a CH341A programmer.

Final considerations on using the hosts file

The /etc/hosts file remains one of the simplest and most versatile tools in the Linux ecosystem for controlling local name routes. It eliminates the need to configure complex DNS servers when you simply need to point a few domains to specific IPs in development, testing, or bench staging environments. Knowing how to use it securely saves precious time in everyday engineering.

Despite its immense utility, remember that manual mapping is static. If the server's IP address changes in the future, you will need to update the file on every client machine where the rule was applied. For larger structures or evolving environments, the ideal approach is to migrate to centralized DNS solutions, keeping the hosts file restricted only to quick testing scenarios and immediate debugging.