How to Modify and Manage the Hosts File on Linux
There’s a single file on your computer that serves as a small gateway between you and the web. It’s called the hosts file. If you need to block websites or create personalized web shortcuts on Linux, you can tackle these jobs by adding or tweaking a few lines in the hosts file.
What’s the Linux Hosts File?
The hosts file is a plain text file that all operating systems use to translate hostnames (also known as web addresses or URLs) into IP addresses. When you type in a hostname, such as wikipedia.org, your system will look into the hosts file to get the IP address needed to connect to the appropriate server.
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. What gives?
Turns out, your system will check the hosts file first before looking up a site on the DNS servers defined in your network settings (usually your ISP’s DNS servers).
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 wikipedia.org, the DNS servers will return Wikipedia’s IP address to your computer. But if you wanted to block Wikipedia on that computer, you can add an entry in the hosts file that tells your computer that wikipedia.org points to some other IP address that’s different from Wikipedia’s actual IP address.
There are many other things you can do with the hosts file, but these are just some basic examples and other uses highly depend on your needs.
The Linux Hosts File’s Location
On Linux, you can find the hosts file under
/etc/hosts. Since it’s a plain text file, you can open the hosts file using your preferred text editor.
However, since the hosts file is a system file, you’ll need administrative rights to save changes. So to edit the file using a Linux terminal-based text editor such as
nano, you will need to first type
sudo to gain superuser access:
sudo nano /etc/hosts
To use a graphical text editor such as gedit, you can try
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-based or graphical text editor. With nano, once you’re done editing the file, hit Ctrl + X, and then y to confirm overwriting the changes.
How to Add Sites to the Hosts File
In the hosts file, each entry has its own line. The syntax is simple. Type the IP address you want the hostname to translate to, press the tab key on your keyboard, and then type the hostname.
For example, to block Wikipedia, you’d type (remembering to use the tab key rather than the spacebar):
127.0.0.1 is the loopback IP address that will always point back to your own system. Since the web isn’t stored on your machine, your browser will say the site can’t be found. It is now effectively blocked.
If you feel intimidated by the terminal, check out Linux Mint’s Domain Blocker application (also known as mintnanny). It will add entries into the hosts file that point the hostnames you specify to 127.0.0.1. But to do anything else, you will still need to make changes with a text editor.
Download: Domain Blocker (Free)
Create Shortcuts in the Hosts File
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, you can type the following in your hosts file:
Then, 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.
Alternatively, you can use the hosts file to create shortcuts to certain sites on the web. Use a command such as nslookup to find a website’s IP address, then add it to your hosts file alongside the desired shortcut, just as in the example above. But this only works on websites with dedicated IP addresses. It probably won’t work with most of the sites you visit.
Potential Issues With the Hosts File
So we’ve established how to make changes to the host file, but you may still run into issues when using Google Chrome. This web browser 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 Wikipedia blocked in the hosts file, then Chrome will circumvent the block if you just type
wikipedia.cominto the address bar. If you type
http://wikipedia.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. This is one of several Google Chrome privacy tips worth doing anyway.
How Will You Change the Hosts File?
The hosts file offers an easy way to block access to certain websites on your computer. Since many versions of Linux don’t come with parental control software baked in, this knowledge can come in handy if you decide to start your kids off with a Linux PC .
Image Credit: Oleksiy Mark/Shutterstock