How to Configure Local Hostname Resolution on Ubuntu Using /etc/hosts.

Learn how to fix the “Temporary failure in name resolution” error on Ubuntu by configuring local hostname resolution. Understand `/etc/nsswitch.conf`, explore mDNS, and use `/etc/hosts` to resolve computer names to IP addresses on your LAN without running a local DNS server.

How to Configure Local Hostname Resolution on Ubuntu Using /etc/hosts.
Image credit: Unknown. Please contact us if you are the owner.


Resolving a computer's hostname to an IP address is known as name resolution. On the Internet, this process is typically handled by the Domain Name System (DNS), which translates domain names into IP addresses. DNS servers make it possible to access network resources using names rather than remembering numerical addresses.

On a local area network (LAN), however, you don't necessarily need to install and configure a dedicated DNS server to resolve the names of your computers. Ubuntu provides several ways to handle local hostname resolution, including the /etc/hosts file and Multicast DNS (mDNS).

In this tutorial, we'll explore how hostname resolution works on Ubuntu and how to configure local hostname mappings to access other machines on your network by name.

The Problem: Temporary Failure in Name Resolution

We encountered the following error when trying to reach another computer on our LAN using its hostname:

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

This error indicates that the system could not resolve BaTman to an IP address. It doesn't necessarily mean the computer is offline; the problem may simply be that Ubuntu doesn't know how to translate the hostname into the correct address.

Before installing additional software or setting up a local DNS server, let's examine how Ubuntu handles hostname resolution.

Important: For reliable local hostname mappings, use stable IP addresses. Static IP configuration is one option, but DHCP reservations on your router can also ensure that a device consistently receives the same address.

Understanding Name Resolution in Linux

Linux can resolve hostnames using several sources, including:

  • /etc/hosts: A local file containing manually configured hostname-to-IP mappings.
  • DNS: A DNS server that resolves names using its configured records.
  • mDNS: A protocol that allows devices on a local network to discover and resolve hostnames without a conventional DNS server.

The order in which Linux consults these sources is controlled by the Name Service Switch (NSS) configuration file:

$ cat /etc/nsswitch.conf

Look for the line beginning with hosts.

For example:

hosts:          files mdns4_minimal [NOTFOUND=return] dns mdns4

This configuration specifies the sources used for hostname lookups and the order in which they are consulted.

  • files: Checks local hostname mappings in /etc/hosts.
  • mdns4_minimal: Attempts to resolve eligible local names using IPv4 mDNS.
  • [NOTFOUND=return]: Stops processing if the preceding service returns a qualifying NOTFOUND result.
  • dns: Queries the configured DNS resolver.
  • mdns4: Provides another IPv4 mDNS lookup method.

The exact configuration may differ between Ubuntu releases and installations.

For our example, we'll use the files source to define explicit hostname mappings. This approach is straightforward and doesn't require a dedicated DNS server.

What Is Multicast DNS (mDNS)?

Multicast DNS (mDNS) allows devices on the same local network to resolve certain hostnames without relying on a conventional DNS server.

The protocol is defined in RFC 6762 and uses UDP port 5353. It is commonly associated with Apple Bonjour and Avahi on Linux.

mDNS is useful for discovering devices on small networks, especially when their names end in .local.

On Ubuntu, the libnss-mdns package provides NSS integration for mDNS lookups:

$ sudo apt update
$ sudo apt install libnss-mdns

Depending on your Ubuntu version and network configuration, you may also need an mDNS responder such as Avahi:

$ sudo apt install avahi-daemon

Installing these packages is not always necessary. If you only need to resolve a few computers with known IP addresses, configuring /etc/hosts may be simpler.

Note: mDNS is not a replacement for conventional DNS in every environment. It is generally intended for local-link discovery and name resolution, and its behavior depends on network configuration and firewall rules.

Configure Local Hostname Resolution Using /etc/hosts

The /etc/hosts file allows you to associate IP addresses with hostnames directly on your Ubuntu system.

When an application requests a hostname lookup, Linux can consult this file according to the order defined in /etc/nsswitch.conf.

To edit the file, run:

$ sudo nano /etc/hosts

Add an entry for each computer you want to reach by name. For example:

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

Replace these example IP addresses with the actual addresses of your devices.

Each entry follows this format:

IP_ADDRESS    HOSTNAME    [OPTIONAL_ALIAS ...]

For example:

192.168.1.128   BaTman

This tells your Ubuntu system to resolve BaTman to 192.168.1.128.

The mapping applies only to the machine on which you edit /etc/hosts. If you want several computers to resolve these names, you must configure each system separately or use a shared DNS service.

Important: Keep the existing localhost entry. Do not replace it with your computer's hostname. Your system's own hostname and its corresponding IP address should be configured appropriately for your environment.

Save the file and exit the editor.

Test Hostname Resolution

Once you've saved the changes, test the hostname:

$ ping -c 3 BaTman

If the mapping is correct and the target computer is reachable, you should see output similar to this:

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

The response confirms that the hostname was resolved to 192.168.1.128 and that the target responded to the ICMP requests.

If you want to test name resolution without relying on ping or ICMP connectivity, use:

$ getent hosts BaTman

Expected output:

192.168.1.128   BaTman

The getent command queries the system's configured name service sources, making it a useful way to verify that NSS can resolve the hostname.

If resolution still fails, verify the following:

  • The hostname is spelled correctly in /etc/hosts.
  • The IP address is correct.
  • The hosts entry in /etc/nsswitch.conf includes files.
  • The target device is reachable on the network.
  • No other network or name-resolution configuration is interfering with the lookup.

Configuring local hostname resolution on Ubuntu doesn't have to be complicated. For a small LAN with a few devices, the /etc/hosts file provides a simple way to map hostnames to IP addresses without running your own DNS server.

For automatic local discovery, mDNS is another option. For larger networks or environments with frequently changing devices, a properly configured DNS server or router-based DNS service may be easier to maintain.

The right solution depends on your network. Understand how Ubuntu resolves hostnames, choose the appropriate method, and verify the result with getent hosts before troubleshooting higher-level connectivity issues.


Thank you for reading.

Keep Us Caffeinated  ⦿ ⦿
Spotify Logo
↑
SNUBmonkey Join our readers