How to resolve LAN hostnames on Ubuntu Server.

Although we are accustomed to using names (i.e, www.snubmonkey.com) when surfing the Internet, the Internet is pretty much based on numbers known as IP addresses.

How to resolve LAN hostnames on Ubuntu Server.

The process of resolving a computer name on a network into an IP address is known as name resolution and the system for providing name resolution for computers located on the Internet is called the DNS (Domain Name System or Service) and a server that provides such a service is called a DNS server. Name resolution is an important part of network communication because the logical host's names on the network must be resolved into their network addresses before communication can take place.

It is important to understand that you must set a Static IP Address in order for it to function.

Issue

In my case, I came across this frustrating error "Temporary failure in name resolution" when I tried to resolve a machine on my LAN.

$ ping -c 3 BaTman                                          ping: BaTman: Temporary failure in name resolution

To solve this issue, there is no need to install a DNS server in your LAN.
By default, Ubuntu Server should come with DNS resolving already installed and configured; if not, you should get and install avahi-daemon (or mdnsresponder) to provide local LAN DNS resolution.

$ sudo apt install libnss-mdns

We can use the mDNS (Multicast Domain Name System) protocol. This protocol is used to resolve hostnames in a small network (LAN). The mDNS service can be contacted using UDP queries over port 5353. The mDNS protocol is published as RFC6762 and implemented by the Apple Bonjour and avahi-daemon services.

Understanding Name Service Switch in Linux

Before moving any further, you should understand a few things about this file: /etc/nsswitch.conf. It provides Name Service Switch functionality which controls the order in which services are queried for name service lookups.

The configuration is based on order; if files is before dns it means the system will query the /etc/hosts file before checking DNS for name service requests. But if DNS is before files then the domain lookup process will consult DNS first before any other appropriate services or files.

$ sudo cat /etc/nsswitch.conf                                            
[sudo] password for snubmonkey: 


# /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 mdns4
networks:       files

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

netgroup:       nis

In this scenario, we want to query the “files” service. 


In this file, we are interested in the line where "hosts" is.

hosts:          files mdns4_minimal [NOTFOUND=return] dns mdns4

This means that files are tried first (this is the /etc/hosts file), then mDNS and only later the real DNS server is queried. mDNS is implemented using Avahi in Linux and is called Bonjour on Apple devices. In this scenario, we want to query the “files” service.

Configure DNS Locally Using /etc/hosts File

Now open the /etc/hosts file using your editor and simply enter a complete name for the machines' names their IP address, as shown below:

$ sudo nano /etc/hosts

INPUT

127.0.0.1 Snubmonkey
192.168.1.128 BaTman
192.168.1.9 Robin
192.168.1.139 CaTwoman.local
192.168.1.145 Jocker

Next, test if everything is working great as expected, using the ping command.

$ ping -c 3 BaTman                                             

PING BaTman (192.168.1.128) 56(84) bytes of data.
64 bytes from BaTman (192.168.1.128): icmp_seq=1 ttl=64 time=0.835 ms
64 bytes from BaTman (192.168.1.128): icmp_seq=2 ttl=64 time=0.400 ms
64 bytes from BaTman (192.168.1.128): icmp_seq=3 ttl=64 time=0.524 ms

--- BaTman ping statistics ---
3 packets transmitted, 3 received, 0% packet loss, time 2013ms
rtt min/avg/max/mdev = 0.400/0.586/0.835/0.182 ms


[...] and that’s all there is to it.
Thank you for reading.