
Displaying the current hostname
- Use the
hostnamecommand to print the current (runtime) hostname. - The persistent hostname is stored in
/etc/hostname. - For detailed metadata and the canonical static/present hostname, use
hostnamectl(systemd).
Setting a new hostname
To change the persistent hostname (updates/etc/hostname automatically), use:
hostnamectl updates the static hostname and is the recommended method on systemd-based distributions.
Local name resolution — /etc/hosts
- The file
/etc/hostscontains local host-to-IP mappings. This file is consulted before DNS whenfilesappears beforednsin/etc/nsswitch.conf. - Each line should contain an IP address, then one or more hostnames separated by whitespace.
node1 to node5 at a new IP), save /etc/hosts. The system will then resolve that hostname using the updated entry.
Testing resolution with ping
After editing/etc/hosts, use ping to confirm the hostname resolves to the expected IP:
google.com) to a chosen IP in /etc/hosts for testing, but avoid doing this for public hostnames in production.
Use getent to test resolver lookups
getent queries the system name service switch and respects the lookup order defined in /etc/nsswitch.conf. Use it to verify how the system resolves names without sending ICMP traffic:
nsswitch.conf rules.
Name resolution order — /etc/nsswitch.conf
The file/etc/nsswitch.conf defines the order the system uses to resolve different databases, including hosts. A typical hosts line is:
files— check/etc/hostsdns— query DNS serversmyhostname— fallback to local machine name methods
/etc/nsswitch.conf excerpt (with authselect comments corrected):
Quick reference
Entries in
/etc/hosts take precedence over DNS when files appears before dns in /etc/nsswitch.conf. Use /etc/hosts for local overrides and testing, but avoid modifying it for public hostnames unless you understand the implications.Links and references
hostnamectl(systemd): https://www.freedesktop.org/software/systemd/man/hostnamectl.htmlgetentmanual: https://man7.org/linux/man-pages/man1/getent.1.htmlnsswitch.confmanual: https://man7.org/linux/man-pages/man5/nsswitch.conf.5.htmlhostsfile format: https://man7.org/linux/man-pages/man5/hosts.5.htmlauthselect(RHEL): https://man7.org/linux/man-pages/man8/authselect.8.html