Hosts file options in ubuntu. How to set up virtual hosts in Ubuntu or Debian

A little known feature called the hosts file can be used to modify your system to suit your needs. Whether you need to create shortcuts, block websites, or whatever, you can do it all with the hosts file.

But how to make changes in it? Here, we'll walk you through how and provide some usage examples so you can start using the file right away.

What is a hosts file?

In plain language, the hosts file is simple text file which is used by any operating system to translate hostnames to IP addresses. When you type in a hostname - like facebook.com or myubuntu.ru - your system will check the hosts file to get the IP address it needs to connect to the corresponding server. But if you open this file on your computer, you will quickly find that the directory of all Internet sites is not stored there. There may be just a couple of lines and nothing more. Your system always checks the hosts file first, and anything that is not specified in it will be found on the DNS servers specified in your network settings (usually your ISP's).


But this also means that you can use the Hosts file to add addresses to it that cannot be provided by DNS servers (for example, it could be aliases for various locations on your home network, which would otherwise only be available if if you have your own DNS server installed and configured on your local network), or to bypass the IP addresses provided by your default DNS servers.

For example, if you ask your browser to open facebook.com, the DNS servers will return Facebook's IP address to your computer. But if you suddenly want to block Facebook on your computer, you can add an entry to the hosts file that will tell your computer that all requests to facebook.com need to be redirected to some other IP address that is different from the real IP address. Facebook. To block this social network, you can simply write the translation of requests on facebook.com to the IP address 127.0.0.1, which will always return to your system. There is a lot you can do with this file, and this is just the simplest example - it all depends on your wishes and needs.

How to edit the hosts file

On Linux operating systems, you can find the hosts file in / etc / hosts. Since this is a regular text file, you can just use a regular text editor(it doesn't matter if it is terminal or graphical). Unfortunately, there is only one graphical tool that provides partial management of the hosts file, and that is Linux Mint's Domain Blocker app.

This program works by adding entries to your hosts file that redirect the domains you specify to 127.0.0.1 - that is, to your computer. Either way, this is all you can do with Domain Blocker - all other uses of the hosts file require you to manually edit the file with a text editor.


Since hosts is system file, you will need administrative rights to save changes to this file. However, you can edit it with a terminal command such as sudo nano / etc / hosts or gksu gedit / etc / hosts. You need to use a terminal so that you can run the required application with administrative rights... You can also replace nano or gedit with a command to launch your favorite text editor. When you're done editing the file in nano, you can press Ctrl + X and then y to confirm overwriting changes

How to understand the format of the contents of the hosts file?

In the hosts file, each entry is on its own line. For the purposes of this article, you can use a very simple syntax - you type the IP address to which you want to redirect the hostname (domain), then the tab character and the actual domain. For example, to block Facebook, you can enter 127.0.0.1facebook.com, where is pressing the Tab key on your keyboard. You can also look at the screenshot and compare it with what you have in the file to make sure the input is correct.


Let's give another example of using this feature. For example, if you have a computer on your home network (let's say its IP address is 192.168.1.10) running a simple website that does something useful for you. You can enter the following into the hosts file: 192.168.1.10homeserver. Now, if you open your browser and just type http: // homeserver into the address bar, it will automatically display the contents of the host 192.168.1.10 to you. It's much easier than typing the IP address every time.

Potential problems

The only problem I came across is related to the operation of the Chrome browser. It tends to ignore the hosts file unless you do at least one of two possible things:

  • put http: // in front of each address. For example, if you have Facebook blocked via the hosts file, Chrome will bypass the block if you simply enter facebook.com into the address bar. If you enter

The DNS domain name system is used to determine which IP belongs to the correct domain on the Internet. When any program needs to access a site by its domain name, the operating system sends a request to the DNS server to find out which IP it should use to send packets further. But this is not always the case. For example, when we access the localhost domain, the request is always sent to our local computer.

The whole reason is the hosts file. If you've used Windows before, you've probably already heard of such a file. There it was most often used to quickly block access to a resource. But its application is much broader. In this article, we will look at how to set up the hosts file in Linux, as well as what features it provides us.

Before we move on to the hosts file itself, we need to understand how the IP address lookup for a domain name is performed in Linux. I said that the operating system immediately sends a request to the DNS server, but this is not entirely true. There is a specific search order according to which it is performed. This order is set in the configuration file /etc/nsswitch.conf

cat /etc/nsswitch.conf

Here we are interested in the hosts line. It lists in order of priority the services that are used to find the IP address for the domain name. The files item means to use the / etc / hosts file, and dns means the Internet domain name service. If files is located before hosts, this means that the system will first try to find the domain in / etc / hosts, and only then by DNS. This is the case by default.

Configuring the hosts file in Linux

The file we need is located in the / etc / directory. To open it, you can use any text editor, both in the command line and in the graphical interface, only you need to open it with superuser rights. For example with vim:

sudo vi / etc / hosts

sudo gedit / etc / hosts

The file syntax is pretty straightforward. It contains several lines with domain names and the IP addresses to use for them. Each of them looks like this:

ip_address domain alias

Usually the first line creates a rule to redirect all requests to the localhost domain to the local IP address - 127.0.0.1:

127.0.0.1 localhost

This file also contains redirects for your computer name and for IPv6 addresses by default. You can create your own settings for any desired domain. To do this, add a line to the end of the file..0.0.1:

127.0.0.1 site

Please note that only the domain is indicated here, no protocol. You don't need to specify the http or https prefix, otherwise nothing will work. But for the www subdomain, you need to create a separate entry or write it down as an alias. For example:

127.0.0..site

Now, when requesting a domain, the site will open our local IP. To return access to the original resource, just remove the added line. But you can use not only the local address, but any other. This is very convenient if you have just registered a domain and the domain zone has not yet been updated, and you already want to work with a new site. Just add data to / etc / hosts and work as usual.

conclusions

In this short article, we have covered how DNS is configured through the Linux hosts file. As you can see, with its help you can block access to unwanted resources, for example, to which programs should not get access, and also use it in the work of the webmaster.

about the author

Founder and site administrator, I am fond of open source software and the Linux operating system. I am currently using Ubuntu as my main OS. In addition to Linux, I am interested in everything related to information technology and modern science.

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 With DynDNS gone, here are the best free dynamic DNS providers, services, and DDNS alternatives to replace it.(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 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):

127.0.0.1 wikipedia.org

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.

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:

192.168.1.10 homeserver

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:

  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 wikipedia.com into the address bar. If you type http://wikipedia.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. This is one of Using a Chromebook, but concerned about privacy? Tweak these 7 settings in the Chrome browser on Chrome OS to stay secure online. worth doing anyway.

TITLE
hosts - static table for converting IP addresses to
machine names

SYNTAX
/ etc / hosts

DESCRIPTION
This page contains a description of the file format
/ etc / hosts. This file contains text describing
correspondence of IP addresses and machine names (one address in
line). For each machine, one line should
the following information be present:

IP_address canonical_name aliases

The fields of this record are separated from each other by spaces and / or
tabs. Text starting with the "#" character to the end
lines are considered a comment and ignored. Machine names
can contain any printable character except the separator
fields, newline character, or comment character.
Aliases are modified, alternative,
shortened or generalized forms of machine names (for example,
localhost). The format of the hostname table is described in RFC 952.

Berkeley Internet Domain Name Server (BIND) contains
Internet name server for UNIX machines. It replaces the file
/ etc / hosts or a machine finder and frees up the machine
from the need for complete detailed filling of the file
/ etc / hosts.

Despite the fact that the functions of this table have been performed for a long time
DNS, it is still used for

Boot system.
Many systems have a small table of names
machines containing information about names and addresses
important machines on the local network. This is useful in that
if DNS does not work, for example, if
system boot.

NIS NIS sites use the name table
machines as a source of information for the base
of these NIS machines. Although NIS may
work with DNS, many NIS sites use
machine name table with strings for all machines
local network as a safety net.

Isolated nodes
Small sites not connected to the global network,
use a hostname table instead of DNS. If
local information rarely changes, and the network does not
connected to the internet, DNS is hardly necessary.

EXAMPLE
127.0.0.1 localhost
192.168.1.10 foo.mydomain.org foo
192.168.1.13 bar.mydomain.org bar
216.234.231.5 master.debian.org master
205.230.163.103 www.opensource.org

HISTORICAL REFERENCE
Before the introduction of DNS, the machine name table was the only one
a means to convert machine names to addresses in
growing internet network. In fact, this file
was created on the basis of the official database of cars,
maintained in the Network Information Control Center
(NIC) and also based on local fixes containing
unofficial aliases and / or information about unknowns
cars. NIC no longer supports hosts.txt files,
however, at the time of this writing (circa 2000)
there were old hosts.txt files on WWW. They were
found; the dates of their placement are 92, 94 and 95.

I want to tell you about a few non-standard way with which you can open sites in the browser using short names. For example, instead of typing “yandex.ru” in the browser, you can simply type one letter “y”. The / etc / hosts file will help us with this.

/ Etc / hosts file

The / etc / hosts file sets the correspondence between the IP address and the hostname (hostname, domain), as well as their aliases (also called synonyms, aliases). When you access any address on the network, for example, http://yandex.ru, then in order to access the site, the system needs to first convert the site address into the corresponding IP address, for this the system asks for the IP address from DNS servers... If a correspondence between yandex.ru and an IP address is written in the / etc / hosts file, then the system will use this particular IP address. And since we can also specify an alias in the / etc / hosts file by which a specific IP address will be available, we can specify, for example, for yandex.ru the alias "y". Accordingly, when you ask "y" in your browser, the system will read the IP address in the / etc / hosts file and open the yandex.ru website. That is, / etc / hosts is a kind of local DNS server.

How to find out the IP address of a site

Now we need to determine the IP addresses of the sites that we want to add to / etc / hosts. It should be noted that not all sites have a separate dedicated IP address and it is impossible to access them only by IP address. But almost all large sites and portals have their own dedicated IP address. An easy way to find out the IP address of a site is to use the ping command:

Ping site-url.

For example, let's run:

ping yandex.ru

The ping process will be displayed on the screen (to abort press Ctrl + C). So, we get the output of the ping command in the form:

PING yandex.ru (87.250.250.11) 56 (84) bytes of data. 64 bytes from yandex.ru (87.250.250.11): icmp_seq = 1 ttl = 57 time = 3.57 ms ...

Here you can see that yandex.ru corresponds to the IP address 87.250.250.11. Similarly, we perform for other sites.

Editing the / etc / hosts file

You need to be careful when editing the / etc / hosts file so as not to spoil anything, as it is a system file and it is important not to break its syntax. Open the file for editing, to do this, run in the command line:

Sudo gedit / etc / hosts

The content of the file looks something like this:

127.0.0.1 localhost :: 1 mycomp localhost6.localdomain6 localhost6 127.0.1.1 mycomp # The following lines are desirable for IPv6 capable hosts :: 1 localhost ip6-localhost ip6-loopback fe00 :: 0 ip6-localnet ...

The syntax for the / etc / hosts file is very simple:

IP address hostname [aliases, ...]

Let's add new lines to the file for different sites:

74.125.232.19 google.ru g 94.100.191.203 mail.ru m 194.186.36.211 rbc.ru r 87.250.250.11 yandex.ru y

Save and close the file. We have registered the correspondence between IP addresses, site names and their aliases. Now go to the browser and in the address bar enter simply "g", the google.ru site should open. To open mail.ru just enter "m", for rbc.ru - just "r", yandex.ru - "y".