1. Introduction
Why Change the Hostname on Ubuntu?
The hostname is a crucial element for identifying a machine within a system or network when managing servers or virtual machines. This is especially important in corporate or cloud environments where multiple servers and virtual machines are in operation. A clear and well-structured hostname significantly improves efficiency and ease of management. Changing the hostname may also be necessary when migrating servers or modifying system environments.
This article provides a detailed guide on how to change the hostname temporarily, how to make permanent changes that persist after reboot, and how to configure network settings using Netplan.
2. How to Check the Current Hostname
Command to Check the Hostname
The most basic method to check the current hostname is to use the following command:
hostname
This command will display the current hostname. If you need more detailed system information, use the hostnamectl
command:
hostnamectl
Executing this command will display additional system details along with the hostname. Here is an example output:
Static hostname: my-hostname
Operating System: Ubuntu 20.04 LTS
With this, you can confirm your current hostname.

3. How to Temporarily Change the Hostname
Temporarily Changing the Hostname Using the hostname
Command
To temporarily change the hostname, use the hostname
command. This change will revert to the original hostname after a system reboot, making it ideal for short-term testing or working with virtual machines.
sudo hostname new-hostname
For example, to temporarily change the hostname to temp-hostname
, execute the following command:
sudo hostname temp-hostname
Verifying the Temporary Hostname Change
To confirm that the hostname has been changed correctly, run the hostnamectl
command again:
hostnamectl
This allows you to check whether the change has been applied successfully. However, since the hostname will revert after a reboot, proceed to the next step if you want to make the change permanent.
4. How to Permanently Change the Hostname
Permanently Changing the Hostname Using the hostnamectl
Command
The recommended way to permanently change the hostname is to use the hostnamectl
command. This method ensures that the new hostname persists after reboot.
sudo hostnamectl set-hostname new-hostname
For example, to change the hostname to my-new-hostname
, execute:
sudo hostnamectl set-hostname my-new-hostname
Editing the /etc/hostname
File Directly
Another permanent method is to directly edit the /etc/hostname
file.
- Open the
/etc/hostname
file with a text editor.
sudo nano /etc/hostname
- Replace the existing hostname with the new hostname.
my-new-hostname
- After saving and exiting, restart the system.
sudo reboot
Editing the /etc/hosts
File
After changing the hostname, make sure to update the /etc/hosts
file accordingly. This file contains mappings between hostnames and IP addresses.
127.0.1.1 my-new-hostname
Updating this ensures that the new hostname is correctly recognized within the network.
5. Modifying Network Settings with Netplan
What is Netplan?
Netplan is a tool for managing network configurations in Ubuntu, especially recommended for use in server and virtual machine environments. It helps automate network configuration in cloud and large-scale networks. Since Netplan allows centralized management of both hostname changes and network settings, it is particularly useful in complex networking environments.
Changing Hostname and Network Settings Using Netplan
- Edit the Netplan configuration file.
sudo nano /etc/netplan/50-cloud-init.yaml
- Add hostname and network settings inside the configuration file.
network:
ethernets:
ens33:
addresses:
- 192.168.1.100/24
gateway4: 192.168.1.1
nameservers:
addresses:
- 8.8.8.8
- 8.8.4.4
version: 2
hostname: my-new-hostname
- After completing the configuration, apply the changes using the following command.
sudo netplan apply
Troubleshooting
If an error occurs when applying Netplan, use the following command to display debug information and check for detailed error messages:
sudo netplan --debug apply
Most errors are caused by syntax mistakes or incorrect network configurations, so carefully review your settings if an issue arises. It is also recommended to create a backup of the existing configuration file before making any changes.
6. Security Considerations
When changing the hostname, it may impact SSH connections and firewall settings. Therefore, it is important to verify and adjust security configurations accordingly. For example, after changing the hostname, ensure that the /etc/hosts
file is correctly updated and that the firewall settings remain consistent with the previous configuration. Additionally, since SSH connections may be affected by hostname changes, reviewing SSH settings as needed is recommended.

7. Conclusion
There are two main methods for changing the hostname in Ubuntu: temporary and permanent changes. For temporary changes, use the hostname
command, while for permanent changes, the recommended method is using hostnamectl
. Additionally, Netplan offers a convenient way to manage network configurations efficiently, making it a preferred choice for complex network environments.
After changing the hostname, always verify that the /etc/hosts
file, firewall settings, and SSH configuration are correctly updated to ensure the system operates smoothly.