How to Modify and Manage the Hosts File on Linux
The hosts file is an important file on a Linux system. It is responsible for translating domain names to IP addresses for computers to communicate with each other. In this article, we will learn how to modify and manage the hosts file on a Linux system.
Step 1: Open the Terminal
First, we need to open the terminal. This can be done by pressing Ctrl+Alt+T on the keyboard or by clicking the terminal icon in the application launcher.
Step 2: Navigate to the Hosts File
Once the terminal is open, navigate to the hosts file by typing the following command:
sudo nano /etc/hosts
This will open the hosts file in the nano text editor.
Step 3: Modify the Hosts File
To modify the hosts file, simply add the IP address followed by the domain name in the following format:
IP_address Domain_name
For example, if we want to add the IP address 192.168.0.2 for the domain name example.com, we would add the following line to the hosts file:
192.168.0.2 example.com
Step 4: Save the Changes
Once the modifications have been made, save the changes to the hosts file by pressing Ctrl+O on the keyboard and then Enter to confirm the filename. Then, press Ctrl+X to exit the nano text editor.
Step 5: Verify the Changes
To verify the changes, use the ping command to test the domain name. For example, to test the domain name example.com, type the following command:
ping example.com
If the modification was successful, the output should show the IP address that was added to the hosts file.
Step 6: Manage the Hosts File
To manage the hosts file, we can use various tools such as text editors or command-line utilities. It is important to note that the hosts file should only be modified by users with administrative privileges.
To backup the hosts file, make a copy of the file to a different location. This can be done by using the cp command,
for example:
sudo cp /etc/hosts ~/Documents/hosts.backup
To restore the hosts file from backup, simply copy the backup file to the original location using the same command, but with the file paths reversed:
sudo cp ~/Documents/hosts.backup /etc/hosts