How to View and Edit a Host File Location in Linux

How To View and Edit a Host File Location In Linux

In this tutorial, we will explain everything about the hosts file in Linux. The purpose of the host file is to map a domain name to its IP address. In this file, we can configure which websites can access the system, how specific hostnames can be resolved, which websites we want to block, etc. In the following paragraphs, we will describe a host file, how to find the host file location in Linux, how it is used, and how to edit it in any Linux operating system.

Editing a host’s files requires root privileges on the server or a user with sudo privileges. This is important because, at the end of the tutorial, we will explain how to edit the host’s file in Linux with real examples. Let’s get started!

What is a Linux Hosts File?

The Linux hosts file can be considered a manual override for DNS lookups on the server. It is a plain-text file that maps hostnames to public and private IP addresses. The Linux host file is crucial for the operating system since the OS checks the primary hostname before communicating with the DNS server. From the user’s perspective, when accessing the website in the browser, the computer first checks the host’s file to find the correct IP address to resolve the requested domain. The ISP will check the DNS server to see if the IP is not listed in the host file. The importance of the host file is that the operating system or local computer always prioritizes the host file over the DNS server.

Why Edit the Hosts File?

Modifying the host’s file lets you test your website without needing a DNS record. Using this mechanism, you can change the DNS settings for a domain only on your working machine. Modifying your local host file makes your local machine use the IP addresses specified in the file rather than DNS lookup. This is usually done when a server is migrated. For example, before changing the domain’s A record, users need to test the migrated website for compatibility with the new OS.

The following are the most common reasons for editing the host file: Application testing, overriding DNS, blocking sites, testing load balancers, using server aliases, etc. The host file is in plain text and can be edited using any text editor, depending on your operating system. On Linux OS, you can use nano, vim, vi, gedit, etc. It depends on which text editor is most suitable for the user. The following paragraph will show you how to view and edit the host file location in Linux OS.

How do you view and edit the Linux Hosts file?

You must know its location before viewing or editing the Linux “hosts” file. The location of the Linux Hosts file is located at /etc/hosts file on any Linux server. To view the Linux hosts file, you can execute the following command on your Linux server:

cat /etc/hosts

You should receive output similar to this:

root@host:~# cat /etc/hosts
127.0.0.1 localhost
127.0.0.1 host.test.vps

# 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

As you can see the host.test.vps points locally to 127.0.0.1.

Before you edit the Linux hosts file, it is recommended to make a backup of the file in case something goes wrong. To do that, execute the following command:

cp /etc/hosts /etc/hosts.backup

Now, when you have a backup of the Linux hosts, you can open and edit it according to your needs. To modify the host’s file, you can create an entry for the domain to point to the IP address you wish the site to resolve. Open the /etc/hosts file with your favorite editor and paste the following lines of code:

192.168.10.15 example.com

This means that when you access the domain name example.com in your browser, it will resolve to the IP address 192.168.10.15

Save the file and close it.

127.0.0.1 localhost
127.0.0.1 host.test.vps
192.168.10.15 example.com

# 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

Of course, you will change the IP address and the domain name you want to use. The most important thing is to follow the syntax below:

[IP-Address-Here] [Domain-Name-Here] [Alias-1-If-Needed] [Alias-1-If-Needed] ...

Next, you can paste aliases to the domain name if you want to use one.

Allow and Deny Hosts Files

The /etc/hosts.allow and /etc/hosts.deny files are also hosts files that we must mention and not avoid.

The /etc/hosts.allow file explicitly identifies IP addresses, domains, and hostnames that have allowed access. The hosts.allow maps service to IP addresses. For example, if you want the IP address 192.168.10.15 to access the SSH service, you have to open the /etc/hosts.allow file with your favorite editor and paste the following line of code:

sshd: 192.168.10.15

The /etc/hosts.allow file works with the /etc/hosts.deny file to either allow or deny access to the machine. The opposite of the rule above is that if you want to restrict access to the IP address 192.168.10.15, you must open the /etc/hosts.deny file and paste the same line of code:

sshd: 192.168.10.15

The line of code is the same, but it depends on which file you paste it into. The Linux host files are a great way to control which machines can access your server.

That’s it. You successfully edited a Linux host file on your server. You learned how to configure which domains to resolve and which IP addresses can or cannot access your server. Of course, if you have difficulties configuring all this stuff, you can contact our support. You only need to sign up for one of our NVMe Linux VPS plans and submit a support ticket. Our admins will help you with any aspect of the /etc/hosts file. Feel free to contact us. We are available 24/7.

PS. If you liked this post about how to view and edit the host file location in Linux, please share it with your friends or leave a comment below.

Leave a Comment