How to Change the Hostname in Linux

By default, the system hostname is set during the installation process, or if you are creating a virtual machine it is dynamically assigned to the instance at startup, but there are situations when you need to change it.
This tutorial will walk you through the process of changing the hostname in Linux without restarting the system. The instructions should work on any modern Linux distribution that uses systemd.
Quick Reference
| Task | Command |
|---|---|
| View hostname | hostnamectl |
| View hostname (short) | hostname |
| Read static hostname file | cat /etc/hostname |
| Set hostname permanently | sudo hostnamectl set-hostname name |
| Set pretty hostname | sudo hostnamectl set-hostname "Name" --pretty |
| Set hostname temporarily | sudo hostname name |
| Update an existing hosts entry | sudo nano /etc/hosts |
| Prevent cloud-init hostname changes | Set preserve_hostname: true in /etc/cloud/cloud.cfg.d/99-hostname.cfg when needed |
What is a Hostname
A hostname is a label assigned to a machine that identifies it on the network. Each device in the network should have a unique hostname.
A static or transient hostname can contain lowercase ASCII letters, numbers, dots, and hyphens. Each label (the part between dots) can be at most 63 characters long and cannot start or end with a hyphen. Systemd limits the complete hostname to 64 characters, even though DNS allows longer FQDNs.
If the machine is accessible on the internet (such as a web or mail server), use a fully qualified domain name (FQDN) as the system hostname. The FQDN consists of two parts, the hostname and the domain name.
Hostname Types
On systemd-based systems, there are three types of hostnames:
- Static - the traditional hostname stored in
/etc/hostname. Set by the administrator and persists across reboots. - Transient - a temporary hostname assigned by the kernel at boot. It defaults to the static hostname but can be changed at runtime by DHCP or mDNS. Lost on reboot.
- Pretty - a free-form, UTF-8 hostname for display purposes (e.g., “John’s Laptop”). Stored in
/etc/machine-info.
You can view all three with:
hostnamectl Static hostname: mail.linuxize.com
Icon name: computer-vm
Chassis: vm
Machine ID: 70a3f06298014fd9ac42e5dc1de1034a
Boot ID: 6d45a1a8d436418e97519da01ea61c1b
Virtualization: kvm
Operating System: Debian GNU/Linux 12 (bookworm)
Kernel: Linux 6.1.0-27-amd64
Architecture: x86-64The Static hostname line shows the persistent hostname stored by systemd.
Displaying the Current Hostname
There are several ways to view the current hostname.
The hostnamectl command (shown above) displays the static hostname along with system information.
The hostname command prints the current hostname:
hostnameYou can also read the static hostname directly from the file:
cat /etc/hostnameChanging the Hostname Permanently
To permanently change the hostname, use the hostnamectl command with the set-hostname argument. Only root or a user with sudo
privileges can change the system hostname.
For example, to change the system hostname to mail.linuxize.com:
sudo hostnamectl set-hostname mail.linuxize.comThis command writes the new hostname to /etc/hostname and updates the transient hostname. It produces no output on success.
To set only a specific hostname type, use the --static, --transient, or --pretty flags:
sudo hostnamectl set-hostname "John's Laptop" --prettyNewer systemd releases document set-hostname under the shorter name hostname, so sudo hostnamectl hostname mail.linuxize.com has the same effect. Both forms work, and set-hostname is still what you will find in most existing scripts and guides.
Update the Hosts File
After changing the hostname, check whether /etc/hosts contains the old name. If it does, replace it with the new hostname so local resolution remains consistent. Otherwise, programs such as sudo may display warnings like “unable to resolve host.”
Systems that use nss-myhostname can resolve the current hostname without an /etc/hosts entry, so you do not need to add a mapping solely because the hostname changed.
If the file needs updating, open it with your editor:
sudo nano /etc/hostsFind the line that contains the old hostname and replace it with the new one:
127.0.0.1 localhost
127.0.1.1 mail.linuxize.comOn systems that use 127.0.0.1 for both entries, update accordingly.
Verify the Change
Run hostnamectl to confirm:
hostnamectlYou can also open a new terminal session to see the updated hostname in the shell prompt.
Changing the Hostname Temporarily
The hostname command changes the hostname for the current session only. It reverts to the static hostname on the next reboot:
sudo hostname temp-nameThis is useful for testing or troubleshooting without making permanent changes.
Changing the Hostname Without systemd
On minimal systems or containers that do not have systemd, you can change the hostname by editing the files directly.
Write the new hostname to /etc/hostname:
echo "mail.linuxize.com" | sudo tee /etc/hostnameApply it immediately without rebooting:
sudo hostname -F /etc/hostnameThen update /etc/hosts as described above if it contains the old hostname.
Cloud Instances and cloud-init
Cloud instances can receive their hostname from instance metadata through cloud-init or provider-specific software. Current cloud-init releases normally stop updating /etc/hostname after detecting a manual change. If the previous hostname still returns after a reboot, add a drop-in file that tells cloud-init to preserve your change:
preserve_hostname: trueThis drop-in keeps your local override separate from the distribution’s package-managed /etc/cloud/cloud.cfg file. Then change the hostname with hostnamectl as normal. If it still reverts, check the provider agent, DHCP settings, and provisioning configuration.
FAQ
Do I need to reboot after changing the hostname?
No. The hostnamectl set-hostname command takes effect immediately. However, you may need to open a new terminal session to see the updated prompt.
Why does sudo show “unable to resolve host” after changing the hostname?
The /etc/hosts file still references the old hostname. Update it to map the new hostname to 127.0.1.1 (or 127.0.0.1).
What is the difference between static and transient hostname?
The static hostname is stored in /etc/hostname and persists across reboots. The transient hostname is a runtime value that can be changed by DHCP or mDNS and resets on reboot.
My cloud instance reverts the hostname on reboot. How do I fix it?
First confirm that cloud-init is changing the hostname. If it is, set preserve_hostname: true in /etc/cloud/cloud.cfg.d/99-hostname.cfg. If the name still reverts, check the provider agent, DHCP settings, and provisioning configuration.
Conclusion
hostnamectl applies the new hostname immediately, and you only need to update /etc/hosts when it still contains the old name. If a cloud instance restores the previous hostname after reboot, check cloud-init and the provider’s provisioning settings; for other features, including machine metadata and remote operation, see the hostnamectl command
guide.
For distro-specific instructions, check the following articles:
Tags
Linuxize Weekly Newsletter
A quick weekly roundup of new tutorials, news, and tips.
About the authors

Dejan Panovski
Dejan Panovski is the founder of Linuxize, an RHCSA-certified Linux system administrator and DevOps engineer based in Skopje, Macedonia. Author of 1000+ Linux tutorials with 20+ years of experience turning complex Linux tasks into clear, reliable guides.
View author page