Change the Linux hostname with sudo hostnamectl set-hostname newname, update /etc/hosts to map 127.0.1.1 to the same name, then verify with hostname -f. The two-step is mandatory. The right answer is to do both, not just the first. The team that skips the /etc/hosts edit breaks sudo and every other service that consults the local name map.
Table of contents
- The two commands
- The verification
- The cloud-init override
- What hostnamectl does
- Why /etc/hosts matters
- FAQ
The two commands
The right commands to change the hostname:
sudo hostnamectl set-hostname newname
sudo nano /etc/hosts
In /etc/hosts, change the line:
127.0.1.1 oldname
to:
127.0.1.1 newname
Do not change 127.0.0.1 localhost. The two addresses are different (loopback vs local hostname) and both are correct in the file.
The verification
The right verification is three commands:
hostname
hostname -f
getent hosts newname
hostname returns the short hostname. hostname -f returns the FQDN (errors if /etc/hosts is wrong). getent hosts newname returns the IP the system resolves the new hostname to — should be 127.0.1.1.
The cloud-init override
On cloud servers (AWS, GCP, Azure), cloud-init resets the hostname on every boot. The right answer is to disable cloud-init’s hostname management:
sudo nano /etc/cloud/cloud.cfg
Set preserve_hostname: true. The right answer is to also set the hostname through the cloud provider’s API for consistency.
What hostnamectl does
hostnamectl updates three things: the kernel hostname, the static hostname file at /etc/hostname, and the systemd-hostnamed daemon’s internal state. The change is immediate. The right answer is that no reboot is needed.
Why /etc/hosts matters
The /etc/hosts file is the local name-to-IP map. Many services (sudo, apt, postfix) consult it before hitting DNS. The wrong answer is to leave the old hostname in /etc/hosts — the result is a sudo: unable to resolve host newname warning on every sudo invocation.
FAQ
Does the change survive a reboot?
Yes, on a non-cloud box. On a cloud box, cloud-init may reset it. The right answer for a cloud box is preserve_hostname: true in /etc/cloud/cloud.cfg.
Can I have a hostname with a dot?
Yes. hostnamectl set-hostname newname.example.com sets the FQDN.
Can I have a hostname with uppercase?
Technically yes. The right answer is lowercase only, because DNS is case-insensitive in lookup but case-preserving in records.
How do I change the hostname on a system without systemd?
Use sudo hostname newname and update /etc/hostname by hand. The kernel hostname is updated immediately; the persistent file is updated for the next boot.
What is the difference between hostname and FQDN?
Hostname is the short name. FQDN is hostname plus domain. hostname -f returns the FQDN.
How do I see the hostname in the prompt?
The shell’s PS1 is set at startup. The right answer is to add \h (short hostname) or \H (FQDN) to PS1, then source ~/.bashrc to reload.
Can I have a hostname with spaces?
Yes for the pretty hostname (hostnamectl set-hostname --pretty "My Server"). No for the static hostname — it must match RFC 952 / RFC 1123.
What is the chassis type?
The chassis type (laptop, desktop, server, vm, container) is set in /etc/machine-info or auto-detected. The right answer is to set it correctly if the auto-detection is wrong.
If you are sizing the infrastructure for the kind of project this post covers, the RunxBuild hosting calculator is the right place to model the line items. The compute, the memory, the storage, the bandwidth, the database - 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 usage in one place.
Useful related references: