The hosts file allows users to configure how specific hostnames are resolved. Since the file takes precedence over other methods of hostname resolution, SysAdmins frequently edit it to create application testing environments and control which websites the system can access.
This article shows you how to edit the hosts file on Linux.
- Command-line access to the system.
- Administrative privileges.
What Is Linux Hosts File?
Hosts is a plain-text file that maps hostnames to public and private IP addresses. In Linux, it is located in the root-owned etc directory with other necessary configuration files and executables.
By default, the operating system uses hosts as the primary hostname resolution method and checks the file before communicating with a DNS server. If the system detects a relevant mapping in the file, it skips querying the server and resolves the host accordingly.
Why Would You Edit Linux Hosts File?
The simple structure of the hosts file allows quick testing and network customization. The following are the most common reasons to edit the file:
- Application testing. Developers of applications with integrated networking features use hosts to test ingress/egress traffic and the communication between components.
- Testing load balancers. The ability to map multiple IP addresses to a single domain allows developers to test the load-balancing capabilities of their website or app.
- Overriding DNS. The hosts file provides a quick way to test DNS configuration changes by temporarily overriding DNS entries.
- Creating server aliases. Replacing server IP addresses with easy-to-remember aliases helps organize the development environment.
- Blocking websites. The hosts file simplifies parental control and blocking websites on corporate networks. The websites are rendered inaccessible by mapping a public domain to a localhost IP address.
How to Edit Linux Hosts File?
The recommended way to modify the contents of the hosts file is by using the terminal and a terminal-based text editor. Follow the procedure below to edit the hosts file in Linux.
Step 1: Backup Old Hosts File
The hosts file can contain critical network configuration parameters. Execute the following command to ensure you have a backup copy of the original file before making any changes:
sudo cp /etc/hosts /etc/hosts.bak
If something goes wrong and you want to restore the previous hosts configuration, type the mv command below:
sudo mv /etc/hosts.bak /etc/hosts
Step 2: Open Hosts File
Open the host file using a text editor. This tutorial uses nano:
sudo nano /etc/hosts
The file may already contain some default system IPv4 and IPv6 mappings. IPv4 mappings are at the top of the file.
Step 3: Edit Hosts File
Place your mappings in the corresponding category. Use the following syntax:
To set up aliases for the same IP address, put them in the same line but separate the hostnames with a space:
[ip-address] [hostname-or-domain-name] [alias-1] [alias-2] [...]
When you finish, save the file and exit.
Note: The hosts file maps IP addresses only. Create a reverse proxy server to map hostnames to a specific port.
After reading this article, you should know how to edit the host file safely in Linux. The article also briefly overviews the hosts file and how its editing can help in application development and network administration.
Next, learn more about Linux configuration by reading Linux Config Files - A Comprehensive Guide.