Where Is the Linux Hostname Stored?
Linux keeps its hostname in /etc/hostname with resolution in /etc/hosts. Files, commands, and split-brain pitfalls.
Last updated
A Linux machine's name lives in two small files with different jobs. /etc/hostname holds the static hostname the system claims at boot. /etc/hosts maps that name to loopback addresses so local resolution never needs DNS. Change one without the other and strange delays appear.
The classic symptom is sudo hanging: the hosts file disagrees with the hostname file, and privilege escalation waits on a lookup that times out. hostnamectl manages both sides on systemd systems and shows static, transient and pretty names together. Containers and cloud images often override all of this at boot, so check the running values, not just the files.
Where Linux hostname stores this, by platform
/etc/hostname
Static hostname, one short name per line. Resolution partner is /etc/hosts with the hostname on 127.0.0.1 or its own 127.0.1.1 line. Manage both via hostnamectl set-hostname on systemd systems. Same paths on every mainstream distribution and stable for decades.
Frequently asked questions
how do i change linux hostname permanently
Write the short name to /etc/hostname and add a 127.0.1.1 line for it in /etc/hosts, then apply with hostnamectl set-hostname. Reboot-free and persistent. Editing only one file leaves sudo delays or resolution failures, since different tools read different files.
why does sudo hang after hostname change
Your hosts file maps the hostname to the wrong address or lacks the 127.0.1.1 line. sudo then waits on a name lookup that times out. Add the hostname beside 127.0.0.1 or its own 127.0.1.1 line and the delay vanishes instantly. This is the most common hostname misconfiguration on desktops.
can hostname differ from dns name
Yes, and that is supported. The kernel name from /etc/hostname can be short (web1) while DNS supplies the domain. Keep /etc/hosts consistent with the short name regardless. hostnamectl shows all three forms: static from the file, transient from DHCP, pretty for display.
Notice an outdated path? Let us know.