How to Change Hostname in a Linux Machine using hostnamectl command
Introduction
This article is about how you can change your Linux machine hostname using the hostnamectl command. While installing Redhat 7.x or CentOS 7.x, in case you have not specified any hostname, then by default “localhost.localdomain” will be assigned as default hostname for that machine. You can easily change the hostname for your Redhat or CentOS Linux machine using the hostnamectl command. This method can be used for CentOS 7.x / RHEL 7.x and RHEL 8 operating systems.
Method 1: hostnamectl
hostnamectl command can be used to change or query system hostname and related settings. The advantage of using hostnamectl is that , we dont require machine reboot to make the changes. Let us learn more about hostnamectl command.
Types of Hostnames
- Pretty Hostname: The high level pretty hostname is in the form of Human readable format and it can include all kinds of special characters. Example of Pretty hostname can be “John’s Laptop” or “Database Server”.
- Static Hostname: It is the traditional hostname, which can be chosen by the user and used to initialize kernel hostname at boot. It is stored in the /etc/hostname file.
- Transient Hostname: It is the dynamic hostname maintained by the kernel and is initialized to the static hostname by default.
Changing your Hostname in CentOS 7 or RHEL 7 /RHEL 8
In CentOS and RHEL 7.x onwards hostname query and control is managed by hostnamectl command. You can use hostnamectl command to change or set hostnames. As described above, all the three hostnames viz Pretty hostname, static hostname and transient hostname can be set using hostnamectl .
To view all the Host names
To query about current status of hostname information, issue the hostnamectl command.
# hostnamectl
To change all the Hostnames
To change all the host names command, use below command as root user. This will change the pretty, static and transient hostnames alike.
# hostnamectl set-hostname name
To Set a Particular hostname
In case you want to set a Particular hostname, use below command with required option.
# hostnamectl set-hostname name <options>
Options can be either:
- –static
- –pretty
- –transient
Example of setting Pretty hostname:
# hostnamectl set-hostname "John's Laptop" --pretty
Example of setting Static hostname:
# hostnamectl set-hostname linuxforgeek --static
To clear a particular hostname
Use below command as root user to clear a particular hostname and revert the same to its default.
# hostnamectl set-hostname "" <options>
In above command “” refers to empty string and will clear the hostname and <option> can be either –pretty, –static or –transient.
Making new hostname effective
Once you are done with changing the hostname of your Linux server/machine, you can open up a new terminal session or issue shell command bash to update in the current session
# bash
Method 2 : modify /etc/hostname
Another method to change hostname in RHEL 7.x/CentOS 7.x is by changing hostname in /etc/hostname file followed by the reboot .
# vi /etc/hostname
# reboot
That is all for this post, feel free to comment below for any query.