How to Change Hostname on Linux

Linuxpanda change hostname linux

In this tutorial, we will learn how to set or change the hostname on a Linux system.

A hostname is set when we install a fresh Linux Operating System on the server or assign a new Hostname to the server.

Understanding Host Names

A hostname is a name that specifies a device on a network. We can’t set two or more machines with the same hostname under the same network.

Read Also: How to Install Multiple PHP on CentOS 7/8 and Ubuntu 20.04

In the Linux system, we can edit and manage the hostname settings with the help of hostname command. There are three categories of Hostname in the Linux system.

  • Static – It is the basic or traditional hostname which is stored in the /etc/hostname file which can be easily set by the user.
  • Transient – It is the dynamic hostname for kernel to store and manage. As per their requirement DNS and DHCP server can change the transient hostname  at the runtime. By default, transient is the same as the static hostname.
  • Pretty – It is the descriptive form of UTF8 hostname, it represents the Owner or User. For example – Linuxpanda's Hosting

Use a FQDN (Fully-Qualified Domain Name) for the hostname of the server. Such as we have Linuxpanda.com Domain, so we have set the hostname as server.Linuxpanda.com

Prerequisite

  • Linux system or the Linux server.
  • Access of Root user or Other user with sudo privileges.

Displaying the Current Hostname

To view the hostname of the server, simply run the below command.

hostnamectl

Or you can also use the other command to check the Hostname.

hostname

Output:-

hostname.linuxpanda.com

In this tutorial, we will change the current hostname hostname.linuxpanda.com

Change the Hostname

Changing the hostname is a very simple task just simply follow the below commands.

sudo hostnamectl set-hostname server.linuxpanda.com

With the above command, we have changed the hostname from hostname.linuxpanda.com to server.linuxpanda.com.

If we want to change the Pretty Hostname then use the same command with --pretty attribute.

sudo hostnamectl set-hostname "Linuxpanda's Hosting" --pretty

hostnamectl command will show 0 on success and a non-zero on failure as output.

The Static hostname is stored in /etc/hostname and the Pretty hostname is stored in /etc/machine-info file.

Verify the change

To verify the hostname changed or not, just simply follow the below command.

hostnamectl

You will see your new hostname just like shown in the below output.

Static hostname: server.linuxpanda.com
Transient hostname: hostname.linuxpanda.com
         Icon name: computer-laptop
           Chassis: laptop
        Machine ID: c362afd4ebbsdsd992f65267ffc680b
           Boot ID: 5ef0e9dsdsd437da74426a4b5e803b7
  Operating System: Ubuntu 18.04.5 LTS
            Kernel: Linux 5.5.0-62-generic
      Architecture: x86-64

Conclusion

In this tutorial, we have learned about the hostname and how to change the hostname. With the help of this tutorial, you can easily change the hostname of the server.

If you guys have any queries related to this Change Hostname on Linux tutorial, let me know in the comments.