Skip to content
Visual explainer 2h 30m to learn 5 steps

Hostnames, DNS resolution & time sync

Set the hostname, debug name resolution and keep time in sync with chrony.

By Invicly Tech, , in Linux Administration (RHCSA-style). About 9 minutes to read.

Topic 25 of 42

In short

Names are resolved in the order set by /etc/nsswitch.conf, usually /etc/hosts first and then DNS servers from /etc/resolv.conf. hostnamectl sets the machine's own name persistently. Accurate time matters for logs, TLS and Kerberos, so chronyd keeps the clock synchronised with NTP servers.

Explained simply

Computers talk using numbers (IP addresses), but people use names like db01 or example.com. Name resolution is how a server turns a name into an address. It can come from a small local file, /etc/hosts, or from a DNS server, which is like a phone book on the network.

Your own server also has a name, its hostname. It shows in your shell prompt, in logs, and in monitoring tools. You set it once with hostnamectl and it stays after a reboot.

Time sounds unrelated, but it is just as basic. If clocks drift apart, log lines from two servers no longer line up, TLS certificates can look expired or not yet valid, and login systems like Kerberos refuse access. So servers keep their clocks in sync with time servers using NTP, the Network Time Protocol.

On RHEL that job belongs to chronyd, the chrony service. You tell it which time servers to use, and it gently adjusts the clock so it stays accurate to within milliseconds.

Press Play on a diagram to walk through it one step at a time.

FlowResolving a hostname
  1. 1
    Application asks

    getaddrinfo("db01") via glibc.

  2. 2
    nsswitch.conf

    hosts: files dns sets the order.

  3. 3
    /etc/hosts

    Static entries win if present.

  4. 4
    resolv.conf

    Nameserver and search domains (systemd-resolved on Ubuntu).

  5. 5
    DNS server answers

    Address returned and possibly cached.

Comparegetent vs dig
getent hosts NAME
  • Follows nsswitch.conf
  • Includes /etc/hosts
  • Shows what apps really get
  • No TTL or server details
dig NAME
  • Asks DNS servers only
  • Ignores /etc/hosts
  • Shows TTL, status and server
  • Can query any record type and server

In depth

Static, transient and pretty hostnames

hostnamectl manages three names. The static hostname is stored in /etc/hostname and used at boot. The transient hostname is the one the kernel uses right now; DHCP may set it if no static name exists. The pretty hostname is a free-form label like Web server 1 for display only.

hostnamectl set-hostname web01.lab.example.com sets the static and transient names together, with no reboot. hostname -f prints the fully qualified name, which needs a matching entry in /etc/hosts or DNS. Many services read the hostname only at start, so restart them, or log out and back in to update your prompt.

A good habit is to add the server's own name to /etc/hosts next to its real IP address, so tools that look up their own name still work if DNS is down.

  • hostnamectl shows all three names
  • hostnamectl set-hostname NAME
  • hostname -f prints the full name

Where a lookup goes: nsswitch, resolv.conf and resolved

Most programs resolve names through the C library, which reads the hosts line in /etc/nsswitch.conf. With hosts: files dns, it checks /etc/hosts first and asks DNS only if the name is not there. RHEL adds myhostname, which always resolves the machine's own name.

For DNS, the library reads /etc/resolv.conf: nameserver lines (up to three are used), and search domains that are appended to short names. On RHEL, NetworkManager writes this file from your profiles.

Ubuntu uses systemd-resolved. /etc/resolv.conf is a link to a stub file that points to 127.0.0.53, a small local resolver that forwards queries to the real DNS servers and caches the answers. So cat /etc/resolv.conf shows only 127.0.0.53; resolvectl status shows the real servers per interface, and resolvectl flush-caches clears the cache.

SequenceLooking up db01 on Ubuntu with systemd-resolved
App
glibc
resolved 127.0.0.53
DNS server
  1. 1getaddrinfo("db01"), not in /etc/hosts
    App to glibc: getaddrinfo("db01"), not in /etc/hosts
  2. 2Query db01.lab.example.com
    glibc to resolved 127.0.0.53: Query db01.lab.example.com
  3. 3Not cached: forward the query
    resolved 127.0.0.53 to DNS server: Not cached: forward the query
  4. 4A 10.0.5.21, TTL 300
    DNS server to resolved 127.0.0.53: A 10.0.5.21, TTL 300
  5. 5Answer, now cached
    resolved 127.0.0.53 to glibc: Answer, now cached
  6. 610.0.5.21
    glibc to App: 10.0.5.21

Asking DNS directly with dig

getent hosts NAME and getent ahosts NAME resolve a name exactly as applications do, including /etc/hosts. dig talks only to DNS, which makes it the tool for checking what DNS itself says.

dig example.com shows the answer section, the TTL (how many seconds the answer may be cached) and which server replied. dig +short gives just the values. Add a type to ask for other records: AAAA for IPv6, MX for mail servers, TXT for text records, CNAME for aliases. dig @9.9.9.9 example.com asks a specific server, which tells you whether your own DNS server or the record itself is wrong. dig -x 10.0.5.21 does a reverse lookup to find the name for an address.

The status field matters: NXDOMAIN means the name does not exist, SERVFAIL means the server could not get an answer, and NOERROR with an empty answer means the name exists but has no record of that type.

  • dig +short example.com AAAA
  • dig @1.1.1.1 example.com
  • dig -x 10.0.5.21
  • host and nslookup are simpler alternatives

chrony: configuring and reading its status

chronyd reads /etc/chrony.conf. A server line names one NTP server; a pool line names a DNS name that resolves to several, such as RHEL's default pool 2.rhel.pool.ntp.org iburst. iburst sends a quick burst of requests at startup so the first sync happens in seconds, not minutes. makestep 1.0 3 lets chrony jump the clock if it is more than one second off during the first three updates; after that it only speeds up or slows down the clock slightly, which is safer for running programs. NTP uses UDP port 123.

chronyc sources lists the servers. In the second column, * marks the source you are synced to, + other good sources being combined, - acceptable ones not used, and ? unreachable ones. Reach is an octal record of the last eight polls: 377 means all eight answered. chronyc tracking shows the current offset. timedatectl shows the time zone and whether the clock is synchronized.

  • timedatectl set-timezone Europe/Berlin
  • timedatectl set-ntp true enables NTP sync
  • chronyc makestep forces an immediate correction

Learn it in 5 short steps

About 30m each. Track them with a timer in your plan.

  1. 1
    Set hostname with hostnamectl~30m
  2. 2
    Trace resolution via /etc/hosts and DNS~30m
  3. 3
    Query DNS with dig and getent~30m
  4. 4
    Configure chrony NTP servers~30m
  5. 5
    Check sync with chronyc sources~30m

Key points

  • getent hosts NAME tests resolution exactly as applications see it, including /etc/hosts.
  • dig queries DNS directly and bypasses /etc/hosts.
  • On RHEL, set DNS through nmcli rather than editing resolv.conf by hand.
  • Configure server or pool lines with iburst in /etc/chrony.conf, then restart chronyd.

Key terms

Tap a card to flip it. 0/5 known

Examples

bash
sudo hostnamectl set-hostname web01.lab.example.com
getent hosts web01
dig +short example.com A
echo "server classroom.example.com iburst" | sudo tee -a /etc/chrony.conf
sudo systemctl restart chronyd
chronyc sources -v
timedatectl

Hostname, resolution tests and NTP with chrony.

ping db01 works, but the app connects to the wrong server

After a database move, the app still reaches the old address. Step 1: getent shows 10.0.5.9, the old address. Step 2: dig shows 10.0.5.21, the new one. Different answers mean something before DNS is winning. Step 3: check nsswitch.conf order and /etc/hosts, where an old entry still points to 10.0.5.9. Step 4: remove it and resolve again. Restart the app too, because some runtimes cache DNS answers in memory.

bash
getent hosts db01
# 10.0.5.9   db01.lab.example.com db01
dig +short db01.lab.example.com
# 10.0.5.21
grep ^hosts /etc/nsswitch.conf
grep db01 /etc/hosts
sudo sed -i '/db01/d' /etc/hosts
getent hosts db01

Point chrony at the company time server

Security requires all servers to sync only with ntp.lab.example.com. Step 1: comment out the default pool line in /etc/chrony.conf. Step 2: add a server line with iburst. Step 3: restart chronyd. Step 4: check chronyc sources until the new server shows ^* and Reach climbs towards 377, and confirm with timedatectl.

bash
sudo sed -i 's/^pool /#pool /' /etc/chrony.conf
echo 'server ntp.lab.example.com iburst' | sudo tee -a /etc/chrony.conf
sudo systemctl restart chronyd
chronyc sources -v
# ^* ntp.lab.example.com   2   6   17   ...
chronyc tracking | grep -E 'Reference|System time'
timedatectl | grep -i synchronized

Quick reference

  • sudo hostnamectl set-hostname web01.lab.example.com sets the name permanently, no reboot
  • getent hosts NAME resolves like apps do (nsswitch, /etc/hosts, DNS)
  • dig +short NAME queries DNS only; dig @SERVER NAME asks one specific server
  • NXDOMAIN: name does not exist. SERVFAIL: the server could not answer
  • Ubuntu: resolvectl status shows the real DNS servers behind 127.0.0.53
  • chrony.conf: server HOST iburst or pool HOST iburst; restart chronyd after changes
  • chronyc sources: ^* is the synced source; Reach 377 means the last 8 polls answered

Quick quiz

Pick an answer to see if you're right and why.

Question 1

1/etc/hosts maps app01 to 10.0.0.5, and DNS says 10.0.0.9. With hosts: files dns, what does ping app01 use?

Question 2

2On Ubuntu, /etc/resolv.conf only lists nameserver 127.0.0.53. What is that?

Question 3

3In chronyc sources output, what does ^* mean?

Question 4

4What does dig return as status for a name that does not exist?

Question 5

5What does the iburst option on a chrony server line do?

0/5 answered

Practice exercises

Try each one yourself first. Use the hint if you get stuck, then compare with the solution.

  1. Exercise 1easy

    Show your hostname details, your time zone and whether the clock is synchronized.

  2. Exercise 2medium

    Find the IPv4 and IPv6 addresses and the mail servers of example.com, then check whether your DNS server and 9.9.9.9 give the same A record.

  3. Exercise 3hard

    Rename the server to app02.lab.example.com, make its own name resolve even without DNS, and confirm chrony is synced to at least one source.

Check yourself

Answer in your head, reveal, then be honest with yourself.

Feel confident about this topic?

Frequently asked questions

How do I change the hostname without rebooting?
Run sudo hostnamectl set-hostname NEWNAME. It takes effect immediately; open a new shell to see it in your prompt.
Why does nslookup work but my application cannot resolve the name?
nslookup and dig query DNS directly, while applications go through nsswitch.conf and /etc/hosts. Use getent hosts NAME to see what applications actually get.
Should I use chrony or systemd-timesyncd?
chrony is the RHEL default and a full NTP client and server with better accuracy and monitoring. systemd-timesyncd is a lighter, client-only option that many Ubuntu installs use; installing chrony replaces it.
How do I flush the DNS cache on Linux?
With systemd-resolved, run resolvectl flush-caches. Plain RHEL setups have no system-wide DNS cache unless you installed one such as nscd or dnsmasq.

Go deeper

ShareXLinkedInWhatsApp