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

hosts-resolv-conf
mm
Written By:- Santosh Prasad

 

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 www.goole.com  computer find our nearest DNS server and ask what is the correct IP address for www.google.com. Than it returns the IP address to our system to communicate with www.google.com.

/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

127.0.0.1   localhost localhost.localdomain localhost4 localhost4.localdomain4

::1         localhost localhost.localdomain localhost6 localhost6.localdomain6

192.168.0.2  client01

192.168.0.3  client02 www.example.com

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 :

192.168.0.3  : IP address

client02      :  Host-name

www.example.com : Aliases of IP address

You can also see other entries in /etc/hosts file like 127.0.0.1 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

nameserver 173.204.4.5

nameserver 173.204.4.7

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

In my example 173.204.4.5 and 173.204.4.7 is my own Name Servers.

You can also use Public Name Server following below configuration.

# cat /etc/resolv.conf

nameserver 8.8.8.8
nameserver 8.8.4.4

nameserver 4.2.2.2

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.

Thanks:)

About Author

mm

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.

Other Post by Santosh Prasad

Visit All Post

Related Article

You may also Like

Leave a Comment

  • dydge

    Hello!
    My /etc/hosts file has only the IP address, 127.0.0.1 and not the private class C IP address (192.168.x.x). Is this normal?
    And can you tell me what are the best practices for resolving the server name, adding an entry with the private class C IP address in my / etc / hosts or left the resolution by the DNS?

    • 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.

Shares