reload etc hosts file in linux

How to Reload etc/hosts in Linux

Every Linux system has etc/hosts file that maps website’s domain name with IP address. It is useful for mapping intranet addresses, blocking websites as well as redirecting domains to new IP addresses. It is also used to test a website without changing its DNS entries. Once you make changes to etc/hosts file in Linux, you need to reload it in order to apply changes. Whenever users on a given system try accessing a website or domain, this file is searched for mapping domain names to IP addresses, before using DNS (Domain Name System) to get the correct IP address for it. If there is an entry for a given domain/website in etc/hosts file it is given priority over DNS.


How to Reload etc/hosts in Linux

Here is the format of etc/hosts file. Each row in it contains the following entries. IP address followed by domain name and an optional domain alias.

IP address          Domain Name          Domain Aliases

If any entry starts with # it is treated as comment. For example, you can open the file in any text editor.

$ vi /etc/hosts

Add the following lines to it, just as an example.

127.0.0.1 www.facebook.com
127.0.0.1 www.twitter.com
#127.0.0.1 www.linkedin.com

Save and close the file.

In the above etc/hosts file, we have assigned IP address for facebook.com, twitter.com and linkedin.com as localhost 127.0.0.1. When your system uses this hosts file, then it will redirect all visitors from your system to facebook.com and twitter.com to localhost instead of their actual IP addresses, thereby effectively blocking these two websites on your system. This will be applicable only for people browsing these websites from your system. But the last line mapping linkedin.com to localhost is considered as comment and not implemented.

In most cases, the changes made to /etc/hosts take place immediately after you close the file. But you will need to restart the apps that cache DNS information. Nevertheless, if you find that the above changes are not applied, you can reload etc/hosts file with the following command.

$ sudo service network-manager restart

In case the system-hostname is not correct, you can restart system-hostnamed service with the following command in terminal.

$ /bin/systemctl restart systemd-hostnamed

If your system uses dnsmasq utility, you may need to terminate it to apply changes.

$ pkill -HUP dnsmasq

Sometimes you may need to restart the web browser to apply changes. If you find that the changes are still not applied, you can simply restart network connection or computer.

In this article, we have learnt how to reload /etc/hosts file in Linux.

Also read:

How to Kill Stopped Jobs in Linux
How to Change MAC Address in Linux
How to Convert Permissions to Octal in Linux
How to Compare Local & Remote Files in Linux
How to Generate Strong Pre Shared Key in Linux

Leave a Reply

Your email address will not be published. Required fields are marked *