5 ways to add or change hostname in RHEL/CentOS 7
Hostname for a system is like an identity for that system, it not only makes system management easy but we can also use hostname to access a system over LAN. In this tutorial, we will learn five different ways by which we can change hostname in RHEL/CentOS 7.
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 command ‘hostname’.
Second way for changing hostname is by using 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 remove the hostname completely.
Next command to change hostname is ‘nmtui‘. NMTUI or “Network Manager Text User Interface” utility can 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‘ & its done.
Restart hostnamed service to implement the changes.
verify new hostname by running ‘hostname’ or ‘hostnamectl’.
Next method of changing hostname is by using ‘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
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 hostname.
That’ it guys, these were the five ways to change hostname. If you have any other method that you think i missed, please do share them via the comment box down below.