hostnamectl set-hostname is the right way to set the hostname in modern Linux. It writes to /etc/hostname, updates systemd, and persists across reboots. The older hostname command does the same in memory but does not persist.
Table of contents
- The two commands
- How to set the hostname
- The static, transient, and pretty hostnames
- Where it persists
- The verification
- The gotchas
- FAQ
The two commands
The two commands:
hostname <name>. Sets the hostname in memory. The change is lost on reboot.hostnamectl set-hostname <name>. Sets the hostname persistently. The change persists across reboots.
The right answer for most teams: hostnamectl set-hostname.
How to set the hostname
The command:
sudo hostnamectl set-hostname my-server.example.com
The hostname can be:
- A short name:
my-server. The right answer for most workloads. - A fully qualified domain name (FQDN):
my-server.example.com. The right answer for the workload that needs the FQDN.
The hostname should be a valid DNS name: lowercase letters, digits, and hyphens, up to 63 characters.
The static, transient, and pretty hostnames
The three hostnames:
- Static. The persistent hostname. Stored in /etc/hostname. Set with
hostnamectl set-hostname. - Transient. The in-memory hostname. Set with
hostnameor with the DHCP server. - Pretty. The human-readable hostname. Set with
hostnamectl set-hostname --pretty. Used by some desktop environments.
The static hostname is the one that matters for the team. The transient and pretty hostnames are mostly cosmetic.
Where it persists
The persistence:
- /etc/hostname. The static hostname. Persists across reboots.
- /etc/hosts. The hosts file. The team should also add the new hostname to /etc/hosts (e.g.,
127.0.1.1 my-server.example.com my-server). - systemd. The systemd hostnamed service. Reads /etc/hostname at boot.
The team that has the new hostname in /etc/hostname but not in /etc/hosts is going to have issues with some applications that resolve the hostname locally.
The verification
The verification:
hostnamectlshows the current static, transient, and pretty hostnames.hostnameshows the transient hostname.cat /etc/hostnameshows the persistent hostname.hostname -fshows the FQDN.ping $(hostname)should resolve to 127.0.0.1 (if the hostname is in /etc/hosts).
The gotchas
The gotchas:
- Cloud instances. The cloud instance’s hostname is usually managed by the cloud provider. The team that changes the hostname on a cloud instance may find it changed back on the next reboot.
- Container instances. The container’s hostname is usually managed by the container runtime. The
hostnamectlcommand may not work inside a container. - DHCP. The DHCP server can set the transient hostname. The team that has set the static hostname may find the transient hostname is different.
- The FQDN. The FQDN is the hostname plus the domain. The team that has set the hostname to
my-serverbut the domain isexample.comhas an FQDN ofmy-server.example.comonly if the search domain is set correctly.
FAQ
How do I set the hostname in Linux?
sudo hostnamectl set-hostname <name>. The change persists across reboots.
What is the difference between hostname and hostnamectl?
hostname sets the in-memory hostname. hostnamectl set-hostname sets the persistent hostname. The persistent change writes to /etc/hostname and persists across reboots.
What is the FQDN?
The fully qualified domain name: the hostname plus the domain. The team that has set the hostname to my-server and the domain is example.com has an FQDN of my-server.example.com only if the search domain is set correctly.
How do I verify the hostname is set correctly?
hostnamectl shows the current static, transient, and pretty hostnames. hostname shows the transient. cat /etc/hostname shows the persistent. hostname -f shows the FQDN.
Can I change the hostname on a cloud instance?
Yes, but the cloud provider may change it back. The team that wants the hostname to persist on a cloud instance should use the cloud provider’s metadata or user-data.
Can I change the hostname inside a container?
It depends on the container runtime. Most container runtimes do not allow the hostname to be changed inside a container. The team that needs the hostname to be specific should set the hostname at container creation.
If you are sizing a server tier for a new project, the RunxBuild hosting calculator is the place to model the line items. The instances, the hostnames, the storage, the network - each one is a separate number, and the team’s mental model for the platform is the sum of those numbers. The RunxBuild dashboard is where the team sees the actual server fleet in one place.