A hosts file is responsible for mapping every website’s domain name with its IP address in every operating system. In simple words, it means that whenever we access any domain on our system, the operating system tries to search the hosts’ file for its corresponding IP address.

This article will look at how one can edit the hosts’ file and mitigate any issues associated with it.

Hosts File

As we mentioned earlier, the hosts’ file is responsible for providing the corresponding IP addresses for the domain names. A hosts file can be found in every operating system, and it has priority over Domain Name System (DNS). It means that whenever a domain is accessed, the hosts’ file is searched first for a corresponding IP address.

If the operating system fails to find any associated IP with the domain name in the hosts file, the system then queries the DNS to provide an associated IP. Essentially, the hosts’ file is a map that relates domain names with their respective IP addresses.

People edit their hosts’ files for many different reasons, some of which we will list here. One of the major advantages of editing the hosts’ file is to test a website without changing DNS settings. Editing the hosts’ file can prove to be convenient in a scenario where you want to migrate your website to a new server without changing the domain name first. Moreover, editing the hosts’ files can also be used to block websites on your system.

Hosts File in Linux

Like we said earlier, the hosts’ file is a simple text file containing IP addresses and domain names. The format of the contents of a host’s file is as follows.

IP address          Domain Name          Domain Aliases

It is worth mentioning that any lines that start with the # symbol are treated as comments in the hosts’ file, meaning that they will not be checked. Comments are created for the user’s own convenience and ignored by a compiler/interpreter. In most Linux systems, the hosts’ file is usually found to be located in /etc/hosts.

Editing the Hosts File in Linux

Now that we have covered some basic ground about what the hosts file is and why someone would want to edit it let us take a deep dive into learning how you can edit it for various purposes. Later, we will come back to reloading it and mitigating any possible problems associated with the process.

Open a new instance of the command terminal by accessing it through the applications menu or by pressing the keyboard shortcut Ctrl Alt T.

As we mentioned earlier, the hosts’ file is essentially a text file that contains IP addresses and domain names. So, how does one edit a text file? Through a text editor, of course. The next step is to open the hosts’ file in a text editor, and this can be achieved through the command given below.

<img alt="" data-lazy- data-lazy-src="https://kirelos.com/wp-content/uploads/2021/07/echo/hosts-after-editing-in-Linux.png" data-lazy- height="768" src="data:image/svg xml,” width=”1024″>

Or, if you like using vim, run the command below.

Since we are attempting to open and edit a system file, we are asked to enter our administrator password. This is done for safekeeping, and so once you have authorized root permissions, we can move on to the next step.

As for editing the hosts file is not the main objective of this tutorial; we will only be covering it briefly.

Let’s say, for starters, that you want to block YouTube on your Ubuntu system for whatever reason. This can be done by simply adding a small line of code at the end of the hosts’ file.

127.0.0.1          www.youtube.com

You can obviously replace YouTube with any other website of your choice.

So, with that being said, it is time for us to move on to reloading, etc/hosts/ after having edited the hosts’ file. We will now be looking at some commonly occurring issues regarding the subject and their solutions.

Issues related to editing the hosts’ file

The changes that are made in the hosts’ file should take effect immediately. However, in case of any issues, you can follow the steps given below.

After editing the hosts’ file, you need to restart any apps that cache DNS information. As we said earlier, changes should be applied immediately; however, you can run the command below to sort out any cache issues if they don’t.

$ sudo service network-manager restart

<img alt="" data-lazy- data-lazy-src="https://kirelos.com/wp-content/uploads/2021/07/echo/hosts-after-editing-in-Linux-2.png" data-lazy- height="768" src="data:image/svg xml,” width=”1024″>

In the case that the systemd-hostname is not correct, you can restart the system-hostnamed service. You can restart the mentioned service by entering the following command in the command terminal.

$ /bin/systemctl restart systemd-hostnamed

<img alt="" data-lazy- data-lazy-src="https://kirelos.com/wp-content/uploads/2021/07/echo/hosts-after-editing-in-Linux-3.png" data-lazy- height="768" src="data:image/svg xml,” width=”1024″>

Any other issues related to the hosts’ file should probably be mitigated by restarting your network connection or computer. However, if you edit your host’s file very frequently and cannot afford to restart it again and again – try the restart networking command we described earlier in this section.

The problem may as well be as simple as restarting your web browser. These applications do internal hostnames and DNS caching, so consider that rebooting them before continuing surfing the internet is a must.

Lastly, if your system uses dnsmasq, you can try the following command:

<img alt="" data-lazy- data-lazy-src="https://kirelos.com/wp-content/uploads/2021/07/echo/hosts-after-editing-in-Linux-4-1.png" data-lazy- height="768" src="data:image/svg xml,” width=”1024″>

Conclusion

In this article, we went into great detail about editing the hosts’ file and reloading it after editing. Lastly, we saw many commands that are useful for eradicating any possible issues with the hosts’ file.

About the author

<img alt="" data-del="avatar" data-lazy-src="https://kirelos.com/wp-content/uploads/2021/07/echo/myAvatar-150×150.png60ef080cdf0b2.jpg" height="112" src="data:image/svg xml,” width=”112″>

Zeeman Memon

Hi there! I’m a Software Engineer by degree, Blogger by skills who loves to write about tech, develop websites & do SEO. You can reach out to me on LinkedIn.