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.

 

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 command ‘hostname’.

 

Second Way

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

$ hostnamectl set-hostname “centos7.linux.com”

To view the changes hostname, only run ‘hostnameclt‘ command,

$ hostnamectl

hostnamectl

If you completely want to remove the hostname, use ‘hostnamectl set-hostname “” ‘. This remove the hostname completely.

 

Third way

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

$ nmtui

hostnamectl

next, select ‘set system hostname’ & click ‘ok and enter your desired hostname , press ‘ok‘ & its done.

hostnamectl
Restart hostnamed service to implement the changes.

$ systemctl restart systemd-hostnamed

verify new hostname by running ‘hostname’ or ‘hostnamectl’.

 

Fourth Way

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,

$ 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

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

hostnamectl

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.

 

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

Connect to us: Facebook | Twitter

| Google Plus

Become a Supporter – Make a contribution via PayPal

[paypal_donation_button align=”left” border=”1″]

Linux TechLab is thankful for your continued support.

Shusain

Passionate about Linux & open source. Loves to learn, read & write about Linux as well as new technologies.

You may also like...

3 Responses

  1. Nazia says:

    I am not able to change the hostame with either of these methods.. After reboot the hostname disappears

    • shsuain says:

      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.

Leave a Reply