Networking

How to Edit Hosts File on Linux?

A Hosts file is supported in Linux, Windows, and Mac; they are plain-text files that work to map hostnames to various IP addresses. It is great to edit the Hosts file when you run the test on the specific network.  You can also use the mapping of an IP address to skip the process in which the web browser uses the DNS (Domain Name Server) lookup for translating a domain name to a particular IP address.

When a user types a website’s domain name, the domain name needs to translate into a specific IP address. A Hosts file has a top priority over DNS since an operating system checks its Hosts file for a domain and in case there is no entry for that domain. It starts to query the configured DNS servers for resolving the particular domain name. It was the little information about the Hosts file, and we will consider every single aspect on how to edit Hosts files on Linux easily.

Why Edit Hosts File?

There are various reasons for editing hosts file in the Linux machine:

  • You can easily block a website.
  • You can handle an attack.
  • You can create an alias for different locations on a local server.
  • You can override addresses that a DNS server delivers.
  • You can easily control access to network traffic.

How to Edit Hosts File on Linux?

Open the terminal from applications > utilities > terminal feature, or you can use CTRL, ALT, and T as shortcut keys.

After opening the terminal, open the Linux Hosts file in the system using the following command:

sudo vim /etc/hosts

We have used Vim, but you can use different text editors like nano.

Once you execute the command, the system will prompt you to enter the password to open the Hosts file.

You can easily modify the Hosts file, so the file is formatted like the IP address is written first, and the server’s name is on second.

0.0.0.0 server.domain.com

You can add entries at the end of the Hosts file as per your requirements. In case you want to ignore any line or specify the system to not read the line, then you can put the “#” sign at the starting of that particular line.  Finally, you can save the file before and make sure you save it before exiting.

Block a Website

If you want to block a particular website from redirecting it to the localhost’s IP address, change the Hosts file’s domain name. For example, we want to block xxyyzz.com, then we can write:

134.1.0.1 <a href="http://www.xxyyzz.com">www.xxyyzz.com

In the above text, 134.1.0.1 is the IP of the localhost, but if you want to make changes in the default route, then you can write:

0.0.0.0 www.xxyyzz.com

In the above text, 0.0.0.0 IP is a default route from this particular Hosts file.

At last, you can press CTRL and X keys simultaneously to save the file.

Name Service Switch

As mentioned, a Hosts file works to bypass a Domain Namer server lookup. Still, it has another file that has the information of the operating system about the order for finding the IP address translation.

The nsswitch.conf is a file completely configured for finding the DNS, then skips the Hosts file and goes in the DNS lookup. You can use the below command line for configuration checking easily:

cat /etc/nsswitch.conf
[email protected]:~$ cat etc nsswttch.conf
# /etc/nsswitch.conf
#
# Example configuration of GNU Name Service Switch functionality.
# If you have the 'glibc-doc-reference' and 'info' packages installed, try:
# 'info libc "Name Service Switch"' for information about this file.

passwd:      compat systemd
group:       compat systemd
shadow:      compat
gshadow:     files

hosts:      files mdns4_minimal [NOTFOUND=return] dns myhostname files
networks:    db files
protocols:   db files
services:    db files
ethers:      db files
rpc:         db files

netgroup: nis
xyz@xyz-VirtualBox: ~$

There is an entry called “Hosts,” ensuring that the files should be listed in the right-hand column in the above results. In case it is not listed like this, then open your file editor using the below command:

sudo vim /etc/nsswitch.conf

This command will open the Vim text editor to make changes and set the files at the beginning for appropriate use.

Conclusion

The Hosts files are beneficial to perform multiple tasks for domain names and IP addresses.  It was the complete information on the Hosts file and how you can easily edit the Hosts file on Linux. This procedure is tried and tested on multiple Linux machines, but you have to carefully follow every step.

About the author

Simran Kaur

Simran works as a technical writer. The graduate in MS Computer Science from the well known CS hub, aka Silicon Valley, is also an editor of the website. She enjoys writing about any tech topic, including programming, algorithms, cloud, data science, and AI. Travelling, sketching, and gardening are the hobbies that interest her.