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:
hostnameThis command will display the current hostname. If you need more detailed system information, use the hostnamectl command:
hostnamectlExecuting 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 LTSWith 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-hostnameFor example, to temporarily change the hostname to temp-hostname, execute the following command:
sudo hostname temp-hostnameVerifying the Temporary Hostname Change
To confirm that the hostname has been changed correctly, run the hostnamectl command again:
hostnamectlThis 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-hostnameFor example, to change the hostname to my-new-hostname, execute:
sudo hostnamectl set-hostname my-new-hostnameEditing the /etc/hostname File Directly
Another permanent method is to directly edit the /etc/hostname file.
- Open the
/etc/hostnamefile 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 rebootEditing 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-hostnameUpdating 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 applyTroubleshooting
If an error occurs when applying Netplan, use the following command to display debug information and check for detailed error messages:
sudo netplan --debug applyMost 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.

![[How to Use YUM on Ubuntu] RPM Package Management Steps and Alternatives](https://www.linux.digibeatrix.com/wp-content/uploads/2024/10/1e7a7b81049dbc1b46e2b26b9fa7bed7-375x375.webp)
