How to change hostname on Linux
The hostname of a Linux system is important because it’s used to identify the device on a network. The hostname is also shown in other prominent places, such as in the terminal prompt. This gives you a constant reminder of which system you’re working with. It’s a real life saver when you’re managing multiple systems through SSH and those command line terminals start to blend together in your mind.
Of course, IP addresses are used when devices need to communicate with each other, but those can change frequently. Hostnames give us a way to know which device we’re interacting with either on the network or physically, without remembering a bunch of numbers that are subject to change. Thus, it’s important that your system bears a hostname which helps you to identify it quickly. For example, “backup-server” is much more informative than “server2.”
In this guide, we’ll show you how to change the hostname on Linux. This can be done via command line or from GUI, and we’ll be showing the methods for both. If you can’t easily identify a system’s purpose from the hostname, it’s time to change it.
In this tutorial you will learn:
- How to change hostname from command line
- How to change hostname from GNOME GUI
Changing hostname on Linux
Software Requirements and Linux Command Line Conventions
Category | Requirements, Conventions or Software Version Used |
---|---|
System | Any Linux distro |
Software | N/A |
Other | Privileged access to your Linux system as root or via the sudo command. |
Conventions | # – requires given linux commands to be executed with root privileges either directly as a root user or by use of sudo command |
$ – requires given linux commands to be executed as a regular non-privileged user |
Change hostname from command line
The following method will work on systems that use systemd (most of the popular Linux distros today). If your system uses init, we cover a separate method in the next section.
- First, let’s check the current hostname by using either the
hostname
orhostnamectl
command.
1 | $ hostname |
Both commands indicate that our hostname is linux-server
.
- Next, change the hostname with the following command. In this example, we’ll change our hostname to
linuxconfig
.
1 | $ sudo hostnamectl set-hostname linuxconfig |
Run the commands from Step 1 again to confirm the new change.
-
Lastly, edit the
/etc/hosts
file to reflect the change. For example:Change this:
1 | 127.0.0.1 localhost |
To this:
1 | 127.0.0.1 localhost |
Editing the hosts file
That’s all there is to it. But there are more command line methods to change the hostname than just this one. We cover more below.
Change hostname from command line (init systems)
An alternative method to the above instructions is to edit the following two files.
- Open
/etc/hostname
with nano or your preferred text editor and change the name inside of it.
1 | $ sudo nano /etc/hostname |
Editing the /etc/hostname file
- Next, open the
/etc/hosts
file and change the name there as well.
1 | $ sudo nano /etc/hosts |
Change hostname from command line (hostname command)
The last method for changing the hostname from command line is with the hostname
command. In most environments, this only temporarily changes the name and it will revert once the system is rebooted. For example, to change the hostname to linuxconfig
:
1 | $ hostname linuxconfig |
Check the changes by just typing hostname
:
1 | $ hostname |
Change hostname from GNOME GUI
If you’re running the GNOME desktop environment and don’t wish to use the command line method, follow along with the steps below.
-
Start by opening the Settings menu from GNOME’s application launcher.
Open the Settings menu
-
Click on the About tab at the bottom and then click on Device Name.
Change the device name (hostname) inside the About tab
-
Type your new desired hostname and click Rename to finalize the changes.
Rename the device
Conclusion
In this guide, we saw various methods for changing the hostname of a Linux system via command line and GUI. We also learned about the importance of picking an applicable hostname to assist in the easy identification of a system. Administrators can use these methods to ensure that they have a network of appropriately named devices.