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
hostname types
We can configure 3 hostname types is CentOS/RHEL 7 :
Hostname Type | Description |
---|---|
Static | Assigned by the system admin |
Dynamic | Assigned by DHCP or mDNS server at runtime |
Pretty | Assigned by the system admin. Its can be used as Description like “Oracle DB server” |
Out of these 3, only static hostname is mandatory. Other 2 are optional.
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 ""
Conclusion
The hostname command is used to view the hostname or to temporarily modify the hostname at runtime. And the hostnamectl command is used to permanently modify the hostname from command line.