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.

https://services.vlitag.com/vpaid/?q=9ba3f2539ddb6b2df45a4f25fbfd7843&defaultVolume=&page_url=https://linuxtechlab.com

$ 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

rhel change hostname

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

change hostname in centos 7

next, select 'set system hostname' & click 'ok and enter your desired hostname , press 'ok' & it's done.

rhel change hostname
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

change hostname in centos 7

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.

If you think we have helped you or just want to support us, please consider these:-

Connect to us: Facebook | Twitter

Linux TechLab is thankful for your continued support.