[How To] configure /etc/hosts and /etc/nsswitch.conf file

The DNS (Domain Name System) is used to determine which IP belongs to the required domain on the Internet. When a program needs to access the site by its domain name, the operating system sends a request to the DNS server to find out which IP to forward packets to. But this is not true always. For example, when we access the localhost domain, the request is always sent to our local computer.

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

The content of the article

Domain Name Resolution in Linux

Before we go to the hosts file itself, you need to figure out how to find the IP address for a domain name in Linux. I said that the operating system immediately sends a request to the DNS server, but this is not quite so. There is a specific search order according to which it is performed. This order is set in the /etc/nsswitch.conf configuration file.

$ cat /etc/nsswitch.conf
# /etc/nsswitch.conf
# Example configuration of GNU Name Service Switch functionality.
# If you have the `glibc-doc-reference' and `info' packages installed, try:
# `info libc "Name Service Switch"' for information about this file.

passwd:         files systemd
group:          files systemd
shadow:         files
gshadow:        files

hosts:          files mdns4_minimal [NOTFOUND=return] dns
networks:       files

protocols:      db files
services:       db files
ethers:         db files
rpc:            db files

netgroup:       nis

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

[adinserter block=”6″]

Configuration of hosts file in Linux

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

$ sudo vim /etc/hosts localhost

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

IP_ADDRESS domain alias

Usually, the first line contains a rule for redirecting all requests to the localhost domain to the local IP address –

Also in this file, by default, redirects are placed for the name of your computer and for IPv6 addresses. You can create your own settings for any desired domain. To do this, add a line to the end of the file. For example, we will direct all requests to the linuxconfig.net domain on ip linuxconfig.net

Please note that only the domain is indicated here, without a protocol. You do not need to specify the http or https prefix, otherwise it will not work. But for the www subdomain, you need to create a separate entry or write it as an alias. For example: linuxconfig.net www.linuxconfig.net

Now when you request the domain linuxconfig.net, our local IP will open. To return access to the original resource, simply remove the added line. 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 the new site. Just add the data to /etc/ hosts and work as usual.


In this small article we covered the configuration of the DNS through the Linux hosts file. As you can see, with its help you can block access to undesirable resources, for example, to which programs should not get access.



Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.