Change the hostname (Computer name) in Linux, Ubuntu

A hostname is a computer name which identifies a computer system in a network. The hostname should be a single word with no spaces, may have letters, numbers, periods, or a hyphen and maximum length of 253 characters.

We recommend always using lowercase characters in hostnames to prevent any possible problems or confusion.

Generally, we set up the hostname to the machine during OS setup. You can change your computer’s hostname. In this tutorial, you will learn to change the hostname.

Method 1: Change Hostname on Ubuntu (Reboot Required)

To change the hostname is ubuntu use the below command in the terminal.

sudo nano /etc/hostname
  • After using the above command, you will see the current hostname on the nano editor, replace that hostname you wish to change with your new hostname. Save and Exit.
  • Here I am changing the hostname from programbr to rocktheworld.

change hostname in ubuntu change hostname in ubuntu change hostname in ubuntu

sudo reboot

Now reboot your computer using the command sudo reboot or simply from GUI to apply the change in hostname.change hostname in Linuxchange hostname in Linux

Method 2: Change Hostname on Ubuntu (Reboot not Required)

If you want to change the hostname without rebooting your computer, use the hostnamectl command. use the following command in the terminal.

hostnamectl set-hostname new-hostname

Don’t forget to replace new-hostname with your desired hostname. Here I am using programbr as new-hostname

  1. You may be required to enter the password to authenticate this step, use your computer-login password.
  2. If authentication is complete, use hostnamectl to check the result.
  3. hostnamectl
  4. you will see static hostname with your new hostname.

hostnamectl- change hostname in ubuntu withought reboot

To see the changes, exit from the terminal (Command Line) and re-open the command line.hostnamectl- change hostname in ubuntu withought reboot

Conclusion

Ubuntu hostname can be changed via command line as well as GUI. In this tutorial, you learned How to Change Hostname in Ubuntu, Linux via command line. This method should be applicable to all ubuntu versions (16.04 -22.04)

Sharing is Caring
Scroll to Top