Check the Current Name
hostname
hostnamectl
hostname prints the name; hostnamectl also shows the static name, the operating system, and the kernel. These steps are written for the current Ubuntu LTS releases and work the same on Debian.
Set the New Name
sudo hostnamectl set-hostname web01
The change applies immediately and is written to /etc/hostname, so it survives a reboot. Your shell prompt still shows the old name until you open a new session. No reboot is needed.
Update /etc/hosts
The system should be able to resolve its own name. Open the file:
sudo nano /etc/hosts
On Debian and Ubuntu, the local name sits on a 127.0.1.1 line. Replace the old name there, or add the line if it is missing:
127.0.0.1 localhost
127.0.1.1 web01.example.com web01
Put the full name first and the short name after it. Leave the localhost line as it is. The Debian documentation suggests using the server's permanent IP address instead of 127.0.1.1 when it has one; either way the name must match /etc/hostname.
Short Name or Full Domain Name?
The hostname can be a short name such as web01 or a fully qualified domain name (FQDN) such as web01.example.com. Many administrators set the short name with hostnamectl and the FQDN in /etc/hosts as shown above; then hostname prints web01 and hostname -f prints the full name. Some software, such as mail servers and control panels, expects the hostname itself to be the FQDN; follow its installation notes. In both cases, the FQDN should also exist in DNS and point to the server.
Keep cloud-init from Resetting It
Many cloud images run cloud-init at boot, and it can set the hostname from the provider's data each time, which brings back the old name after a reboot. To keep your own name, set this in /etc/cloud/cloud.cfg:
preserve_hostname: true
If the line already exists with false, change the value rather than adding a second line. When cloud-init's manage_etc_hosts setting is true, it also rewrites /etc/hosts from a template at every boot; a comment at the top of that file says so, and in that case edit the template it names or turn the setting off. If your provider's control panel has a field for the server name, changing it there keeps both sides in line. Reboot once and check with hostname that the name stayed; how to reboot a Linux server covers the safe way.
The Unable to Resolve Host Error
If sudo prints a warning that it is unable to resolve host followed by the new name, the name is missing from /etc/hosts. The command still runs, but the warning appears every time and some programs wait for a DNS timeout. Add the 127.0.1.1 line shown above to fix it.
AlmaLinux and Rocky Linux
The same command applies:
sudo hostnamectl set-hostname web01.example.com
These systems have no 127.0.1.1 convention. Add the name either to the 127.0.0.1 line or, more commonly on servers, on a line with the server's own IP address:
203.0.113.10 web01.example.com web01
cloud-init behaves the same way on cloud images of these systems.
Rules for Host Names
- Characters: letters, digits, and hyphens; no spaces or underscores.
- Hyphens: not at the start or end of a part.
- Length: each part between dots up to 63 characters; Linux limits the whole hostname to 64.
- Case: use lowercase; names are not case-sensitive.
Reverse DNS Is Separate
The hostname on the server does not change what the IP address resolves to in reverse DNS (the PTR record). That record is set where the IP address is managed, usually in your provider's control panel, and matters mainly for mail servers. Forward DNS problems are covered in ERR_NAME_NOT_RESOLVED. To check the system version shown by hostnamectl, see how to check your Ubuntu version.