How to Edit Hosts File on Linux?

On a Linux system, the “hosts” is a text file that performs mapping the hostnames to IP addresses. It resolves hostnames to IP addresses without the need to query a Domain Name System (DNS) server.

A host file is a useful tool for testing and debugging network configurations and blocking access to certain websites by mapping their hostnames to a non-existent IP address.

This guide will demonstrate various ways to edit the host file on Linux. The supported guideline of this tutorial is as follows:

Let’s start the article with the accessibility of the host file.

Why is the Hosts File Edited?

You can edit the hosts file to add, remove, or modify the mappings of hostnames to IP addresses. The host file is located at “/etc/hosts” in most Linux systems. It is a simple list of hostname-to-IP address mappings. Each mapping is formatted on a separate line. To access the “/etc/hosts” file, execute the below script:

$ nano /etc/hosts

The output shows the hostname “localhost” is mapped to the IP address “”. While another hostname, “johnson-vm” is mapped to the IP address

Using Nano Editor to Edit the Host File

To edit the host file on a Linux system, you will need to use an editor such as “nano”.

After opening a terminal, enter the below command that opens the “hosts” file with the “sudo” privilege:

$ sudo nano /etc/hosts

The host file will open in the nano text editor. After necessary changes in the file and then press “Ctrl+X” to exit. For saving, hit “Y” and “Enter” to save the changes to the host’s file.

Using Vim Editor to Edit the Hosts File

If you are using a different text editor, such as vim, to install it on various Linux distributions, follow the commands:

$ sudo apt install vim     # Ubuntu Debian and LinuxMint
$ sudo dnf install vim     # CentOS, RHEL and Fedora
$ sudo pacman -S vim       # Arch Linux and Manjaro

After that, you can open the host file by entering the following command:

$ sudo vim /etc/hosts

Make any necessary changes to the file and then press “Esc” to exit edit mode.

For saving changes, type “:wq” and press “Enter“. It writes the changes and quits the editor.

Keep in mind using “sudo” to edit the host file, as it is a protected system file.

That is how you can edit the Host file on Linux.


Linux offers the “nano” and “vim” text editors to edit or modify the host or any other file using the “sudo” privilege. With the modification of host files, users can block a website, control access to network traffic, and many more. This guide has provided multiple ways to edit the host file on Linux.