Modifying the hosts file on Linux

The hosts file is a local text file on a computer used to map hostnames to corresponding IP addresses. On Linux systems, we can modify the hosts file to configure hostname resolution.

Step 1: Open the terminal

On Linux systems, we can execute commands using the terminal. To open the terminal in a desktop environment, press the Ctrl+Alt+T key combination.

Step 2: Switch to superuser mode

In the terminal, we need to switch to superuser mode in order to have permission to modify the hosts file. Execute the following command:

sudo su

The system will ask you to enter the administrator password. Press enter after typing in the password.

Step3: Edit the hosts file

Use the following command to open the hosts file. You can also use your preferred text editor.

nano /etc/hosts

This will open the hosts file using the nano text editor.

Step 4: Modify the hosts file

In the opened hosts file, you can see some existing hostname and IP address mappings. You can add new mappings below these mappings.

Each line is formatted as IP address hostname. For example: localhost

You can add new mappings below the last line. For example:

Step 5: Save and exit

In the nano editor, hold the Ctrl key and press X simultaneously, then press Y, and finally press enter. This will save the changes and exit the editor.