Linux Commands

How to Set Hostname Using Hostnamectl Command?

The hostname is an identity of the system and is used by the networks to search the system.

The “hostnamectl” is a Linux command that is used to set the hostname in the terminal without even opening and editing in the etc/hostname file of a system.

Using the “hostnamectl” command, the user can edit the static, pretty, and transient hostname as well.

This command-line utility is categorized into three (3) types:

  1. Static Hostname: Set by system administrators
  2. Dynamic Hostname: Set by mDNS servers on execution time
  3. Pretty Hostname: Set by system administrators or user

The syntax of the “hostnamectl” command is given below:

hostnamectl [options…] [command]

How to Use “hostnamectl” Command:

Execute the “hostnamectl” command without giving any argument in the terminal to display the current status of hostname:

$ hostnamectl

Change Static Hostname:

The “hostnamectl” command allows the user to change the “static hostname” without even restarting the system.

Run the following command syntax in terminal and set the “static hostname” according to your need:

$ hostnamectl set-hostname [set static-hostname] –static

Suppose, to set hostname as “Linux-User”, the command would be:

$ hostnamectl set-hostname Linux-User --static

The changed static hostname is visible in the terminal.

The “pretty hostname” can also be changed through the “–pretty” option of the “hostnamectl” command. So, type in double quotes (“”) the name that you want to set and execute it in terminal:

$ hostnamectl set-hostname [set pretty-hostname”] –pretty

For instance, to assign a pretty hostname as “Linux”, execute the command:

$ hostname set-hostname “Linux” --pretty

It can be seen in the above image that the pretty hostname has changed.

To check if the “pretty-hostname” is updated on the system or not, first, set the name you want to change, then execute the “–pretty status” command to confirm the modification:

$ hostnamectl set-hostname “Linux” --pretty

$ hostnamectl --pretty status

To display the help of the “hostnamectl” command in terminal, type:

$ hostnamectl --help

To display the version of the “hostnamectl” command, run:

$ hostname --version

Conclusion:

The “hostnamectl” command is a handy command to modify the system’s hostname without changing the etc/hostname file. In this guide, we have learned that how to set static and pretty hostname of a system.

About the author

Syeda Wardah Batool

I am a Software Engineer Graduate and Self Motivated Linux writer. I also love to read latest Linux books. Moreover, in my free time, i love to read books on Personal development.