Manage Local Hostname Resolution in Linux

CompTIABeginner
Practice Now

Introduction

In this lab, you will learn the essential skill of managing local hostname resolution in Linux by directly editing the /etc/hosts file. This file allows you to manually map hostnames to IP addresses on your local machine, a crucial technique for web development, network testing, and system administration. By controlling these mappings, you can override public DNS servers and direct traffic for specific domains to a local or custom IP address.

You will be guided through a complete, practical workflow. First, you will inspect the default contents of the /etc/hosts file using the cat command to understand its structure. Then, you will use the nano editor to add a custom host entry. To verify your configuration, you will test the new hostname with the ping command. Finally, you will remove the entry you added, restoring the file to its original state and completing the hands-on exercise.

Inspect the Default /etc/hosts File with cat

In this step, you will learn how to view the contents of the /etc/hosts file. This file is a simple text file that your operating system uses to map hostnames to IP addresses. Before your computer asks a DNS server on the internet to resolve a domain name, it first checks this local file. Understanding its structure is a fundamental networking skill on Linux.

We will use the cat command, which is a standard utility for reading files and printing their contents to the terminal.

Open your terminal and execute the following command to display the contents of the /etc/hosts file:

cat /etc/hosts

Example Output:

You will see an output similar to the one below. The exact content might vary slightly, but the format will be the same.

127.0.0.1       localhost

## The following lines are desirable for IPv6 capable hosts
::1     ip6-localhost   ip6-loopback
fe00::0 ip6-localnet
ff00::0 ip6-mcastprefix
ff02::1 ip6-allnodes
ff02::2 ip6-allrouters
127.0.1.1       Aliyun

192.168.2.67    iZbp1gakoh36s0s067d3ebZ iZbp1gakoh36s0s067d3ebZ

172.20.239.123  ubunut-base-image       ubunut-base-image

172.28.138.90   iZj6c8bsoq3dpsob2zhgheZ iZj6c8bsoq3dpsob2zhgheZ

Let's break down this output:

  • 127.0.0.1 localhost: This is the most common entry. It maps the IP address 127.0.0.1 (also known as the loopback address) to the hostname localhost. This is crucial for many local services to function correctly.
  • Lines starting with #: These are comments. The system ignores them, but they are useful for providing explanations.
  • IPv6 entries: Lines like ::1 ip6-localhost are the IPv6 equivalents for loopback and other standard network configurations.

Now that you have seen the default structure of the hosts file, you are ready to learn how to modify it in the next step.

Add a Custom Host Entry Using nano

In this step, you will manually add a new entry to the /etc/hosts file. This is a practical skill for developers who need to point a domain to a local server or for system administrators setting up internal networks. Since /etc/hosts is a system-protected file, you must use sudo to gain the necessary permissions to modify it.

We will use nano, a simple and user-friendly command-line text editor that is pre-installed in the LabEx environment.

Execute the following command to open /etc/hosts with nano:

sudo nano /etc/hosts

The terminal will now display the contents of the file inside the nano editor. Use the arrow keys to move your cursor to the end of the file. Add the following new line:

192.168.1.100   myfictionalserver.local

This line tells your system that whenever it sees the hostname myfictionalserver.local, it should resolve it to the IP address 192.168.1.100.

Now, you need to save the changes and exit the editor. Follow these key presses:

  1. Press Ctrl+O (the "Write Out" command) to save the file.
  2. nano will ask for the "File Name to Write". The default is /etc/hosts, which is correct, so just press Enter to confirm.
  3. Press Ctrl+X to exit nano and return to your terminal prompt.

You have now successfully modified the hosts file. In the next step, we will test this new entry to confirm it works as expected.

Test the Custom Hostname with ping

In this step, you will verify that the custom entry you added to /etc/hosts is working correctly. The best way to do this is by using the ping command. ping is a network utility used to test the reachability of a host on an IP network. It also measures the time it takes for messages to be sent from the source host to a destination computer and back.

When you ping a hostname, the system first tries to resolve that name into an IP address. Since we added an entry for myfictionalserver.local in /etc/hosts, your system should find it there and use the IP address 192.168.1.100.

Let's test it. In your terminal, run the following command. We use the -c 4 flag to send only 4 packets and then stop, which is convenient for testing.

ping -c 4 myfictionalserver.local

Example Output:

You should see output that confirms the name resolution, even if the host is unreachable.

PING myfictionalserver.local (192.168.1.100) 56(84) bytes of data.
^C
--- myfictionalserver.local ping statistics ---
4 packets transmitted, 0 received, 100% packet loss, time 3053ms

The most important part is the very first line: PING myfictionalserver.local (192.168.1.100). This confirms that your system successfully resolved the hostname myfictionalserver.local to the IP address 192.168.1.100 using your /etc/hosts file entry.

The command will continue to run until it is stopped (for example, by pressing Ctrl+C). Because there is no actual device at 192.168.1.100 on the network, no packets will be received. However, the name resolution part of the process was a success, which is what we wanted to demonstrate.

You have now confirmed that your custom hosts file entry is active. In the final step, you will learn how to clean up by removing this entry.

Remove the Custom Host Entry to Restore a Clean State

In this final step, you will clean up the changes you made to the /etc/hosts file. It is a crucial practice, especially in shared or production environments, to revert temporary changes to avoid unexpected behavior later. Restoring the file to its original state ensures that your system's name resolution works as expected for all other network operations.

Just as before, you will need sudo privileges to edit the file with nano.

Open the /etc/hosts file again in the editor:

sudo nano /etc/hosts

Use the arrow keys to navigate to the line you added in Step 2:

192.168.1.100   myfictionalserver.local

With your cursor on that line, you can delete it. A quick way to delete an entire line in nano is to press Ctrl+K.

After deleting the line, save the file and exit the editor:

  1. Press Ctrl+O and then Enter to save the changes.
  2. Press Ctrl+X to exit nano.

To confirm that the entry has been removed and the system can no longer resolve the hostname locally, try to ping it one last time:

ping myfictionalserver.local

Example Output:

This time, the command should fail immediately with an error message, as the system has no way to resolve the name.

ping: myfictionalserver.local: Name or service not known

This confirms that you have successfully removed the custom entry and restored your hosts file to a clean state.

Summary

In this lab, you learned how to manage local hostname resolution in Linux by manipulating the /etc/hosts file. You began by using the cat command to inspect the default contents and structure of this file, understanding its role in mapping IP addresses to hostnames before a DNS query is made. You saw how standard entries like 127.0.0.1 localhost are formatted and the use of comments for documentation.

Following the inspection, you gained practical skills by using the nano text editor to add a custom host entry, a common task for local development or network testing. To confirm your changes were effective, you used the ping command to test the new custom hostname, verifying that the system resolved it to the IP address you specified. Finally, you completed the lab by removing the custom entry to restore the system to its original state, demonstrating the full lifecycle of managing a local host entry.