The /etc/hosts file is a text file used by the operating system to map hostnames to IP addresses. It is an important system file that is used to resolve hostnames to IP addresses when a DNS server is not available or when you want to override the default DNS resolution.
When a user enters a hostname in a web browser or attempts to connect to a server, the system consults the /etc/hosts file to determine the IP address associated with that hostname. If the hostname is listed in the file, the system will use the associated IP address to establish the connection. If the hostname is not listed in the file, the system will query a DNS server to resolve the hostname.
In addition to resolving hostnames, the /etc/hosts file can also be used to create custom domain names, define local network hosts, and block access to specific websites by redirecting their hostnames to the loopback address (127.0.0.1). Overall, the /etc/hosts file is a useful tool for network administrators, developers, and users who want to customize their network settings and improve network connectivity.
Important things to know about the /etc/hosts file:
Location: The /etc/hosts file is located in the /etc directory on Unix-based operating systems.
File Format: The file is a simple text file that contains lines of text. Each line corresponds to a mapping between a hostname and an IP address. The format of each line is as follows:
<IP Address> <Hostname> [<Alias1> <Alias2> ...] 127.0.0.1 localhost localhost.localdomain localhost4 localhost4.localdomain4
This line maps the loopback IP address (127.0.0.1) to the hostname “localhost”.
IP Address and Hostname: The IP address must be the first field in each line, followed by the hostname. The hostname can be a domain name or a hostname on the local network.
Aliases: Optional aliases can be added to the end of the line, separated by spaces. Aliases are alternative names for the same host, and they are used to allow multiple names to map to the same IP address.
Comments: Lines starting with the # symbol are treated as comments and are ignored by the system.
Priority: The /etc/hosts file takes priority over DNS resolution. If a hostname is listed in the /etc/hosts file, the system will use the IP address listed in the file instead of performing a DNS lookup.
Security: The /etc/hosts file can be edited by any user with root access to the system. It is important to only make changes to the file if you know what you are doing, as incorrect entries can cause problems with network connectivity.
How to create a good /etc/hosts file:
- Understand the Purpose: First, understand the purpose of the /etc/hosts file. It is used to map hostnames to IP addresses, and it can be used to override DNS lookup for specific hosts.
- Identify Hostnames and IP Addresses: Identify the hostnames and IP addresses that you want to add to the file. This can include local network hosts, development environments, or custom domains.
- Choose Hostnames: Choose a hostname that is easy to remember and descriptive of the host. For example, “webserver” for a web server or “dbserver” for a database server.
- Assign IP Addresses: Assign a unique IP address to each hostname. Make sure the IP address is correct and up-to-date.
- Add Aliases: Consider adding aliases for each hostname. Aliases are alternative names for the same host, and they can be used to allow multiple names to map to the same IP address.
- Add Comments: Add comments to describe each entry in the file. This will make it easier to understand the purpose of each entry and to make changes in the future.
- Test: Test the /etc/hosts file to make sure it is working correctly. Try to ping each hostname to ensure it resolves to the correct IP address.
Sample /etc/hosts File :
# This file describes the network interfaces available on your system # and how to activate them. For more information, see interfaces(5). # The loopback network interface 127.0.0.1 localhost 127.0.1.1 myhostname # Local network hosts 192.168.0.10 webserver.local web 192.168.0.11 dbserver.local db # Custom domains 10.0.0.1 example.com www.example.com
In the above sample file example, the file includes comments explaining the purpose of each entry. The first two lines are comments that describe the network interfaces available on the system.
The next two lines map the loopback IP address to the hostname “localhost” and the hostname of the system (“myhostname”) respectively. The loopback interface is a virtual network interface used for local communication within the system.
The next two lines map local network hosts to their respective hostnames. “webserver.local” is mapped to the IP address 192.168.0.10, and “dbserver.local” is mapped to the IP address 192.168.0.11. “web” and “db” are aliases for the hostnames.
The final two lines map custom domains to their respective IP addresses. “example.com” and “www.example.com” are both mapped to the IP address 10.0.0.1.
How to update the /etc/hosts file using nano:
- Open the file: Open a terminal window and type the following command to open the /etc/hosts file in a text editor:
sudo nano /etc/hosts
- Add or edit entries: Use the text editor to add or edit entries in the file. Each line should contain an IP address followed by one or more hostnames, separated by spaces or tabs. For example, to add a new entry for a local network host with the IP address 192.168.0.10 and the hostname “webserver.local”, add the following line:
192.168.0.10 webserver.local
- Save the file: After making changes to the file, save it by pressing
Ctrl+O
, thenEnter
to confirm the filename, and thenCtrl+X
to exit the text editor. - Flush the DNS cache: To ensure that the changes are applied immediately, flush the DNS cache by typing the following command in the terminal:
sudo systemd-resolve --flush-caches
That’s it! The /etc/hosts file has been updated using nano editor with the new entries, and the changes should take effect immediately. You can verify that the changes have been applied by pinging the hostname or accessing the server using a web browser.
How to update the /etc/hosts file using vim:
- Open the file: Open a terminal window and type the following command to open the /etc/hosts file in vim:
sudo vim /etc/hosts
- Add or edit entries: Use the vim editor to add or edit entries in the file. Press I to go to insert mode, then add or update file. Each line should contain an IP address followed by one or more hostnames, separated by spaces or tabs. For example, to add a new entry for a local network host with the IP address 192.168.0.10 and the hostname “webserver.local”, add the following line:
192.168.0.10 webserver.local
- Save the file: After making changes to the file, save it by typing
:w
and then pressing Enter. - Flush the DNS cache: To ensure that the changes are applied immediately, flush the DNS cache by typing the following command in the terminal:
sudo systemd-resolve --flush-caches
That’s it! The /etc/hosts file has been updated using vim with the new entries, and the changes should take effect immediately. You can verify that the changes have been applied by pinging the hostname or accessing the server using a web browser.
How to backup and restore the /etc/hosts file in Linux:
Backup:
- Open a terminal window and type the following command to create a backup of the current /etc/hosts file:
sudo cp /etc/hosts /etc/hosts.backup
- Press Enter and provide your user password if prompted.
- The above command creates a backup of the /etc/hosts file named “hosts.backup” in the same directory.
Restore:
- Open a terminal window and type the following command to restore the /etc/hosts file from the backup:
sudo cp /etc/hosts.backup /etc/hosts
- Press Enter and provide your user password if prompted.
- The above command replaces the current /etc/hosts file with the backup file “hosts.backup”.
That’s it! You have successfully backed up and restored the /etc/hosts file in Linux. It is a good practice to create a backup of the file before making any changes to it, so you can easily revert to the original configuration if needed.
The codes (if any) mentioned in this post can be downloaded from github.com. Share the post with someone you think can benefit from the information