Pinterest Stumbleupon Whatsapp
Ads by Google

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 How To Change Your DNS Servers & Improve Internet Security How To Change Your DNS Servers & Improve Internet Security Imagine this - you wake up one beautiful morning, pour yourself a cup of coffee, and then sit down at your computer to get started with your work for the day. Before you actually get... Read More (usually your ISP’s DNS servers).

muo-linux-hostsfile-dns

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.

Ads by Google

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 gedit: One Of The Most Feature-Filled Plain Text Editors [Linux & Windows] gedit: One Of The Most Feature-Filled Plain Text Editors [Linux & Windows] When you think of plain text editors, the first thing that may pop into your head is Windows' Notepad application. It does exactly what its job description states - plain features for a plain text... Read More . Sadly, there’s only one graphical tool that provides partial control of the hosts file: Linux Mint Is Linux Mint 17 "Qiana" The Ubuntu Killer? Is Linux Mint 17 "Qiana" The Ubuntu Killer? The latest version of Linux Mint, the 17th release codenamed "Qiana", is out! It's a great alternative for people leaving Windows as well as those who just don't quite like Ubuntu. Read More ‘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.

linux_mint_domain_blocker

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 An A-Z of Linux - 40 Essential Commands You Should Know An A-Z of Linux - 40 Essential Commands You Should Know Linux is the oft-ignored third wheel to Windows and Mac. Yes, over the past decade, the open source operating system has gained a lot of traction, but it’s still a far cry from being considered... Read More 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.

hosts_file_example

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.

Potential Issues

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:

  1. Type 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.com into the address bar. If you type http://facebook.com into the address bar, it will follow the hosts file.
  2. 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.

chrome_hosts_file_settings

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 6 Surprising Uses for the Windows Hosts File 6 Surprising Uses for the Windows Hosts File The Windows Hosts file allows you to define which domain names (websites) are linked to which IP addresses. It takes precedence over your DNS servers, so your DNS servers may say facebook.com is linked to... Read More 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

Leave a Reply

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