Changing Hostname on Ubuntu & Other Linux

In this quick tutorial, I’ll show you how to change hostname on Ubuntu, Debian and other Linux distributions.
Warp Terminal

The hostname is basically your computer’s name. It can be used to identify and communicate your system on the same network.

Some Linux distributions and cloud service providers let you choose the hostname while installing. Some will automatically create one for you but you’ll always have a computer name.

If you don’t like the one you’ve got, you can use the following systemd command to change the hostname on major Linux distributions:

hostnamectl set-hostname new_host_name

Read the rest of the article to learn more about hostname and ways to change it.

Before you see various ways to change the hostname, you should first know what’s a hostname!

What is a hostname in Linux?

A hostname is a name assigned to a “host” i.e. a computer on a network. The hostname is basically just your computer’s name. It’s used to identify your computer on the network.

You can see the hostname in the terminal using the hostname command.

check the current hostname in terminal
Check the current hostname in terminal

As you can see in the above screenshot, the username is abhishek while the hostname is itsfoss.

When you install Ubuntu, it asks you to choose a name for your computer. That’s the “hostname” for your system.

If at any point in time you feel like your computer doesn’t have a fancy or relevant name, you can easily change the hostname in Linux.

Note that you shouldn’t use the same hostname on two different computers on the same network, as this can cause problems. This hostname is supposed to uniquely identify a computer on a network.

There are two ways to change the hostname in Ubuntu:

  • Change hostname in command Line
  • Change hostname using GUI Way

Here I am assuming you have Ubuntu on your system, but you can try the same command line trick on Linux Mint and some other Debian distributions as they use the same configuration as Ubuntu.

Change hostname in Ubuntu & other Linux using command line

If you want to change the hostname in Ubuntu or any other Debian based Linux distribution, you can use the typical Linux way to do that, which is using the command line.

Now, there are several commands for changing the hostname in Linux. I’ll show you two such methods here.

Method 1: Change hostname using systemd tools

If your Linux system uses systemd, you can use the tools provided by systemd to change the hostname. To check your current hostname and some other information, use the command:

hostnamectl

It will give you a lot of details:

 Static hostname: itsfoss
       Icon name: computer-laptop
         Chassis: laptop 💻
      Machine ID: xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
         Boot ID: xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
Operating System: Ubuntu 23.04                    
          Kernel: Linux 6.2.0-32-generic
    Architecture: x86-64
 Hardware Vendor: Dell Inc.
  Hardware Model: XPS 13 9310
Firmware Version: 3.17.0

To change the hostname, you can use this option with the hostnamectl command:

hostnamectl set-hostname <new_hostname>

Of course, replace the <new_hostname> with your new hostname in the above command.

changing the hostname through terminal command
Changing the hostname through terminal command

Method 2: Change hostname temporarily with hostname command

Open a terminal and use the command below to change the hostname. Replace <new_hostname> with the new hostname you want to use:

sudo hostname <new_hostname>
change the hostname using hostname command
Change the hostname using hostname command

Now if you use the command hostname, you’ll see that your hostname has been changed.

But the changes you just made are not permanent. If you reboot your system, the old hostname will be back.

Method 2: Change hostname permanently with hostname command

To change your hostname permanently, you’ll also need to edit your /etc/hostname and /etc/hosts file, which is where Ubuntu, Mint and other Debian-based distributions store the hostname.

Since you are in terminal, you need to use a command line based text editor to edit these files like Vi or Vim. Ubuntu has Nano editor installed by default so you can use that as well.

I am going to use nano here. You’ll only see one line here and that contains your system’s hostname. Change it to the hostname you want and save the file.

sudo nano /etc/hostname

You must also edit the /etc/hosts file in the similar fashion and replace all the occurrences of your old hostname with the new hostname.

sudo nano /etc/hosts

This is important otherwise you’ll start seeing “sudo: unable to resolve host” error.

hosts and hostname files containing current hostname
Hosts and hostname files containing current hostname

That’s it. You are done!

Though I used Ubuntu here, you can use these methods to change hostname in Debian or most other distributions.

Change hostname in Ubuntu Using GUI

The beginner-friendly Linux distribution Ubuntu often provides the graphical way of doing things.

To change the hostname in Ubuntu 22.04 or any other Linux distribution using GNOME desktop, just go to the System Settings and click on About.

In here, you’ll see the ‘Device name’ field that is editable. This ‘Device name’ is your system’s hostname. Change it to whatever you want. The changes you make here are permanent.

change the hostname through ubuntu settings gui
Change the hostname through ubuntu settings GUI

It’s the same process pretty much in any other desktop environment.

Bonus Tip: Mandatory rules for hostnames

There are some rules specified for hostname:

  • hostname can only contain letters, digits, and the hyphen character, and the period character
  • hostname must begin and end with a letter or number
  • hostname must be between 2 and 63 characters long

If your proposed hostname doesn’t follow the rules, you may see an error like this:

hostname: the specified hostname is invalid
invalid hostname because of unsupported characters
Invalid hostname because of unsupported characters

I hope this article helped you to change hostname on Ubuntu and other Linux distributions. Feel free to provide your feedback.

About the author
Abhishek Prakash

Abhishek Prakash

Created It's FOSS 11 years ago to share my Linux adventures. Have a Master's degree in Engineering and years of IT industry experience. Huge fan of Agatha Christie detective mysteries 🕵️‍♂️

Become a Better Linux User

With the FOSS Weekly Newsletter, you learn useful Linux tips, discover applications, explore new distros and stay updated with the latest from Linux world

It's FOSS

Great! You’ve successfully signed up.

Welcome back! You've successfully signed in.

You've successfully subscribed to It's FOSS.

Success! Check your email for magic link to sign-in.

Success! Your billing info has been updated.

Your billing was not updated.