The hosts file is a text file in Linux that maps hostnames with IP addresses. It has priority over the DNS resolution, so it can be used for testing applications, development, and blocking websites. There is also a hosts file in other Operating Systems, such as Windows and MacOS. However, this tutorial will show how to edit your hosts file in Linux.
Usually, when migrating your application, you can edit the hosts to check if your application is working correctly without making any DNS changes. This overrides DNS entries to test your website or application without making the DNS changes. You can also use the hosts file to add the website you want to block by mapping the domains or subdomains to the localhost IP address 127.0.0.1. Let’s say you have a local website or a device on your network that you want to access without having to remember the IP address. You can add the aliases in the hosts file at /etc/hosts to access them using a domain.
Table of Contents
Step 1. Back up
Before making any changes, you can backup the hosts file by simply making a copy of the file with the following:
cp /etc/hosts /etc/hosts.bak
This way, if you had previous entries in the file and accidentally modified or deleted them, you can revert the changes using the backup file. You are now ready to edit the hosts file.
Step 2. Editing the hosts file in Linux
Use your favorite text editor to open the file for editing, e.g., nano /etc/hosts. You can notice that there are already some entries in the hosts file.
127.0.0.1 localhost
127.0.1.1 blogpost
# 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
You can see the default entry for the loopback interface localhost, which maps to the loopback interface IP address 127.0.0.1. The entries below are for the IPv6 addressing, localhost, and other entries such as ipv6-allnodes, ipv6-allroutres, and others.
Let’s get back to the local website you have been working on, you can edit the hosts file to open the website using mywebsite.com.
nano /etc/hosts
Now just below the localhost entries 127.0.0.1 or in the first line, you can add the entry for your local website.
127.0.0.1 localhost
127.0.1.1 blogposts
127.0.0.1 mywebsite.com
# The following lines are desirable for IPv6 capable hosts
…
Now save the /etc/hosts file and try opening the website on the same machine using mywebsite.com URL in your favorite browser.
You can also add an entry for the IoT device and the media server that you have on your local network. Let’s say that the IP address of the IoT device is 192.168.1.123, and the IP address of the media server is 192.168.1.222.
You can add the entries in the /etc/hosts file using the following method:
127.0.0.1 localhost
127.0.1.1 blogposts
192.168.1.123 iot.device
192.168.1.222 mediaserver.my
Now save the /etc/hosts file, and you are set to use iot.device and mediaserver.my to access your IoT device and media server.
You can also block or filter some websites by mapping them to the loopback interface IP address 127.0.0.1. Let’s say the website you want to block is maliciousweb.com. You can add the entry in /etc/hosts file, which will block this website. Your local machine would resolve this website to 127.0.0.1, thus blocking the website.
127.0.0.1 localhost
127.0.1.1 blogposts
127.0.0.1 maliciousweb.com
…
Conclusion
The hosts file can be used to test websites in development or migrated websites before changing the DNS entries. The hosts file has precedence before the DNS, so you can also use the /etc/hosts file to block or filter certain websites from accessing your local machine. You can also use the hosts file in Linux to give aliases to the devices connected to your network.
You can contact us anytime with any further questions if you have one of our Linux VPS hosting plans. Our level 3 system admins are available 24/7 and will answer your chat in seconds. If you liked this article on how to edit the hosts file in Linux, please share this article with your friends or leave a comment below.