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.
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.
The hostname can contain alphanumeric characters, dots, and hyphens. It cannot start or end with a hyphen, and each label (the part between dots) can be at most 63 characters long. The total FQDN length must not exceed 253 characters.
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" --prettyUpdate /etc/hosts
After changing the hostname, you should update the /etc/hosts file to map the new hostname to the loopback address. Without this step, some programs (including sudo) may display warnings like “unable to resolve host.”
Open the file 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.
Cloud Instances and cloud-init
On cloud instances (AWS, GCP, Azure, DigitalOcean), the hostname is often reset on every reboot by cloud-init. To make a hostname change persist, set preserve_hostname to true in /etc/cloud/cloud.cfg:
preserve_hostname: trueThen change the hostname with hostnamectl as normal. Without this setting, cloud-init will overwrite your changes on the next boot.
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 hosts file | sudo nano /etc/hosts |
| Preserve hostname on cloud | Set preserve_hostname: true in /etc/cloud/cloud.cfg |
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?
Set preserve_hostname: true in /etc/cloud/cloud.cfg before changing the hostname.
Conclusion
Changing the hostname in Linux is straightforward with hostnamectl. Remember to update /etc/hosts after the change to avoid resolution warnings. On cloud instances, configure cloud-init to preserve your hostname.
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 800+ Linux tutorials with 20+ years of experience turning complex Linux tasks into clear, reliable guides.
View author page