How to Set or Change Hostname in Linux System

Hostname is the label of a system or in other words it is a human readable name of a system, after setting up the hostname of the device then it becomes easy to manage and access based on their label or hostname.

In this post, we will learn how to set or change the hostname of a linux system using hostnamectl and nmtui tool. Hostname can be of three types,

  • Pretty – It supports all kind of special characters.
  • Static – It is the type of hostname which used to initialize the kernel hostname at boot
  • Transient – It is the hostname which is set base on based network configuration.

In static and transient hostname, only accepted characters of internet domain name is allowed to use whereas there is no limit on characters in pretty hostname.

Set hostname with hostnamectl command

In modern linux distributions like RHEL, CentOS, Rocky Linux, Debian and Ubuntu hostnamectl is available for setting up the hostname. When we run hostnamectl command without any arguments then it will display the current hostname,

$ hostnamectl

hostnamectl-command-output

Alternate way to check the hostname of your system is by running hostname command without any options, example is show below

pkumar@ubuntu:~$ hostname
ubuntu
pkumar@ubuntu:~$

Set Static Hostname

To set the static hostname, run the following hostnamectl command,

Syntax:

$ sudo hostnamectl set-hostname <hostname>

Example

$ sudo hostnamectl set-hostname "linuxbuzz.local.lan"
$ exec bash

Verify the hostname using following commands,

$ hostname
$ hostnamectl

Output of above command:

Set-Static-Hostname-Linux

Set Pretty Hostname

To set the pretty type hostname then use –pretty arguments in hostnamectl command, example is shown is below:

$ sudo hostnamectl --pretty set-hostname "PKUMAR_LinuxBuzz ##Blog##"
$ exec bash
$ hostnamectl

Output

Set-Pretty-Hostname-Hostnamectl-command

Perfect, above shows that static and pretty hostname are set.

Remove Hostname

To remove the static hostname, run

$ sudo hostnamectl set-hostname ""

To remove pretty hostname, run

$ sudo hostnamectl --pretty set-hostname ""

Set hostname using nmtui tool

Static hostname can also be set using nmtui tool. It’s simple and straight forward, start nmtui tool by running,

$ nmtui

Set-hostname-nmtui-command

Choose ‘Set system hostname’ option and hit enter. In the following window, specify the hostname that you want to set and then choose OK and hit enter.

Specify-Hostname-nmtui-command

Choose OK in the following window and then quit the nmtui tool.

Choose-Ok-to-Set-hostname-nmtui

Verify the new hostname by running following command

pkumar@linuxbuzz:~$ hostname
linuxbuzz.example.lan
pkumar@linuxbuzz:~$

Great, that’s all from this post. I hope you have found it informative and insightful. Please do share your feedback and queries in below comments section.

Read AlsoHow to Setup Chroot SFTP Server in Linux

Leave a Comment

nineteen + 5 =