How to modify and manage Hosts files on Linux

There is a single file on the computer that acts as a small port between the user and the web, called the hosts file. If you need to block web pages or create personalized web shortcuts on Linux, solve these tasks by adding or adjusting a few lines in the hosts file.

Learn about the Hosts file on Linux

  1. What is file hosts on Linux?
  2. Location of hosts file on Linux
  3. How to add the site to the hosts file
  4. Create a shortcut in the hosts file
  5. The underlying problem with the hosts file

What is file hosts on Linux?

File hosts is a simple plain text file that all operating systems use to translate hostnames (also known as web addresses or URLs) into IP addresses. When entering the hostname, such as quantrimang.com, the system will review the file hosts to obtain the necessary IP address and connect to the appropriate server.

If you open the hosts file, you will quickly notice that there is no directory of the entire Internet in it, but only a few short lines.

Turns out, the system will check the host file before searching for a site on the DNS servers identified in the network settings (usually the ISP's DNS server).

This means that the hosts file can be used to add what the DNS server cannot provide (such as an alias for locations on the local network. This only happens if a DNS server is set up. in the local network) or override the IP addresses that DNS servers normally provide.

For example, if requested by quantrimang.com, DNS servers will return IP Address TipsMake.com to the computer. But if you want to block TipsMake.com on that computer, you can add an item in the hosts file, let the computer know that quantrimang.com points to some IP addresses other than the actual IP address of TipsMake.com .

There are many other things to do with hosts files, but these are just a few basic examples and other uses that depend heavily on the needs of each person.

Location of hosts file on Linux

On Linux, you can find the hosts file in / etc / hosts. Because this is a / etc / hosts file, you can open the hosts file with your favorite text editor.

However, because the hosts file is a file system, it will need admin rights to save the changes. So, to edit the hosts file with a terminal Linux-based text editor, such as nano, first enter sudo to have superuser access (superuser):

 sudo nano /etc/hosts 

To use a graphical text editor like gedit, you can try gksu instead:

 gksu gedit /etc/hosts 

You must use the terminal to be able to launch the application in accordance with admin permissions. Just replace nano or gedit with a terminal-based text editor or favorite graphics. With nano, when you've finished editing the file, press Ctrl + X and then press Y to confirm overwriting changes.

How to add the site to the hosts file

How to modify and manage Hosts files on Linux Picture 1

In hosts file, each item has its own line. The syntax is very simple. Enter the IP address you want to translate into the hostname, press the Tab key on the keyboard and then enter the hostname.

For example, to block Wikipedia, enter (remember to use the Tab key instead of the spacebar):

 127.0.0.1 wikipedia.org 

127.0.0.1 is a loopback IP address that will always point to your own system. Since the website is not hosted, the browser will indicate that the site cannot be found. Now, the website has been effectively blocked.

If you do not like to use Terminal, check out Linux Mint's Domain Blocker application (also known as mintnanny). Domain Blocker will add entries to the hosts file pointing to the hostname you specified to 127.0.0.1. But to do anything else, you will still need to change with the text editor.

  1. Download Domain Blocker (Free)

How to modify and manage Hosts files on Linux Picture 2

Create a shortcut in the hosts file

If you have a computer on your home network (assuming your device's IP address is 192.168.1.10 ) and a simple website is useful for you, you can enter the following content into your hosts file:

 192.168.1.10 homeserver 

Then, if you open the browser and enter http:/// homeserver, it will automatically redirect to 192.168.1.10. Much easier than typing IP addresses each time.

Alternatively, you can use file hosts to create shortcuts to certain websites on the web. Use a command like nslookup to find the site's IP address, then add it to your hosts file with the desired shortcut, just like in the example above. But this only works on websites with dedicated IP addresses and may not work for most websites you visit.

The underlying problem with the hosts file

The article mentioned changes to hosts files, but you may still have problems using Google Chrome. This web browser tends to ignore the hosts file unless you do one of the following:

1. Enter http:/// at the beginning of each address. For example, if you block Wikipedia in the hosts file, then Chrome skips blocking this page, if you enter wikipedia.com into the address bar. If you enter http://wikipedia.com into the address bar, the browser will follow the hosts file.

2. Disable 'Use a web service to help resolve navigation errors' in Chrome settings and then you do not have to enter http:/// at the beginning of the address bar every time you visit the site. This is one of several Google Chrome security tips worth doing.

File hosts provide an easy way to block access to some websites on the computer. Since many versions of Linux do not include parental control software, this can be useful if you decide to start your children using a Linux computer.

4.5 ★ | 2 Vote

May be interested

  • Manage folders in Unix / LinuxManage folders in Unix / Linux
    a directory is a file whose only task is to store the name and related file information. all files, which can be regular, special or directory files, are kept in folders.
  • File Management in Unix / LinuxFile Management in Unix / Linux
    all data in unix is ​​organized in files. all files are organized in folders. these directories are organized in a tree structure that is called the file system.
  • Is it possible to run .exe files on Linux?Is it possible to run .exe files on Linux?
    you don't need to trade the appeal of windows software for the stability, security, ease of customization (and even great classic appearance) of linux. in this article, you will be shown how to run windows exe (possibly executable) files and software using the linux operating system. these methods can be applied to any linux distribution, including ubuntu, kali linux, centos and many others.
  • Guide to network operation for Linux users: 11 commands to knowGuide to network operation for Linux users: 11 commands to know
    linux supports commands to download files, diagnose network problems, manage network interfaces or view network statistics on the terminal. here are some common linux commands to work with, please consult.
  • How to manage and restore Tmux sessions in LinuxHow to manage and restore Tmux sessions in Linux
    tmux is a multiplexer terminal (a tool that allows to use multiple terminals in a window) that integrates many useful features and is supported by a large number of community-created plugins.
  • How to use pandoc to convert files on LinuxHow to use pandoc to convert files on Linux
    you can use pandoc on linux to convert over 40 different file formats. you can also use it to create a simple docs-as-code system by writing to markdown, saving with git and exporting it in any supported format.
  • How to split PDF files from Linux Terminal with PDFtkHow to split PDF files from Linux Terminal with PDFtk
    pdftk can not only split pdf files but also edit and modify these files.
  • How to create new files in LinuxHow to create new files in Linux
    there are a number of different linux applications and commands that will create new files for you, even without launching the application. the method you use will depend on your purpose for the file. let's take a look at the options so you can see which one is most useful for you!
  • How to Create ISO Files on LinuxHow to Create ISO Files on Linux
    today's tipsmake will show you how to turn multiple files into one iso file on a linux computer. you need to use the linux command line to do it.
  • How to manage symlinks in LinuxHow to manage symlinks in Linux
    symlink (symbolic links) are files that link directly to other files. in linux systems, symlinks act as shortcuts.