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
|Requirements, Conventions or Software Version Used
|Any Linux distro
|Privileged access to your Linux system as root or via the
# – requires given linux commands to be executed with root privileges either directly as a root user or by use of
$ – 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 linux-server $ hostnamectl Static hostname: linux-server Icon name: computer-vm Chassis: vm Machine ID: e94c741849c848f8baca2ec406f88c41 Boot ID: 55f7533a0cb6448ba677ef30b2104481 Virtualization: oracle Operating System: Ubuntu 20.04.1 LTS Kernel: Linux 5.4.0-42-generic Architecture: x86-64
Both commands indicate that our hostname is
- Next, change the hostname with the following command. In this example, we’ll change our hostname to
$ sudo hostnamectl set-hostname linuxconfig
Run the commands from
Step 1again to confirm the new change.
- Lastly, edit the
/etc/hostsfile to reflect the change. For example:
127.0.0.1 localhost 127.0.1.1 linux-server
127.0.0.1 localhost 127.0.1.1 linuxconfig
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.
/etc/hostnamewith nano or your preferred text editor and change the name inside of it.
$ sudo nano /etc/hostname
- Next, open the
/etc/hostsfile and change the name there as well.
$ 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
$ hostname linuxconfig
Check the changes by just typing
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.
- Click on the About tab at the bottom and then click on Device Name.
- Type your new desired hostname and click Rename to finalize the changes.
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.