The hostnamectl
command in Linux is used to view or modify the hostname and related settings of the system. It is available on systems that use systemd, such as many modern distributions including Ubuntu, CentOS, and Fedora.
Here are some common uses of the hostnamectl
command:
- View the current hostname:
hostnamectl
This command displays information about the current system hostname, including the static hostname, transient hostname, and pretty hostname.
- Change the hostname:
To change the system’s hostname, you can use theset-hostname
option followed by the desired hostname. However, this operation typically requires administrative privileges. Here’s an example:
sudo hostnamectl set-hostname new_hostname
Replace new_hostname
with the desired hostname you want to set for the system. After executing the command, you may need to restart your system or certain services for the changes to take effect.
- Set the static hostname:
The static hostname is the persistent hostname that survives system reboots. To set the static hostname, you can use theset-hostname
option with the--static
flag:
sudo hostnamectl set-hostname --static new_static_hostname
Replace new_static_hostname
with the desired static hostname.
- Set the pretty hostname:
The pretty hostname is an arbitrary and user-friendly name associated with the system. You can set it using theset-hostname
option with the--pretty
flag:
sudo hostnamectl set-hostname --pretty "My Pretty Host"
Replace "My Pretty Host"
with the desired pretty hostname.
- Set the transient hostname:
The transient hostname is a temporary hostname that is dynamically assigned and can change during the system’s runtime. To set the transient hostname, you can use theset-hostname
option with the--transient
flag:
sudo hostnamectl set-hostname --transient new_transient_hostname
Replace new_transient_hostname
with the desired transient hostname.
Please note that the exact functionality and available options of the hostnamectl
command may vary depending on your Linux distribution and system configuration. You can refer to the hostnamectl
manual page for more detailed information by running man hostnamectl
in the terminal.