Ubuntu

How to Change the Hostname in Ubuntu?

If you have ever used a computer or browsed through websites, you must have encountered the term hostname. The hostname is a customized name generated by the user, helps in identifying the system within the network. Here, we are considering the hostname in the Ubuntu system. A user assigns a hostname to their machine during the setup of the operating system. It helps them to recognize their machine uniquely over the internet.

Various reasons may influence you to change your machine name. One of the most important reasons is that no two systems can share the same machine name to avoid conflicts. Therefore, if you are new and want to set up a hostname, it should be unique and chosen wisely.

What is a Hostname?

A hostname generally refers to the specific device on a network. However, you can also refer to a hostname as a computer name and site name. Having a hostname for your system will uniquely identify your device within a local network. If you want to exchange data over the network to any machine, you must know that system’s hostname. Hostname comes as a part of the domain name.

Understanding Host Names

A hostname helps in identifying your device on a network. Unfortunately, we cannot run two or more machines or systems having the same hostname on the same network. But it may be possible if the machine is on a different network.

In Ubuntu, you are allowed to edit the hostname of your system and various related settings using its most commonly used command, “hostnamectl”. This tool helps in recognizing the three different classes of hostname, as shown below.

  • Static: it specifies the standard hostname. It is stored in the file located at the path /etc/hostname that the user can set.
  • Pretty: it specifies the descriptive free-form UTF8 hostname that is well-suited for presentation to the user. For example, Linuxize’s laptop.
  • Transient: it refers to the dynamic hostname, specially maintained by the kernel. The two servers, DHCP or mDNS, can be used for changing the transient hostname during runtime. However, by default, this hostname has the same as the static hostname.

Next, we shall learn various ways to change the hostname of the Ubuntu server 20.04.

How to Change the Hostname in Ubuntu

It is one of the common practices to change the hostname for your Linux server. Therefore, you should have a piece of a sound knowledge of the command line commands and proper access to run those commands on the terminal.

Prerequisites

Below are some basic requirements that you may require while changing the hostname.

  • Ubuntu 20.04 server installed on your machine.
  • Root access or user with pseudo access for running the commands.
  • You should be able to access non-GUI methods.

Checking Current Hostname on Ubuntu 20.04

Linux is all about running commands from the command line. For example, if you want to check the current hostname of your Ubuntu machine, run the following two commands.

The below-mentioned command will display the hostname only. Simply type “hostname” and hit enter to get the hostname.

Use its second command, “hostnamectl”. It will display the hostname along with additional important information that may be useful for various purposes. For example, in the below image, you can see the static-hostname, which specifies the hostname of your machine.

Changing the Hostname Temporarily

If you want to make a temporary change to the machine’s hostname, use the hostname command to do that.

Run the below-mentioned command in the terminal that will replace the new-hostname parameter with the provided name.

$ sudo hostname new-hostname

On completion of this command, there will be no output displayed on the screen. If you want to look for the applied changes, check the current hostname of your system.

Changing the Hostname of Ubuntu System Without Reboot Option

Suppose you want the permanent change to the hostname for your Ubuntu system without the need to reboot your machine. First, use the “hostnamectl” command. Then, follow the below-mentioned simple steps to process this.

Change the hostname.
Use the below command to replace the new hostname with the provided name.

$ hostnamectl set-hostname new-hostname

Confirming the change.
Once you run the command mentioned above, you can check the output with the “hostnamectl” command.

Changing the Pretty hostname.
This hostname is only available to the user and not available for another system on the network. For changing the pretty hostname of the system, use the same command “hostnamectl” along with the “–pretty” parameter.

$ hostnamectl set-hostname "new-hostname" --pretty

Again, replace the “new-hostname” with the provided hostname.

Once you run the above command, you will get an extra line in the output mentioning the pretty hostname for your system, as shown below.

Changing the Hostname of Ubuntu System with Reboot Option

Apart from the option mentioned above, you can change the hostname of your Ubuntu system by rebooting the system. It will change the hostname permanently by changing the configuration files.

  • /etc/hostname
  • /etc/hosts

The changes will be in effect once you reboot the system. You can follow the below simple steps for implementing this permanent change to the hostname.

Open /etc/hostname for modification
Edit the file at this path using any text editor available. Here, we are using Vim editor for this purpose. Run the below-mentioned command.

$ sudo vi /etc/hostname

This file will display the current hostname which you can change by providing the name of your choice.

Open /etc/hosts for modification
You can edit this file in the same as above. But, first, open the file using Vim editor and provide the chosen name for the hostname.

$ sudo vi /etc/hosts

This file helps in mapping the hostname to the IP addresses. Select the hostname for changing and replace it with the new hostname.

Rebooting the system.

For making the changes permanent, reboot the system. Run the below command to do so.

$ sudo systemctl reboot

Changing Hostname with Ubuntu 20.04 GUI

Follow the below simple steps to move forward and make changes to the hostname using the Graphical user interface of the Ubuntu 20.04 server.

Open Settings, and navigate to the About section.

Now, locate the device name field for making changes.

Now, click the Device name filed and then open the rename device name dialog box to continue.

Now, provide the new name for your hostname and then confirm the changes by clicking the rename option on the top of the dialog box.

After clicking the rename option, you can make the hostname permanent.

Conclusion

The hostname is an important part of your machine. It is the name with which your machine will be recognized, and it has to be unique. No two machines on the same network can share the same hostname. If you want to connect to any other system present on the network, you need the hostname to connect.

Changing the current hostname of any system is not a difficult task. We have mentioned a few ways to change your hostname, either temporary or permanent depending on your requirement.

About the author

Simran Kaur

Simran works as a technical writer. The graduate in MS Computer Science from the well known CS hub, aka Silicon Valley, is also an editor of the website. She enjoys writing about any tech topic, including programming, algorithms, cloud, data science, and AI. Travelling, sketching, and gardening are the hobbies that interest her.