The hostname is a critical piece of information that helps you maintain your server machines. As a system administrator, it is imperative to have a short but recognizable hostname to separate the server machines from each other. An ambiguous or complex hostname may cause difficulty in recognizing the exact server that you may want to access remotely and can increase chances of error while applying software patches or other updates.
In this article, I’ll focus on changing the CentOS server’s Hostname, but before that, let’s discuss the different types of Hostname.
Hostname has three types:
Static Hostname: It is the primary and essential Hostname. They are kept in the /etc/hostname file. It is used by machines to recognize the specific server.
Pretty Hostname: This hostname type is user friendly. It allows you to use additional character and punctuation. But it is not permitted for machine code due to its non-standard characters.
Transient Hostname: It is a temporary hostname and is reset after the reboot. Linux Kernel maintains this Hostname. You can use it for a minor troubleshooting job in a session.
In this article, we will describe a method to change it permanently without restarting your PC.
How to change the Hostname on CentOS
To change the Hostname in the CentOS permanently follow the beneath steps:
Step 1: Check Current Hostname
Firstly, you should know the current Hostname. To check the current Hostname, type hostnamectl command in the console.
This command will list the operating system and network configuration information along with the static Hostname.
Static hostname: workmachine21 Icon name: computer-desktop Chassis: desktop Machine ID: 5f7e36c18a974f06ae94ddaaf11d71e8 Boot ID: 337e48b00fed4abe9ab929fed5aa6018 Operating System: CentOS Linux 8 (Core) CPE OS Name: cpe:/o:centos:centos:8 Kernel: Linux 4.18.0-193.x86_64 Architecture: x86-64
Step 2: Change the Static Hostname
Before changing the static Hostname of CentOS 7, you should know the values of it because it allows only Fully Qualified Domain Names (FQDNs).
- Lower-case letters A-Z
- Numbers 0-9
- Hyphens and Periods
- Character limit of the Hostname is 2 to 63
- Hostname start and end must be a letter or a number.
Now type the command as follows:
hostnamectl set-hostname fosslinux
We just changed the static Hostname from “workmachine” to “fosslinux”.
Step 3: Verify the Hostname
After changing the Hostname, verify it by typing the hostnamectl command. The result shows the system information along with a new static hostname.
Static hostname: fosslinux Icon name: computer-desktop Chassis: desktop Machine ID: 5f7e36c18a974f06ae94ddaaf11d71e8 Boot ID: 337e48b00fed4abe9ab929fed5aa6018 Operating System: CentOS Linux 8 (Core) CPE OS Name: cpe:/o:centos:centos:8 Kernel: Linux 4.18.0-193.x86_64 Architecture: x86-64
Step 4: Use Pretty and Transient Hostname (Optional)
For setting a pretty Hostname, the use of quotation marks is mandatory if the Hostname contains spaces or a single quotation mark. For example:
hostnamectl set-hostname "FOSS Linux" --pretty
For Transient Hostname, where we set a temporary hostname, use the command below:
sudo hostnamectl –transient set-hostname temporary.fosslinux
After completing the renaming, check the status of Hostname by using the command:
The console will return a list of information, including the static, pretty, and transient Hostname, to confirm the change.
Note: Machines can only identify the FQDN hostname. So, CentOS automatically changes or removes the unacceptable characters.
Clearing a Particular Host Name
To clear a hostname and revert to the default, enter the following command as root with the appropriate option:
hostnamectl set-hostname "" [option...]
Where “” is a quoted empty string followed by option is one or more of: –pretty, –static, and –transient.
Changing Host Names Remotely
For the situations where you want to execute a hostnamectl command on a remote system, use the -H, –host option as follows:
hostnamectl set-hostname -H [username]@name
The tool will use SSH to connect to the remote system. Where name is the remote Hostname, you want to configure. The username is optional.
That’s all about setting, clearing the CentOS hostname on a local machine or over the network, remotely. Run the desired command from the commands as mentioned earlier and permanently or temporarily change the Hostname.