Skip to content

Linux hostname and FQDN — what every utility expects

A misconfigured hostname breaks mail delivery, log forwarding, and TLS hostname verification — quietly. Here is what hostname and FQDN actually mean to every tool on the box.

The hostname is one of those settings that looks trivial — one line, one command — but is wired into surprisingly many places. Mail (Postfix) refuses to start without an FQDN. Cluster software (etcd, Consul, Patroni) registers nodes by hostname. Log forwarding labels every line with the hostname. TLS certificates issued by cert-manager or by Let’s Encrypt’s DNS-01 check expect a hostname that matches DNS. Getting it right once at provisioning saves a long tail of confusing failures. Here is the model and the procedure.

How to verify

Inventory what the host currently thinks its name is.

hostname
hostname --short
hostname --fqdn
hostname --all-fqdns
hostnamectl
cat /etc/hostname
grep -E '^[^#]*\b127' /etc/hosts
getent hosts $(hostname)

hostname returns the kernel hostname (short form). hostname --fqdn resolves it against /etc/hosts and DNS to find a fully qualified name. hostnamectl is the systemd-era unified view. The three commonly diverge — that is where the bugs live.

What’s happening

Linux distinguishes three related concepts:

  • Kernel hostname. What hostname returns and gethostname(2) provides. A single label, no dots traditionally; modern kernels accept dots but most tools assume the short form.
  • Pretty hostname. A free-form display string (hostnamectl --pretty). Optional, for human display.
  • FQDN. Fully Qualified Domain Name — host.domain.tld. Not a kernel attribute; resolved by name service (/etc/hosts, DNS) from the short kernel name.

When an application asks for “the hostname,” the question is ambiguous. Postfix wants the FQDN. journald uses the short name. TLS verification compares the certificate CN/SAN to whatever the client requested (so to the name used in the URL, not to a system attribute).

The kernel hostname is set at boot from /etc/hostname. The FQDN comes from name resolution: the kernel hostname is passed through the system’s name service switch (nsswitch.conf), which by default consults /etc/hosts first, then DNS. If /etc/hosts has a line like 127.0.1.1 web-01.prod.example.net web-01, then hostname --fqdn will report web-01.prod.example.net. If /etc/hosts only has 127.0.1.1 web-01, the FQDN resolution falls through to DNS, which may or may not return what you want.

The 127.0.1.1 (not 127.0.0.1) is a Debian convention: it gives the hostname a loopback IP without colliding with localhost. On RHEL-family distros the convention is to use 127.0.0.1 hostname.fqdn hostname directly.

The procedure

  1. Decide the canonical name. For Stack Harbor’s standard, that is <role>-<index>.<environment>.<domain> — e.g. web-01.prod.example.net. Short and FQDN both derived from the same source of truth.

  2. Set the kernel hostname. hostnamectl writes /etc/hostname and applies the kernel setting in one step.

    sudo hostnamectl set-hostname web-01.prod.example.net
    hostnamectl status

    You can pass --static, --pretty, --transient to set them separately; default sets the static.

  3. Update /etc/hosts. The kernel hostname must resolve to an IP. Without this, every tool that does a forward-then-reverse lookup of its own name will fail.

    sudo sed -i.bak '/^127\.0\.1\.1/d' /etc/hosts
    echo "127.0.1.1 web-01.prod.example.net web-01" | sudo tee -a /etc/hosts

    Order matters: the FQDN should appear before the short name on the line. Tools like hostname --fqdn return the first name on the line, which they treat as canonical.

  4. Verify all three views agree.

    hostname
    # web-01.prod.example.net
    hostname --short
    # web-01
    hostname --fqdn
    # web-01.prod.example.net
    hostnamectl --static
    # web-01.prod.example.net

    If hostname --short returns the FQDN (because the kernel name has dots), that confuses some tools (Postfix in particular). The cleanest answer is to set the kernel hostname to the short form and let /etc/hosts provide the FQDN:

    sudo hostnamectl set-hostname web-01
    echo "127.0.1.1 web-01.prod.example.net web-01" | sudo tee /etc/hosts

    The choice between “kernel = FQDN” and “kernel = short” is a fleet convention; either is workable. We standardize on “kernel = short, FQDN in /etc/hosts” because it matches what most distros expect.

  5. For cloud instances, override cloud-init. Cloud-init usually sets the hostname at first boot, and may re-set it on every reboot if not pinned. Edit /etc/cloud/cloud.cfg.d/:

    sudo tee /etc/cloud/cloud.cfg.d/99-hostname.cfg > /dev/null <<'EOF'
    preserve_hostname: true
    manage_etc_hosts: false
    EOF

    This stops cloud-init from clobbering your changes on reboot.

  6. Update services that record the hostname at startup. Some services cache the hostname; restart them after the change.

    sudo systemctl restart rsyslog systemd-journald postfix 2>/dev/null

    journalctl --since=now will show new entries tagged with the new hostname.

  7. Verify reverse DNS matches if it matters. For mail-sending hosts, the IP must reverse-resolve to a name that matches the FQDN. From your workstation:

    dig +short -x 203.0.113.10
    # web-01.prod.example.net.

    If reverse DNS does not match, mail delivery to receivers that check (most major providers) will be deferred or rejected.

  8. Document the convention in your provisioning code. This is one of those settings that gets messy when individual operators pick names by feel. The Ansible/Terraform module that brings up the host should compute the hostname from a structured input:

    - name: Set hostname
      ansible.builtin.hostname:
        name: "{{ inventory_hostname }}"
    - name: Ensure /etc/hosts has the FQDN
      ansible.builtin.lineinfile:
        path: /etc/hosts
        regexp: '^127\.0\.1\.1'
        line: "127.0.1.1 {{ inventory_hostname }}.{{ env }}.{{ domain }} {{ inventory_hostname }}"

Common pitfalls

  • Setting hostnamectl set-hostname web-01.prod but /etc/hosts still has 127.0.1.1 web-01.old.example.net web-01, leaving the FQDN stale.
  • Postfix refusing to start with “fatal: parameter inet_interfaces: no local interface found for ::1” — usually a /etc/hosts that lost the IPv6 localhost line.
  • Cloud-init resetting the hostname on every reboot because preserve_hostname is not set.
  • TLS hostname verification failing because the certificate is for web-01.prod.example.net and the client connected to the IP — never to the hostname.
  • A trailing dot in /etc/hostname (web-01.) — some tools accept it, others choke. Strip the dot.

In our fleets, hostname and FQDN are computed deterministically from the inventory and applied by configuration management — never set by hand. The result is that monitoring, log forwarding, mail, and TLS all agree on what to call each host. That uniformity is part of the Managed Operations provisioning floor.