1. Home
  2. Linux

How to change the Linux computer hostname

Unix-like operating systems like Linux, BSD, and others, use hostnames to talk with one another. What is a hostname? It’s a unique name that the user sets. These names can be anything, from a first name to a code-name to a string of numbers, your website’s domain name, or anything in between.When it comes to Linux, hostnames are often set during the initial installation process. If you’re not happy with it, you can change the Linux computer hostname to something different.

Changing the hostname on Linux involves messing around with the Root folder. It will require Root access, so before we begin with this guide, please follow our tutorial on how to enable the Root user on Linux.

Option 1 – The Hostname application

Many Linux operating systems come with a program called “Hostname.” It’s a small utility that allows users to quickly and easily swap out and change the hostname of any given Linux system quickly and easily. It also allows users to set alias names, view the IP address of a given hostname, and much more.

While most distributions ship with this application, it’s a good idea to go over how to install it anyways, as some may not have it set up or may have previously uninstalled it. To set up the Hostname application launch a terminal window by pressing Ctrl + Alt + T or Ctrl + Shift + T with the keyboard. After that, follow the instructions below that correspond with the Linux OS you are using.

Ubuntu

sudo apt install hostname

Debian

sudo apt-get install hostname

Arch Linux

Sadly, the Hostname application is one tool that the Arch Linux operating system does not have. Thankfully, for as useful as the Hostname app is, it’s not required to use to change your hostname. If you’re on Arch and in need of changing the hostname on your system, it’s still possible to do by following option 2 in the next section of this article.

Fedora

sudo dnf install hostname

OpenSUSE

sudo zypper install hostname

Generic Linux

Most Linux users will have access to the Hostname app. However, if for some reason you don’t have a way to get access to it, it’s best to skip Option 1 and try Option 2. There’s no software to install, and every Linux operating system no matter how obscure has an /etc/hostname file.

Change your hostname with the Hostname utility

Hostname works in the terminal and requires Root privileges. The reason it needs Root is that the hostname file on a Linux system resides in the system directory, and users without superuser privileges cannot access this file. To gain root access, you can run the su command. Or sudo -s is possible if you have the Root account disabled.

su -

Or:

sudo -s

Once you’ve got Root access, you’ll be able to change the hostname on the machine with the command below.

hostname -b new hostname

After the change is made, you can confirm your new hostname with the following command. Though, keep in mind: your new hostname will not show up in any Linux terminal until you restart your Linux PC or server.

hostname

Set hostname with a file

The Hostname application allows users to set hostname info with a file. This feature is especially useful if the new name you’re trying to set is a very long one. To do it, open up a terminal window and gain a root shell with su or sudo -s.

su -

Or

sudo -s

Next, create the text file that will hold the hostname with the touch command.

touch /home/username/hostname-file

Open the hostname file in the Nano text editor.

nano -w /home/username/hostname-file

Paste the new hostname into the Nano text editor by pressing Ctrl + Shift + V on the keyboard. Then, save it by pressing Ctrl + O and exit with Ctrl + X.

Finally, change your hostname from the file with:

hostname -i hostname-file

Option 2 – Modify /etc/hostname file

Another way to change the Linux computer hostname is to directly modify the file where the system saves the name of your computer, in the /etc/hostname file. To access and edit the file, open up a terminal window. Then, gain a root shell by using su to log in as root, or by using sudo -s if you do not have access to superuser privileges.

su -

Or

sudo -s

Once you’ve got Root access in the terminal window, open up the /etc/hostname file in the Nano text editor.

nano -w /etc/hostname

Using the keyboard, erase the existing hostname from the file. Be sure that there are no spaces left over. Then, when the old hostname is deleted, write out your new desired hostname.

When you’re satisfied with the new hostname change, save the Nano text editor by pressing the Ctrl + O keyboard combination. Then, close it with Ctrl + X.

Leave a comment