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
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.


SUBSCRIBE TO NEWSLETTER
Subscribe to Linux Career NEWSLETTER and receive latest Linux news, jobs, career advice and tutorials.


  1. First, let's check the current hostname by using either the hostname or hostnamectl command.
    $ 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 linux-server.
  2. Next, change the hostname with the following command. In this example, we'll change our hostname to linuxconfig.
    $ sudo hostnamectl set-hostname linuxconfig
    
    Run the commands from Step 1 again to confirm the new change.


  3. Lastly, edit the /etc/hosts file to reflect the change. For example:

    Change this:
    127.0.0.1 localhost
    127.0.1.1 linux-server
    
    To this:
    127.0.0.1 localhost
    127.0.1.1 linuxconfig
    
    Editing the hosts file
    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.

  1. Open /etc/hostname with nano or your preferred text editor and change the name inside of it.


    $ sudo nano /etc/hostname
    
    Editing the /etc/hostname file
    Editing the /etc/hostname file
  2. Next, open the /etc/hosts file 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 linuxconfig:

$ hostname linuxconfig

Check the changes by just typing hostname:

$ 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.


SUBSCRIBE TO NEWSLETTER
Subscribe to Linux Career NEWSLETTER and receive latest Linux news, jobs, career advice and tutorials.


  1. Start by opening the Settings menu from GNOME's application launcher.

    Open the Settings menu
    Open the Settings menu
  2. Click on the About tab at the bottom and then click on Device Name.

    Change the device name (hostname) inside the About tab
    Change the device name (hostname) inside the About tab


  3. Type your new desired hostname and click Rename to finalize the changes.

    Rename the device
    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.

FIND LATEST LINUX JOBS on LinuxCareers.com
Submit your RESUME, create a JOB ALERT or subscribe to RSS feed.
LINUX CAREER NEWSLETTER
Subscribe to NEWSLETTER and receive latest news, jobs, career advice and tutorials.
DO YOU NEED ADDITIONAL HELP?
Get extra help by visiting our LINUX FORUM or simply use comments below.

You may also be interested in:



Comments and Discussions