Kahibaro
Discord Login Register

DNS and hostnames

Understanding Hostnames on Linux

On a Linux system, the hostname is the human‑readable name that identifies a machine on a network. It’s used by DNS, monitoring systems, SSH, prompts, and logs.

There are three closely related concepts:

On a typical Linux system:

Viewing the Current Hostname

Key commands:

  hostname
  hostname -f
  hostnamectl

If hostname -f fails or returns something unexpected, it often means the system’s hostname or name resolution configuration is incomplete.

Setting the Hostname (systemd-based systems)

Most modern distributions use systemd, so the recommended tool is hostnamectl.

  sudo hostnamectl set-hostname web01.example.com

This sets the static hostname to web01.example.com. The short hostname will be web01.

  sudo hostnamectl set-hostname "Web Server 01" --pretty
  sudo hostnamectl set-hostname temp-host --transient

On older non‑systemd systems, hostnames may be set in files such as /etc/hostname or /etc/sysconfig/network, but the exact details are distribution‑specific and usually covered in distro documentation.

Hostnames and `/etc/hostname`

On many distributions, the static hostname is stored in /etc/hostname as a single line:

web01.example.com

To change it manually:

  1. Edit the file:
   sudo nano /etc/hostname
  1. Put the desired hostname (short or FQDN) on a single line.
  2. Apply without reboot:
   sudo hostname "$(cat /etc/hostname)"

or simply reboot.

Whenever possible, prefer hostnamectl instead of editing files directly, as it updates all relevant locations.

Name Resolution on Linux

When a program needs to translate a hostname (like web01.example.com) to an IP address, Linux uses the name service switch configuration in /etc/nsswitch.conf. A typical line:

hosts: files dns

This means:

  1. Look in local files (e.g. /etc/hosts)
  2. If not found, query DNS

Other sources (like mdns, ldap, wins) can be added, but in many setups files and dns are enough.

The `/etc/hosts` File

/etc/hosts provides a simple, static way to map hostnames to IP addresses without using DNS.

Example content:

127.0.0.1   localhost
127.0.1.1   web01 web01.example.com
192.0.2.10  db01 db01.example.com
203.0.113.5 backup backup.example.com

Key points:

Use /etc/hosts for:

DNS Configuration Files

For DNS lookups, most systems use a resolver library which reads configuration from:

`/etc/resolv.conf` Basics

A minimal resolv.conf might look like:

nameserver 1.1.1.1
nameserver 9.9.9.9
search example.com internal.example.com

Directives:

Many systems do not want you to edit /etc/resolv.conf directly, because it’s managed by:

Edits may be overwritten when the network restarts. The correct way to set DNS servers often depends on your network manager (GUI, nmcli, netplan, or distro-specific tools).

systemd-resolved and Stub Resolvers

On distributions using systemd-resolved, /etc/resolv.conf may be a symlink to something like /run/systemd/resolve/stub-resolv.conf. In that case, configure DNS via:

systemd-resolve --status
sudo resolvectl status          # newer syntax

And change settings via NetworkManager, netplan, or resolved configuration, instead of directly editing resolv.conf.

How DNS Names Map to IPs

DNS provides a hierarchical, distributed database mapping names to IP addresses and other information.

Common record types:

On a client system, you mainly care about:

Forward vs Reverse DNS

Reverse DNS is useful for:

Note that forward and reverse DNS are separate; having one doesn’t guarantee the other is configured.

DNS and Hostnames in Practice

Choosing Hostnames

Guidelines for hostnames (especially for servers):

For internal-only networks, you can use a private domain such as corp.example.com or another domain you control.

How a Typical Lookup Works

When you run:

ping web01

The system roughly does:

  1. Check /etc/nsswitch.conf to see hosts order.
  2. If files comes first, search /etc/hosts for web01.
  3. If not found and dns is listed, apply search domains from /etc/resolv.conf:
    • Try web01.example.com
    • Try web01.internal.example.com
  4. Query the configured nameserver addresses for each candidate until a match is found or all fail.

Tools like getent hosts web01 use the same NSS mechanism, so they’re good for debugging.

Testing and Debugging DNS/Hostname Problems

Common CLI tools:

Examples:

  host web01.example.com
  dig web01.example.com
  host 203.0.113.10
  dig -x 203.0.113.10
  getent hosts web01

Common Issues

Integrating Hostnames with DNS

While running a full DNS server is covered elsewhere, from a client/admin perspective you should understand how hostnames and DNS relate:

      127.0.0.1   localhost
      203.0.113.10 web01.example.com web01

This alignment ensures:

Summary

Views: 21

Comments

Please login to add a comment.

Don't have an account? Register now!