Linux Basics : Part13 : Local DNS

We all have come across scenarios where we have a bunch of local machines on which we are learning or working and we want them to be known by their hostname on the network. For this purpose, it is not possible to have these hostnames or domains hosted out the network.

So to achieve this one possible way is to host a separate DNS server on the network and make entries of the hostnames within it and point them to their respective IP addresses.

This is feasible when you have different set of servers and you are hosting multiple local domains for different purposes, however just for some servers may be in a lab environment or may be in an isolated internal environment this approach may seem a bit overkill. This is where you can take the second approach and configure a local DNS using the “/etc/hosts” file.

This file provides the facility to make entries of an IP against its FQDN or hostname and its alias if required.

If you just installed Linux and check out the contents of this file it will have some default entries for the local host ipv4 and ipv6.

#cat /etc/hosts
127.0.0.1 localhost

You may as well rename the localhost to something else like the hostname of the machine, but it is not recommended as this is used by the system for a lot of internal operations.

Instead it is better to just add additional names in a space separated format as mentioned below:

127.0.0.1 localhost machine1

Similarly, if you want to make additional entries for the machines own IP or other machines on the network you may populate the file as below:

127.0.0.1 localhost machine1
172.16.1.3 machine1.example.com machine1
172.16.1.4 machine2.example.com machine2
172.16.1.5 machine3.example.com machine3

Now these entries act as the local DNS and you will be able to resolve these machines by their name as you have mentioned above.

When you try to resolve a hostname suppose machine1, the query will first check the “/etc/nsswitch” file.
This file has a list of preferences for name server look up. So within this file you will find a line for hosts specifying the preference for DNS lookups.

#cat /etc/nsswitch.conf | grep hosts:
hosts: files dns

The above entry means that whenever there is a query for DNS, first check the files for any matching entry and if the entry is found resolve it from there and if the entry is not found then make an actual dns lookup from a DNS server.

You can very well modify the settings of this file as per your convenience but it is not recommended.

So re-iterating what we did above, when you now try to ping machine1 with its hostname or the name mentioned in the hosts file, your query will check the nsswitch file and check the “/etc/hosts” file first for any matching entry.
If it finds an entry which it will because you have made an entry for the same, it will respond back with the IP.

Now if you try to ping machine4, your query will check the nsswitch file and check the “/etc/hosts” file first for any matching entry, but this time it will not find an entry and it will delegate the request to “/etc/resolv.conf” where it will check the IP of the DNS server and then it will query that DNS server for IP of machine4.

The hosts file is a very important file when configuring OS HA clusters and Kubernetes clusters because a lot of checks and HA functionality depends on it.
So take time to understand the type of environment you are trying to setup and accordingly decide how you want the resolution to happen. Will the host file suffice or you need to setup a full-fledged DNS server and then plan and make the configuration.

That’s it for this post, see you soon in the next post of blog.avoidingtech.com.

Leave a Comment

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

error: Content is protected !!