A hostname in Linux is what identifies your computer on the network. Ubuntu hostname is added when setting up the OS, but if you change your mind, understanding how Ubuntu change hostname works allows you to easily set a new hostname.
When it comes to changing your Linux hostname, there are numerous options you can use. Besides, some options only change the hostname temporarily, while others may require you to reboot the system for the change of hostname to reflect. This post discusses the different options you can use and where they are applicable. Let’s dive in!
How to Change Hostname on Ubuntu 22.04
Before we learn how changing the hostname works, let’s first understand how you can check your hostname. There are two commands that you can use.
To only get the hostname alone, run the hostname command.
$ hostname
Alternatively, to check the hostname and other additional information about your system, use the hostnamectl command.
$ hostnamectl
The first line in your output is your system’s hostname.
Method 1: Set a Static Hostname
The first method in our Ubuntu change hostname guide involves setting a temporary hostname that will change when you reboot the system. This method is ideal when you want a new hostname for a short time.
To set ‘new-user’ as our hostname, run the below command.
$ sudo hostname new-user
Enter your password to confirm the change.
You can then verify that the hostname has changed by running the hostnamectl command. The static hostname remains unchanged, but the transient hostname will change.
Method 2: Ubuntu Change Hostname Permanently
If you want a permanent way to change your Linux hostname, the best option is to use the hostnamectl command with the set-hostname option, as in the example below.
$ sudo hostnamectl set-hostname geekuser
When we check the current hostname, we notice that our static hostname reflects the new hostname we’ve specified when running the command.
Method 3: Set the Pretty Hostname on Ubuntu
If you don’t know what a “pretty” hostname is, let me break it to you. A “pretty” hostname is a fancy name for your computer and will only be visible to you. Moreover, it doesn’t follow any naming structure like a static hostname.
To set a pretty hostname, use the hostnamectl command but add the –pretty option.
$ sudo hostnamectl set-hostname “My Ubuntu Machine” --pretty
When we check our existing hostname, we verify that we have the “pretty” hostname set as expected.
Method 4: Ubuntu Change Hostname via GUI
For someone uncomfortable with changing their Linux hostname via terminal, it is possible to do it graphically.
Start by opening the Settings option on your Ubuntu. Navigate to the About section and tap on the Device Name.
In the next window, type the new hostname and click the rename button.
Bingo! You’ve managed to set a new hostname on your system via GUI.
Read Also: How to install OpenSSH Server on Ubuntu.
Method 5: Change Ubuntu Hostname – Reboot Required
So far, all the methods we’ve covered allow you to change your Linux hostname without rebooting your system. However, when you change the hostname by editing the configuration files /etc/hostname and /etc/hosts, you must reboot the system.
Open your terminal and access the /etc/hosts using a text editor like Nano.
$ sudo nano /etc/hosts
Find your IP address and edit the hostname.
Save the file and open the /etc/hostname.
$ sudo nano /etc/hostname
Add the same new hostname you added in the previous configuration file.
Save the file and reboot your system for the new hostname to take effect.
Conclusion
You can change your Ubuntu hostname anytime, provided you know what steps to take. This post shares five methods you can use depending on your goal. Go through them and settle on the best method for your case to quickly change your Ubuntu hostname.