A little known feature called the hosts file can be tweaked to modify your system, depending on your needs. Whether you need to create shortcuts, block websites, or much more, you can easily take care of it all in the hosts file.
But how do you make changes to it? We’ll show you that plus some usage examples so you get started right away.
What’s the Hosts File?
Simply put, the hosts file is a plain text file that all operating systems use to translate hostnames into IP addresses. Whenever you type in a hostname, such as facebook.com, your system will look into the hosts file to get the IP address that it needs to connect to the appropriate server. However, if you open the hosts file, you’ll quickly notice that it doesn’t have the directory of the entire Internet in there. Instead, there might be just a couple lines and that’s it. Your system will always check the hosts file first, and anything that isn’t defined in the hosts file will be looked up on the DNS servers defined in your network settings (usually your ISP’s DNS servers).
But this means that you can use the hosts file to add to what the DNS servers can’t provide (such as aliases for locations on your local network, which is otherwise only possible if you have a DNS server set up within your local network) or override the IP addresses that your DNS servers would normally provide.
For example, if you ask for facebook.com, the DNS servers will return Facebook’s IP address to your computer. But if you wanted to block Facebook on that computer, you can add an entry in the hosts file that tells your computer that facebook.com points to some other IP address that’s different from Facebook’s actual IP address. To block Facebook, you could say in the hosts file that facebook.com should point to 127.0.0.1, which is the loopback IP address that will always point back to your own system. There are lots of other things you can do with the hosts file, but these are just some basic examples and other uses highly depend on your needs.
How to Edit It
On Linux, you can find the hosts file under
/etc/hosts. Since it’s a plain text file, you can easily use either a terminal text editor or a graphical text editor. Sadly, there’s only one graphical tool that provides partial control of the hosts file: Linux Mint‘s Domain Blocker application. It works by adding entries into the hosts file that points the hostnames you specify to 127.0.0.1. However, that’s all you can do with the Domain Blocker — any other uses of the hosts file require that you make the changes with a text editor.
Since the hosts file is a system file, you’ll need administrative rights to save changes to the file. Therefore, you can edit it using terminal commands such as
sudo nano /etc/hosts or
gksu gedit /etc/hosts. You have to use the terminal so you can launch the appropriate application with administrative rights. Just replace nano or gedit with your favorite terminal text editor and graphical text editor, respectively. With nano, once you’re done editing the file, hit Ctrl + X, and then y to confirm overwriting the changes.
Understanding Hosts File Syntax
In the hosts file, here, each entry is on its own line. For the purposes of this article, the syntax is very simple: you type the IP address you want the hostname to translate to, then a tab, and then the hostname. For example, to block Facebook, you’d type 127.0.0.1<tab>facebook.com, where <tab> is you pressing the tab key on the keyboard. To double check, you can also check the screenshot and compare it to yours.
In another example, if you have a computer on your home network (say with an IP address of 192.168.1.10) that has a simple website that does something useful for you, can can type the following in your hosts file: 192.168.1.10<tab>homeserver. Now, if you open your browser and just type in
http://homeserver, it’ll automatically redirect to 192.168.1.10. And that’s much easier than typing in the IP address every time.
The only issue I’ve come across is with Chrome. It tends to ignore the hosts file unless you do one of two possible things:
http://at the beginning of each address. For example, if you have Facebook blocked in the hosts file, then Chrome will circumvent the block if you just type
facebook.cominto the address bar. If you type
http://facebook.cominto the address bar, it will follow the hosts file.
- Disable “Use a web service to help resolve navigation errors” in Chrome’s settings and then you won’t have to type
http://at the beginning every time.
Try it Out!
The hosts file is a magical little file that offers several useful features. Even if you don’t necessarily need any of the features it provides (although getting some ideas for your hosts file wouldn’t hurt), it’s not a bad idea to play around with it to familiarize yourself. That way, if you ever come across a situation where you could use the hosts file’s features, you’re aware of them and know how to set it up.
What’s in your hosts file? Are there any unique ways you’ve used the hosts file? Let us know in the comments!
Image Credit: Internet building blocks via Shutterstock
Explore more about: Computer Security.