hostnamectl command provides a proper API used to control Linux system hostname and change its related settings. The command also helps to change the hostname without actually locating and editing the /etc/hostname file on a given system.
hostnamectl [OPTIONS...] COMMAND ....
Where COMMAND can be any of the following:
- status: Used to check current hostname settings.
- set-hostname NAME: Used to set system hostname.
- set-icon-name NAME: Used to set icon name for host.
- set-chassis NAME: Set chassis type for host.
Types of hostname:
- Static: Assigned by system admin and it is used to initialize the kernel hostname during boot time.
- Dynamic or Transient: Assigned by mDNS server or DHCP server during run time.
- Pretty: It’s a high-level hostname assigned by system admin or end-user.
Example 1: Running hostnamectl command to check the current host names. We can either execute hostnamectl or hostnamectl status, the result will be same as status option is automatically assumed if no option is given.
Example 2: To change static host name to geeksforgeeks. It may require root permission.
hostnamectl set-hostname geeksforgeeks --static
Now, let us verify the changed hostnames using the hostnamectl command.
As you see that the static hostname has been updated to geeksforgeeks.
- -h, –help: Show help text and exit.
- –version: Show package version and exit.
- –transient: This option is used when we particularly want to set transient hostname only.
Example: To set transient name to ubuntu.
Now, let’s check the transient name using hostnamectl.
- –static: This option is used when we only want to set static hostname.
Now, let’s verify the change using hostnamectl.
- –pretty: This option is used when we only want set pretty hostname. The name that is to be set needs to be in the double quote(” “).
Verifying the chnage using following command:
hostnamectl --pretty status
- –no-ask-password: This option do not prompt for password for authentication in any privileged operations.
- -H –host=[USER@]HOST: This option is used to change the host names remotely. May need root privilege while execution.
hostnamectl set-hostname -H [username]@HostName
Here, HostName is the remote host that we want to configure.
Example: To set server3 as host name on a remote server called as 220.127.116.11 we can use the following command:
hostnamectl set-hostname server3 -H email@example.com
- apt-get command in Linux with Examples
- last command in Linux with Examples
- Cat command in Linux with examples
- until command in Linux with Examples
- sum command in Linux with Examples
- gdb command in Linux with examples
- cvs command in Linux with Examples
- df command in Linux with Examples
- wc command in Linux with examples
- pwd command in Linux with Examples
- du command in Linux with examples
- w command in Linux with Examples
- ZIP command in Linux with examples
- env command in Linux with Examples
- ed command in Linux with examples
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to firstname.lastname@example.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.