The hostname for a system is like an identity for that system, it not only makes system management easy but we can also use the hostname to access a system over LAN. In this tutorial, we will learn five different ways by which we can change hostname in CentOS 7.
Recommended Read: Linux commands you should never run on your system
We have been using this way for most of the Linux versions & it works fine in CentOS/RHEL 7 as well. In this way, we will manually add hostname by editing /etc/sysconfig/network,
and add your hostname,
Restart network services to implement the changes.
You can now view the new hostname using the command 'hostname'.
The second way for changing the hostname is by using the command 'hostnamectl'. Hostnamectl is used to change or query hostname. For changing hostname, run the following command
To view the changes hostname, only run 'hostnameclt' command,
If you completely want to remove the hostname, use 'hostnamectl set-hostname "" '. This removes the hostname completely.
The next command to change the hostname is 'nmtui'. NMTUI or "Network Manager Text User Interface" utility can be used while using CLI or GUI. With 'nmtui', we can edit network interfaces & change hostname also.
Type 'nmtui' into your shell & a new interface with some options will launch
next, select 'set system hostname' & click 'ok and enter your desired hostname , press 'ok' & it's done.
Restart hostnamed service to implement the changes.
verify new hostname by running 'hostname' or 'hostnamectl'.
The next method of changing the hostname is by using the 'nmcli' command. It is meant to be used with nmcli, to view the hostname run the following command,
To change the hostname using nmcli, use the command below,
Restart the hostnamed service to implement changes & set the new hostname
The last way for changing the hostname is not a preferred way but none the less it gets the job done. To view the current hostname,
& if you want to change the hostname, run
Changes to the hostname will be immediate & you can verify the new hostname by running any of the above commands to check the hostname.
That's it guys, these were the five ways to change the hostname in CentOS 7. If you have any other method that you think I missed, please do share them via the comment box down below.