In this quick tutorial, I’ll show you how to change hostname on Ubuntu, Debian and other Linux distributions.
How to Change hostname in Linux
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.
As you can see in the above screenshot, the username is abhishek while the hostname is istfoss-notes.
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 of the time you feel like your computer doesn’t have a fancy or relavant 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 problem. This hostname is supposed to uniquely identify a computer on a network.
There are two ways to change hostname in Ubuntu:
We shall see both ways of changing the hostname in this tutorial.
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:
It’s output should be something like this:
Static hostname: itsfoss
Icon name: computer-laptop
Machine ID: 1b9ab93094fa4978beba80fd3c48a18c
Boot ID: 68a0f4c77b9d4ca3aaa2c99ce051a7f8
Operating System: Ubuntu 18.04.2 LTS
Kernel: Linux 4.15.0-46-generic
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 host name in the above command.
Method 2: Change hostname 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>
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.
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.
I am going to use nano here. You’ll only see one line here and that contains the 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.
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 18.04 or any other Linux distribution using GNOME desktop, just go to the System Settings and click on Details.
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.
It’s the same process pretty much in any other desktop environment. If you are using Ubuntu 16.04 Unity you can expand the next section to see the screenshots.
Change hostname in Ubuntu 16.04 Unity
To change the hostname, go to System Settings (search for it in Unity Dash or GNOME). In here, look for Details.
Here, you’ll see the option to change the name. You don’t need to save it explicitly:
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
In the end…
I hope this article helped you to change hostname on Ubuntu and other Linux distributions. Feel free to provide your feedback.