On most Linux systems, the persistent hostname is stored in /etc/hostname. The file usually contains just the machine’s short hostname, but the exact setup can vary by distribution and by which service manages the system’s network identity.
What /etc/hostname contains
/etc/hostname is a plain-text configuration file used by many Linux distributions to set the system hostname at startup. For example, a file containing web01 sets the machine’s hostname to web01. The file typically has one hostname on one line, without a domain name or explanatory text.
The hostname is the system’s local name: it appears in command prompts, logs, monitoring tools, and some network-related output. It is distinct from a username and from a DNS record. A hostname in this file does not, by itself, register the machine with DNS or guarantee that other computers can resolve it.
How Linux applies the hostname
Linux maintains a hostname in the running kernel. During startup, an initialization system or network-management component can read the configured name and apply it. On many modern distributions, systemd provides the hostnamectl command, while other systems may use their own startup scripts or configuration tools.
That distinction explains why editing a file and changing the current hostname are not always the same operation. A file edit changes persistent configuration, but the running system may keep its existing hostname until it is set again or rebooted. Conversely, changing the live hostname with a command may not survive a reboot unless the persistent configuration is updated too.
To see the current name, run:
hostname— prints the running system hostname.hostnamectl status— on systemd systems, shows hostname information and related system details.cat /etc/hostname— displays the file’s contents, if that file is used on the system.
Change the hostname safely
On a system using systemd, the usual persistent method is to run sudo hostnamectl set-hostname web01, replacing web01 with the desired name. This normally updates the configured hostname and applies it to the running system. Check the result with hostnamectl status and, if relevant, inspect /etc/hostname.
On other distributions, use the distribution’s documented hostname command or configuration utility. If you edit /etc/hostname manually, use administrative privileges and place the intended short hostname on its own line. You may need to apply the new name with a command such as sudo hostname web01, or reboot, depending on how the system is configured.
Before changing a hostname on a server, consider services that refer to the old name. Monitoring, certificates, application settings, cluster membership, backup jobs, and automation may need updates. A hostname change is usually low risk on a personal machine, but it can affect systems that use the name as an identifier.
Hostname, FQDN, and /etc/hosts
A common source of confusion is the difference between a hostname and a fully qualified domain name (FQDN). A hostname might be web01; an FQDN might be web01.example.net. The FQDN combines a host label with a DNS domain. Whether a system reports an FQDN can depend on DNS configuration, resolver behavior, and local settings; it is not necessarily written as the hostname in /etc/hostname.
/etc/hosts is a separate file. It maps names to IP addresses locally, often including an entry for the machine itself. For example:
127.0.0.1 localhostmaps the conventional localhost name to the IPv4 loopback address.127.0.1.1 web01is a local mapping used by some Debian-derived installations.
That second line is not a universal requirement, and the right entries depend on the distribution and network setup. Do not assume that changing /etc/hosts changes the system hostname: it changes local name-to-address mappings. Similarly, editing /etc/hostname does not create a DNS record.
Why the file or value may differ
Some Linux installations do not use /etc/hostname as the sole source of truth. A minimal container may inherit a hostname from its runtime or orchestrator. A cloud image may receive one from cloud-init or a provider’s metadata service. Network managers and distribution-specific tools can also set a name during boot.
In these environments, a manual edit may be overwritten at the next boot or when the managing service runs. If the hostname keeps reverting, identify its owner rather than repeatedly editing the file. Check the distribution’s documentation and inspect relevant tools or services, such as cloud-init on images that use it. Containers may also have a hostname supplied when they are created, so the durable change may belong in the container configuration rather than inside the container.
Also distinguish a transient hostname from a static one where the system supports both. A transient name can be supplied dynamically, often through network configuration; a static name is intended to persist. The behavior and available settings depend on the operating system’s tools and configuration.
Common troubleshooting checks
If the hostname shown by a command does not match the contents of /etc/hostname, check which configuration manager controls it and whether the file was changed after startup. Verify both the live value and persistent setting, then consult the distribution’s startup and network configuration.
- If a new name appears only after reboot, the file was likely updated but the live hostname was not applied immediately.
- If the old name returns after reboot, a cloud, container, or network-management service may be resetting it.
- If local programs cannot resolve the machine’s name, inspect
/etc/hosts, DNS configuration, and the relevant resolver; the hostname file alone does not provide name resolution. - If a command reports an FQDN different from the short hostname, check DNS and resolver settings rather than assuming the hostname file is malformed.
There is no need to add quotes or a trailing domain to /etc/hostname just to make a machine reachable by network clients. Reachability and name resolution are separate networking concerns.
Related
Frequently Asked Questions
Where is the hostname file in Ubuntu?
On standard Ubuntu installations, the persistent hostname is commonly stored in /etc/hostname. Ubuntu systems also use /etc/hosts for local name mappings, which serves a different purpose.
Can I change the Linux hostname without rebooting?
Usually. On systemd-based systems, sudo hostnamectl set-hostname newname changes the hostname immediately and persistently. Other distributions have different tools, so confirm the supported method for your system.
Is /etc/hosts the same as /etc/hostname?
No. /etc/hostname commonly stores the machine’s configured hostname. /etc/hosts maps hostnames to IP addresses for local name resolution.
Why does my Linux hostname change back after a restart?
A service or platform may regenerate it during startup. Cloud-init, a container runtime, or network-management software can control the name. Find and change the authoritative configuration rather than editing only a file that another service overwrites.
Does /etc/hostname contain the full domain name?
Usually it contains the short hostname, such as web01. The FQDN, such as web01.example.net, is determined by the system’s broader DNS and resolver configuration and is not guaranteed by this file.