How to Change Hostname on Linux
Hostname is a name given to a computer device. With the hostname, we can identify a computer device connected to a network by identifying the hostname label.
On Linux, giving the hostname is done at the time of installation. But the fact is that some users do not know the function of the hostname, so they give random names.
No need to worry if you have a random hostname, you can change it very easily. Therefore, in this post I will give a tutorial on how to change the hostname on Linux.
Open a terminal on your computer device. Then open the file /etc/hostname by typing the following command in the terminal:
sudo nano /etc/hostname
After the file opens you will see a text which is your hostname in the file. Replace the text with the hostname you want.
Then press combination CTRL + O on the keyboard to save, then press enter. If you have saved, press the combination CTRL+X to exit and return to terminal
![]() |
| Replace with the hostname you want |
Open file /etc/hosts by typing the following command in the terminal:
sudo nano /etc/hosts
In the file there is some text, below the text localhost as in the image below, is the hostname on your computer, replace the text with the hostname you want.
Then save by pressing the combination CTRL + O and press enter, press CTRL+X to get out of.
![]() |
| Replace with the hostname you want |
Restart your computer, and see if you have succeeded in changing your computer’s hostname. Safe.
![]() |
| Hostname has been changed. |


