3 of 3

Network ConfigurationLesson 3 of 3

Host names and name resolution

Set a permanent host name, resolve names with /etc/hosts and DNS, and know which tool consults which source.

  • hostnamectl
  • hostname
  • /etc/hostname
  • /etc/hosts
  • /etc/nsswitch.conf
  • getent
  • dig
  • nmcli general hostname
  • bind-utils

40 min5 exercises

After this lesson you can

  • Set and verify the static host name so it survives a reboot.
  • Add entries to /etc/hosts and explain the order in which files and DNS are consulted.
  • Tell apart getent (what applications see) from dig (DNS only), and diagnose a name that fails to resolve.

The idea

The host name

A machine has up to three names, and hostnamectl shows them all:

NameWherePurpose
Static/etc/hostnameThe configured name, applied at boot. This is the one you set
TransientKernelCurrently in use; may come from DHCP or DNS when no static name is set
Pretty/etc/machine-infoFree-form display name

Use the fully qualified domain name (FQDN) as the static name: node1.lab.example. The short name is the part before the first dot.

sudo hostnamectl set-hostname node1.lab.example

That single command writes /etc/hostname, sets the live name, and survives reboots. Do not edit /etc/hostname and forget to apply it, and do not rely on hostname NAME, which changes only the running kernel’s name until reboot.

Turning names into addresses

Programs that need an address for a name call the C library, which follows /etc/nsswitch.conf. On the hosts: line the sources are consulted in order:

hosts:      files myhostname dns
  1. files: /etc/hosts
  2. dns: the servers listed in /etc/resolv.conf
  3. myhostname: the machine’s own name

/etc/hosts is a plain table: address, canonical name, aliases.

127.0.0.1       localhost localhost.localdomain
192.168.122.51  node2.lab.example node2

It works with no network or DNS at all, which makes it ideal for a lab, for a few critical servers, and for overriding DNS temporarily. /etc/resolv.conf is generated by NetworkManager from the connection: nameserver lines for DNS servers and search for the domains appended to short names.

Which tool uses which source

This causes real confusion:

ToolConsults
getent hosts, ping, ssh, curl, most programsnsswitch.conf order: files, then DNS
dig, host, nslookupDNS only, never /etc/hosts

So a name in /etc/hosts can ping perfectly and still show nothing in dig. Use getent hosts NAME to see what applications will see, and dig NAME to see what DNS says.

Try it

hostnamectl
 Static hostname: node1.lab.example
       Icon name: computer-vm
         Chassis: vm
      Machine ID: 3f1c0c3a6f4d4a1c8e2a7d0d9b0c5e11
         Boot ID: 7b2e0d9f0a1c4a45a86b1f31d1f3a2c0
  Virtualization: kvm
Operating System: Red Hat Enterprise Linux 10.0 (Coughlan)
          Kernel: Linux 6.12.0-55.el10.x86_64
    Architecture: x86-64
sudo hostnamectl set-hostname node1.lab.example
cat /etc/hostname
hostname -f
node1.lab.example
node1.lab.example

Your shell prompt still shows the old name until you open a new login shell.

A hosts entry

echo '192.168.122.51 node2.lab.example node2' | sudo tee -a /etc/hosts
getent hosts node2
ping -c1 node2
192.168.122.51  node2.lab.example node2
PING node2.lab.example (192.168.122.51) 56(84) bytes of data.
64 bytes from node2.lab.example (192.168.122.51): icmp_seq=1 ttl=64 time=0.412 ms

getent versus dig

grep '^hosts' /etc/nsswitch.conf
getent hosts node2.lab.example
dig +short node2.lab.example
hosts:      files myhostname dns
192.168.122.51  node2.lab.example node2

dig printed nothing: DNS does not know the name, and dig does not read /etc/hosts. The system can still use the name.

When names fail but addresses work

ping -c1 8.8.8.8
ping -c1 example.com
cat /etc/resolv.conf
1 packets transmitted, 1 received, 0% packet loss
ping: example.com: Temporary failure in name resolution
# Generated by NetworkManager
nameserver 192.0.2.1

The network is fine and the first nameserver is wrong or unreachable. Fix it where it is generated:

sudo nmcli connection modify "System eth0" ipv4.dns 192.168.122.1
sudo nmcli connection up "System eth0"
getent hosts example.com

Going further

hostname -I prints all addresses, and hostname -s the short name. nmcli general hostname reads (or sets) the name through NetworkManager. resolvectl is available when systemd-resolved is in use, which is not the RHEL default. host -t MX example.com and dig +short TXT example.com query specific record types. Reverse lookups (dig -x 192.168.122.51) go through PTR records, which /etc/hosts does not provide to dig. For many hosts use DNS, since keeping /etc/hosts files in sync across machines does not scale; but for a handful of cluster nodes it is simple, robust and independent of the DNS server.

Practice

0 of 5 done

Do these on your own system. They get harder. Check each result, then tick it off.

  1. Warm-up

    What is this machine called?

    Display the host name in three ways: the short one, the fully qualified one, and the full hostnamectl report. Say which file stores the permanent name.

    Check your result

    hostname, hostname -f and hostnamectl print names. /etc/hostname holds the static name.

    Hint

    hostnamectl shows static, transient and pretty names. cat /etc/hostname.

    Solution
    hostname
    hostname -f
    hostnamectl
    cat /etc/hostname
  2. Practice

    Set the permanent name

    Set the host name of this machine to node1.lab.example. Make sure it is still in place after a reboot, and that no separate edit of any file is needed. Do the same on the second machine with node2.lab.example.

    Check your result

    hostnamectl --static prints node1.lab.example. cat /etc/hostname shows the same. After a reboot the prompt and hostname show the new name.

    Hint

    sudo hostnamectl set-hostname node1.lab.example writes /etc/hostname and applies at once. Open a new shell to see it in the prompt.

    Solution
    sudo hostnamectl set-hostname node1.lab.example
    hostnamectl --static
    cat /etc/hostname
    hostname -f
  3. Practice

    Resolve a peer without DNS

    Add 192.168.122.51 node2.lab.example node2 to /etc/hosts on node1 and the matching line for node1 on node2. Verify with getent hosts and ping by both the short and the long name. Explain why the entry works even though no DNS server knows the name.

    Check your result

    getent hosts node2 prints 192.168.122.51 node2.lab.example node2. ping -c1 node2 succeeds.

    Hint

    Format: address, then the canonical name, then any aliases, separated by whitespace. Append, never overwrite, the existing localhost lines.

    Solution
    echo '192.168.122.51 node2.lab.example node2' | sudo tee -a /etc/hosts
    getent hosts node2
    getent hosts node2.lab.example
    ping -c1 node2
  4. Practice

    Why does ping work but dig does not?

    Run getent hosts node2 and dig +short node2.lab.example for the name you put in /etc/hosts. Explain the different results using /etc/nsswitch.conf. Install the DNS tools if needed.

    Check your result

    getent prints the address (it uses files first); dig prints nothing because it asks the DNS server directly and never reads /etc/hosts.

    Hint

    grep ^hosts /etc/nsswitch.conf shows the order. Package bind-utils provides dig, host and nslookup.

    Solution
    sudo dnf install -y bind-utils
    grep '^hosts' /etc/nsswitch.conf
    getent hosts node2.lab.example
    dig +short node2.lab.example
  5. Challenge

    Names fail but addresses work

    Make DNS fail on purpose by pointing the connection at an unused DNS server (for example 192.0.2.1). Show that ping 8.8.8.8 works while ping example.com fails, and that /etc/hosts entries still resolve. Repair it by setting the correct server on the connection, not in /etc/resolv.conf, and show the file updated.

    Check your result

    Broken: ping example.com gives Temporary failure in name resolution or times out. After nmcli connection modify ... ipv4.dns and up, getent hosts example.com works and /etc/resolv.conf shows the right nameserver.

    Hint

    Remember /etc/resolv.conf is generated by NetworkManager. Change ipv4.dns and reapply.

    Solution
    sudo nmcli connection modify "System eth0" ipv4.dns 192.0.2.1
    sudo nmcli connection up "System eth0"
    ping -c1 8.8.8.8
    ping -c1 example.com
    getent hosts node2
    sudo nmcli connection modify "System eth0" ipv4.dns 192.168.122.1
    sudo nmcli connection up "System eth0"
    cat /etc/resolv.conf
    getent hosts example.com