In most cases, systemctl is used to control services, but there are some additional systemd commands that you can use. For example, hostnamectl enables you to view the system’s network hostname and other information about the system’s hardware and the Linux kernel it is running. You can also use this command to change the system’s hostname.
Syntax
The syntax of the hostnamectl command is:
# hostnamectl [options] [subcommand] [arguments]
For example, to set the hostname to server01:
# hostnamectl set-hostname server01
If you encounter the below error while executing any hostnamectl command:
hostnamectl: command not found
you may try installing the systemd package as per your choice of distribution.
Distribution | Command |
---|---|
Debian | apt-get install systemd |
Ubuntu | apt-get install systemd |
Arch Linux | pacman -S systemd |
Kali Linux | apt-get install systemd |
CentOS | yum install systemd |
Fedora | dnf install systemd |
Raspbian | apt-get install systemd |
hostnamectl Command Examples
1. Get the hostname of the computer:
# hostnamectl
2. Set the hostname of the computer:
# hostnamectl set-hostname "hostname"
3. Set a pretty hostname for the computer:
# hostnamectl set-hostname --static "hostname.example.com" && sudo hostnamectl set-hostname --pretty "hostname"
4. Reset hostname to its default value:
# hostnamectl set-hostname --pretty ""