Linux Administrator

/etc/hosts vs. /etc/resolv.conf in Linux

Whenever any applications performs DNS Lookup in Linux operating system it looks in both “/etc/hosts” and “/etc/resovl.conf” configuration files to resolve the DNS name.  In Linux for DNS lookup order it use “/etc/nsswitch.conf” file. There are two library libc and glibc  are used to perform this operations in Linux , by defining DNS Lookup orders.

What is DNS ?

DNS ( Domain Name System) is mechanism to make internet human friendly.  Computer communicate with each other using their IP addresses.  There are lots of IP address in internet world, and  It is very difficult to remember all IP address . To solve this issue and make more human friendly DNS invented. IP address map with host-name in DNS server.

When we enter a domain name into our browser like  computer find our nearest DNS server and ask what is the correct IP address for Than it returns the IP address to our system to communicate with

/etc/nsswitch.conf file

In /etc/nsswitch.conf file there is a database for DNS lookup called “hosts”.

#cat /etc/nsswitch.conf | grep "hosts"

hosts:      files dns

Above you can see DNS lookup refers to /etc/hosts/ and /etc/resolv.conf. First DNS lookup look into /etc/hosts file to resove name and than it will look into /etc/resolv.conf file to resolve the host-name.

Please note :- DNS lookup toots like nslookup and dig will ignore /etc/nsswitch.conf file and always refer to DNS server using /etc/resolv.conf.

/etc/hosts file

/etc/hosts file is the most important file in Linux operating system. It is a text file for name resolution.

# cat /etc/hosts   localhost localhost.localdomain localhost4 localhost4.localdomain4

::1         localhost localhost.localdomain localhost6 localhost6.localdomain6  client01  client02

Both fields are separated by space or tab followed by IP and Host-name per line. Host-name contain only alphanumeric characters, minus sign (-) and period (.) .

Fields Explanations :  : IP address

client02      :  Host-name : Aliases of IP address

You can also see other entries in /etc/hosts file like is loopback addresses. Which is pointed to “localhost” host-name.

/etc/resolv.conf file

This is the another important file in Linux operating system. It contains information that help to computer to convert domain name to its IP address . All process called resolving.

# cat /etc/resolv.conf



Above you can see “nameserver” directive which is pointed to the IP address of Name Server.

In my example and is my own Name Servers.

You can also use Public Name Server following below configuration.

# cat /etc/resolv.conf



I hope this article will be helpful to understand “/etc/host” and “/etc/resolv.conf” file in Linux. If you have any queries and problem please comment in comment section or you can also ask your question.


Thank you! for visiting LookLinux.

If you find this tutorial helpful please share with your friends to keep it alive. For more helpful topic browse my website To become an author at LookLinux Submit Article. Stay connected to Facebook.

About the author


Santosh Prasad

Hi! I'm Santosh and I'm here to post some cool article for you. If you have any query and suggestion please comment in comment section.

1 Comment

  • Hello Dydge,
    Yes it is normal, you can use /etc/hosts file to resolve server name if DNS server is not available in the local network or if it is down. If DNS working fine than use it to resolve server name.

Leave a Comment