What is the hosts file?
If we talk in plain language, the hosts file is a simple text file that can be used on any operating system for translating host names into IP addresses. When you type a host name - for example, facebook.com or mintguide.org - your system will check the hosts file to get the IP address, which is required for connecting to the appropriate server. But if you open this file on your computer, you will quickly find that there is not stored a directory of all websites on the Internet. There may be just a few lines and nothing more. Your system is always first checks the hosts file, and everything not specified in it, will be found on the DNS servers configured in your network settings (usually a server of your Internet provider).
But it also means that you can use the Hosts file to add addresses that cannot be provided DNS servers (for example, it may be aliases of different locations on your home network that otherwise would be available only if the local network is installed and configured your own DNS server), or to bypass the IP addresses provided by your DNS servers by default.
For example, if you ask your browser to open facebook.com the DNS server will return the IP address of Facebook on your PC. But if you suddenly want to block Facebook on your computer, you can add to hosts file entry that points to your computer that all requests for facebook.com need to redirect to some other IP address that is different from the real IP address of Facebook. To block this social network, you can simply register a transfer of requests facebook.com the IP address 127.0.0.1, which will always return to your system. You can do a lot with this file, and this is just a very simple example - it all depends on your desires and needs.
Warning: keep in mind, hosts this is a system file, edit it only if you know what you're doing it!
How to edit hosts file
In Linux Mint you can find hosts file in /etc/hosts. Since this is a plain text file, you can just use a plain text editor (whether terminal or graphical). Unfortunately, there is only one graphic tool that provides partial control of the hosts file is the application Domain Blocker for Linux Mint.
This program works by adding entries to your hosts file which redirects your specified domains to 127.0.0.1 - that is on your computer. Anyway, that's all you can do with Domain Blocker - all other use of the hosts file require you to manually edit the file with a text editor.
As hosts it is a system file, you'll need administrative rights to save changes to the file. However, you can edit it using terminal commands such as
sudo nano /etc/hosts
sudo gedit /etc/hosts
You can also replace the nano or gedit on the command to start your favorite text editor. When you're finished editing the file in nano, you can press Ctrl+X and then y to confirm the overwriting of changes.
How to understand the format of the contents of the hosts file?
In the hosts file each record is on its own line. For the purposes of our article you can use a very simple syntax - you type the IP address to which you want to redirect the host name (domain), then the tab character (TAB) and the actual domain. For example, to block Facebook, you can enter 127.0.0.1-facebook.com where is - the Tab key on your keyboard. To make sure it is entered correctly, you can also look at the screenshot and compare it with what is in your file.
Here is another example of use of this opportunity. For example, if your home network is the computer (for example, its IP address is 192.168.100.1) running a simple website that makes something useful for you. You can enter the following in the hosts file: 192.168.100.1-myhomeserver. Now, if you open your browser and just type in the address bar http://myhomeserver it will automatically show you the contents of the host 192.168.100.10. It is much easier than to enter the IP address every time.
The only problem that I came across, related to the work of the Chrome browser. Chrome tends to ignore the hosts file, if you don't do at least one of two possible things:
- to enter http:// in front of each address. For example, if you blocked Facebook via hosts file, Chrome will bypass the lock if you simply enter facebook.com in the address bar. If you enter http://facebook.com in the address bar, Chrome "listen" hosts file and block the website of this social network.
- disable the option "Use a web service to help resolve navigation errors" in the Chrome settings, and then you don't have to type http:// before the address each time
Hosts is a small magic file, which offers several useful features. Even if you don't need any features that it provides (although, perhaps, you think), to see how it works will be very helpful. If you ever get into a situation where you could use the hosts file, you are familiar with them and know how to configure.
What is in your hosts file? Are there any unique cases of such use? Tell us in the comments!