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
Also Read: Simple way to change home directory of Jenkins Server
First way
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,
$ vi /etc/sysconfig/network
and add your hostname,
NETWORKING=yes
HOSTNAME=centos7.linux.com
Restart network services to implement the changes.
$ systemctl restart network
You can now view the new hostname using the command 'hostname'.
Second Way
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
$ hostnamectl set-hostname "centos7.linux.com"
To view the changes hostname, only run 'hostnameclt' command,
$ hostnamectl
If you completely want to remove the hostname, use 'hostnamectl set-hostname "" '. This removes the hostname completely.
Third way
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
$ nmtui
next, select 'set system hostname' & click 'ok and enter your desired hostname , press 'ok' & it's done.
Restart hostnamed service to implement the changes.
$ systemctl restart systemd-hostnamed
verify new hostname by running 'hostname' or 'hostnamectl'.
Fourth Way
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,
$ nmcli general hostname
To change the hostname using nmcli, use the command below,
$ nmcli general hostname centos7.linux.com
Restart the hostnamed service to implement changes & set the new hostname
$ systemctl restart systemd-hostnamed
Fifth way
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,
$ sysctl kernel.hostname
& if you want to change the hostname, run
$ sysctl kernel.hostname=centos7.linux.com
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.
I am not able to change the hostame with either of these methods.. After reboot the hostname disappears
Are you using DHCP to assign IP Address ? and if you are using a static IP, try to stop NetworkManager & prevent it to start after reboot. Also check /etc/rc.local to see if there is something that might be changing hostname after reboot. Do these & let us know.