How to Modify and Manage the Hosts File on Linux

Bertel King Updated 14-12-2019

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, 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 The 5 Best Free Dynamic DNS Providers A DDNS service can help you connect to your home PC while on the move. Here are the best free dynamic DNS providers to consider. Read More (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, 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 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 instead:

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):

Advertisement 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 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 that has a simple website that does something useful for you, you can type the following in your hosts file:        homeserver

Then, if you open your browser and just type in http://homeserver, it’ll automatically redirect to 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:

  1. Type 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 into the address bar. If you type 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. This is one of several Google Chrome privacy tips 7 Essential Privacy Settings for Chrome OS and Google Chrome Using a Chromebook, but concerned about privacy? Tweak these 7 settings in the Chrome browser on Chrome OS to stay secure online. Read More worth doing anyway.

How Will You Change the Hosts File?

The hosts file offers an easy way to block access to certain websites 5 Ways to Block Sites and Limit Screen Time on Linux Parental control software is virtually unheard of on Linux. Here are several ways you can block sites and manage content on Linux. Read More 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 6 Reasons to Start Your Kids Off With Linux Computers, and their operating systems, are not created equal. The OS they use matters, and Linux might be the best one to start them off with. Let's talk about why. Read More .

Image Credit: Oleksiy Mark/Shutterstock

Related topics: Computer Security, Linux Tips, Troubleshooting.

Affiliate Disclosure: By buying the products we recommend, you help keep the site alive. Read more.

Whatsapp Pinterest

Leave a Reply

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

  1. Upplady
    March 4, 2020 at 7:27 pm

    Modifying your hosts file enables you to override the domain name system (DNS) for a domain on a specific machine. DNS management is useful when you want to test your site without the test link prior to going live with SSL, verify that an alias site works prior to DNS changes, and for other DNS-related reasons. For information about changing your DNS settings, see Changing DNS settings on Linux .

  2. Arghya Polley
    March 15, 2019 at 8:45 am

    Keep up the good work.

  3. Jack M.
    August 10, 2018 at 12:12 pm

    I used to use a program called CIP from to load and update my hosts file. I came here looking for a Linux equivalent. Any ideas other than learning programming (which I may eventually end up doing) ?

  4. Michael Frost
    July 26, 2018 at 6:38 pm

    Thanks for the troubleshooting tips for Chrome, very helpful