The Ubuntu hosts file is a utility that allows devices using this operating system to map one or more IP addresses to a hostname without having to perform a DNS lookup. We will show you how to edit your Ubuntu hosts file to add your entry in a few simple steps and recover a newly modified file of this type within minutes. However, you must ensure that you’re logged into your Linux device as a superuser before proceeding with the following steps. But before we get started with the recovery and modification of the Ubuntu hosts file, let’s see how it is formatted.
Format of the Ubuntu Hosts File
Every entry that belongs to the Ubuntu hosts file follows a specific format. Here’s the syntax and order that every line of the host file must use:
IPADDRESS HOSTNAME [HOSTNAME …]
If you look at the syntax above, you can see that we need two values as a bare minimum to form a line of code in the file. The first value you require to establish a line of code is the IP address (IPADDRESS) to which you want to direct the hostname. Meanwhile, the second value you must specify is the hostname (HOSTNAME) you want to associate with your chosen IP address. If you want to associate more than one, you merely need to type the hostnames on the same line. You just need to add one character space between two different hostnames in the same line for each entry.
Editing the Ubuntu Hosts File
You are now familiar with the syntax that needs to be used to form each line of code in the Ubuntu hosts file. Thus, it’s time to show you how you can edit it. On the Ubuntu Linux distribution, the host file will be located inside the “/etc/hosts” directory. These methods should work on every version of the Ubuntu distro, including 20.04. Although we will be using the nano text editor for this guide, you can use any text editor you want. You can also employ the Terminal application if you prefer. Follow these steps to edit and recover the Ubuntu hosts file:
1. Open the Hosts File as an Ubuntu Superuser
Once you get to the text editor, you can start editing by using this command: sudo nano /etc/hosts After the command is executed, you will need to type your user account password to verify that you’re the superuser.
2. Preview and Familiarize Yourself with the File
Once you verify that you’re the superuser, you will be able to see the details of the Ubuntu hosts file of your device. Inside the file, you will find the default entries and details, such as the localhost associated with the loopback address. You can also find the hostname of your computer, directed at the same address. Here’s an example of an Ubuntu hosts file:
127.0.1.1 pimylifeup
# The following lines are desirable for IPv6 capable hosts
::1 ip6-localhost ip6-loopback
fe00::0 ip6-localnet
ff00::0 ip6-mcastprefix
ff02::1 ip6-allnodes
ff02::2 ip6-allrouters
3. Add New Configurations to the Hosts File
Now, we will show you how to add new entries to the hosts file. Trust us, it’s incredibly simple. Previously, we have given you a rough idea of how to add a new IP address and hostname when we explained the host file syntax. Thus, let us give you a real example to show you what the lines would look like. For example, let’s assume that the hostname is “helloworld.” Further, the IP address the hostname is pointed to is “192.168.0.193.” With that in mind, the new entry would look like 192.168.0.193 helloworld Once you make this new entry, your hosts file should look something like this now:
127.0.0.1 localhost
127.0.1.1 pimylifeup
# The following lines are desirable for IPv6 capable hosts
::1 ip6-localhost ip6-loopback
fe00::0 ip6-localnet
ff00::0 ip6-mcastprefix
ff02::1 ip6-allnodes
ff02::2 ip6-allrouters
192.168.0.193 helloworld
As you can see, the new entry is located at the bottom of the original host file.
4. Save the Hosts File and Quit Text Editor
In this step, you will be saving the modified hosts file and leaving the editor. Since we are using the nano editor for this example, you need to press the “CTRL + X” keys and then the “Y” key to exit the editor after saving. If you’re using Terminal or another text editor of your choice, use the appropriate measure to save changes and leave Terminal or the text editor.
5. Double-check the Network Configuration
Now, all that is left to do is to check if you have configured the host file correctly. The best way to do so is to ping the newly added hostname. In this case, we will check whether the IP address responds properly to the hostname once we use the ping command. Go to the Terminal and execute this command: ping helloworld Once this command is executed, the computer should accept the ping and respond like this:
PING helloworld (192.168.0.193) 56(84) bytes of data.
64 bytes from helloworld (192.168.0.193): icmp_seq=1 ttl=63 time=0.477 ms
64 bytes from helloworld (192.168.0.193): icmp_seq=2 ttl=63 time=0.648 ms
If you get a similar response, it would mean that you have successfully edited your Ubuntu host file and the hostname has successfully routed to your computer’s IP address.