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.
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 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.
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 18.104.22.168 nameserver 22.214.171.124
Above you can see “nameserver” directive which is pointed to the IP address of Name Server.
In my example 126.96.36.199 and 188.8.131.52 is my own Name Servers.
You can also use Public Name Server following below configuration.
# cat /etc/resolv.conf nameserver 184.108.40.206 nameserver 220.127.116.11 nameserver 18.104.22.168
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.