In this quick tip, I’ll show you how to change hostname on Ubuntu Linux and other Debian based Linux distributions.
But before you see that, 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 problems. This hostname is supposed to uniquely identify a computer on a network.
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
There are two ways to change hostname in Ubuntu:
- Command Line
- GUI Way
We shall see both ways of changing the hostname in this tutorial.
Change hostname in Ubuntu 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.
Open a terminal and use the command below to change the hostname. Replace <new_hostname> with the new hostname you want to use:
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.
Let’s see how to change hostname permanently in the next step.
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.
Run the following command in the terminal to open the /etc/hostname file in the vim editor for editing (assuming that you have Vim installed):
sudo vim /etc/hostname
You can also choose other editors like Nano, GNU Emacs etc. The /etc/hostname file is very simple. It contains your computer’s hostname.
You need to change the existing hostname with the new hostname.
You should also edit the /etc/hosts file in the similar fashion and replace all the occurrences of your old hostname with the new hostname.
Ubuntu, Mint and other Debian-based distribution read the /etc/hostname file while booting up and set your computer’s hostname to the one contained in the file. To verify the permanent changes, you can restart your computer. And then, you are done!
Change hostname in Ubuntu Using GUI
The beginner friendly Linux distribution Ubuntu often provides the graphical way of doing things.
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:
If you open a new terminal now and check your hostname, you’ll see that it has been changed:
I hope this article helped you to change hostname on Ubuntu and other Linux. Feel free to provide your feedback.
With inputs from Abhishek Prakash.