The hostnamectl command in Linux with examples

In this article, you will learn about the hostnamectl command in Linux with examples.

A hostname is a human-readable name. This is the name which people can refer to a computer and they can easily remember it. The hostname was started before the DNS.

In the past time, we had to define the hosts on a network locally in the file /etc/hosts. And, this was very tedious work, because if you have 100 hosts and you got one new host then you had to update their /etc/hosts file to reflect the correct IP address and corresponding hostname for each of their neighbors. This is not good for a big organization like the world wide web. Therefore, DNS was invented. Now, the network hostname is more important.

  • The hostnamectl is the command-line utility in Linux and Unix-like operating systems. It is useful for changing the system hostname. There are mainly three types of hostname.

    Types of Hostname

    Static Hostname

    • it is the name provided by the Linux kernel.
    • And, it is stored in /etc/hostname. Transient Hostname
    • It is also known as a dynamic hostname.
    • It gets this name from the network configuration.
    • By default, it reads from /etc/hostname on startup.
    • It can be changed manually or dynamically by  a service like DHCP or mDNS
    • If you have a system in the network with the hostname server and you are going to change the hostname of the second system same as the server then the hostname for the second system will be automatic update with the server-1  Pretty Hostname
    • It is the name provided by a user.
    • It is a free-form UTF-8 string of text that is presented to the user.

      Restriction of the Hostname 

    • The hostname can be up to 64 characters in length.
    • It can have 7-bit ASCII lowercase characters
    • No spaces.
    • There must be no dots in the name portion.
    • Red Hat recommends both the static and transient names match the FQDN used by DNS.
    • The name can start to a to z, A-Z, 0 to 9, (hyphen), _ (underscore), and. (dot).
    • However, the name can not start and end with a dot and also can not have two dots consecutively.

      Examples of the Hostname

      So, you can find below a few examples of the hostname.
    • My.server.domain.com
    • MY.server.domain.com
    • my-server.domain.com
    • my_server.domain.com

      How to get help for the hostnamectl command

      I always suggest reading the manual page and help. This will give you a deep introduction to the command.

      So, to get the manual page type the below command,

      man hostnamectl

      Also, to get the only command options, you can type the below command.

      hostnamectl --help

      Syntax of the hostnamectl command

      As we know now, how to get some information about the hostnamectl command using the above command with arguments. Now let’s talk about hostnamectl syntax. The syntax of the hostnamectl command is mentioned below,

      hostnamectl [OPTIONS…] {COMMAND}

      Checking the current system hostname

      There are three main commands to check the hostname. 

      Firstly, using the hostname command.

      hostname
  • hostname-command
    The output of the hostname command

    Secondly, using the hostnamectl command.

    hostnamectl
    hostnamectl-command-output
    The output of the hostnamectl command.

    Thirdly, using the hostnamectl status command. This command is the same as the hostnamectl command.

    hostnamectl status
    The output of the hostnamectl status command. This output is the same as the hostnamectl command.

    Preconditions

    • A VPS or a local server on which CentOS/RHEL 7 /8, Fedora, or Ubuntu/Debian is running.
    • You must log in with a root user or a user with sudo privilege to make the changes.

      Changing the static hostname

      So, if you want to change the static hostname then you can use the below command. And, there is an option in the hostnamectl command that is –static option.

      For instance, I am going to change the static hostname to the client.linuxgurus.local. But, you can put the static hostname which you want to keep.

      sudo hostnamectl set-hostname client.linuxgurus.local --static

      As a result, it will change the static hostname. So, you can see in the below picture.

      hostnamectl-static-name-change
      Changing the static hostname.

      Changing the transient hostname

      After that, we are going to learn about changing the transient hostname. So, there is an option in the hostnamectl command that is –transient option.

      For example, I’m changing the transient name to linuxgurus. Finally, the command will be as below and here you can put the transient name which you want to keep.

      sudo hostnamectl set-hostname linuxgurus --transient

      As a result, it will change the transient hostname. So, you can see in the below picture.

      Changing the transient hostname.

      Changing the pretty hostname

      So, here we are going to change the pretty hostname. There is an option in the hostnamectl command that is –pretty option. 

      For example, I’m changing the pretty hostname to lg. Finally, the command will be as below and here you can put the pretty hostname which you want to keep.

      sudo hostnamectl set-hostname "lg" --pretty

      Note: You have to put this pretty hostname between double quotes (” “).

      As a result, it will change the pretty hostname. So, you can see in the below picture.

      hostnamectl-pretty
      Changing the pretty hostname. And, the output.

      Changing hostname remotely

      Above all, you can also change the name of the remote host system from your system. This is a very nice option that helps to change the hostname over the network.

      It will ask for the confirmation password for the remote host and when you provide the remote host root or sudo level privilege then it will change the hostname. So, we are using the -H option. The syntax will be as below.

      syntax: sudo hostnamectl set-hostname new_hostname -H username@ipaddress

      For example, I want to change the hostname of the system whose ip address is 192.168.43.10 and the new hostname for the remote host will be ns1.linuxgurus.local. So, the command will be as below.

      sudo hostnamectl set-hostname ns1.linuxgurus.local -H sahil@192.168.43.10

      The most frequently used option with hostnamectl command

      Lastly, I’m going to discuss at a glance the most frequently used option along with the hostnamectl command.

    • –static Only set the static hostname
    • –transient Only set the transient hostname
    • –pretty Only set the pretty hostname
    • -H –host=[USER@]HOST Operate on the remote host. This is the command to change the hostname from your system to another system over the network.

      Conclusion

      In this tutorial, you learn about what is the hostname and hostnamectl command. In conclusion, you can use the hostnamectl command now. Therefore, Keep practicing. I hope, you understand but if you have any questions, you can ask in the comment section.

      You can read about What is the passwd command in Linux? 

      Also, How to setup passwordless ssh login

      You can also read about hostnamectl from redhat,  Red Hat article about hostnamectl

       

Leave a Reply