Change HostName in CentOS 7 / RHEL 7

Method 1 : nmcli

To view the current hostname :

# nmcli general hostname
localhost.localdomain

To change the hostname to geeklab :

# nmcli general hostname geeklab

We need to restart the systemd-hostnamed service for the changes to take effect :

# service systemd-hostnamed restart

Re-login and erify the hostname change :

# hostname
geeklab

Method 2 : nmtui

We can also change the hostname using the nmtui tool :

nmtui

Select the option to “set the hostname” and hit enter

change hostname nmtui

Set the hostname

set hostname nmtui

Confirm the hostname change

confirm hostname nmtui

Restart the systemd-hostnamed service for the changes to take effect.

# service systemd-hostnamed restart

Re-login and verify the hostname change.

# hostnamectl
   Static hostname: geeklab
         Icon name: computer
           Chassis: n/a
        Machine ID: 55cc1c57c7f24ed0b0d352648024cea6
           Boot ID: a12ec8e04e6b4534841d14dc8425e38c
    Virtualization: vmware
  Operating System: CentOS Linux 7 (Core)
       CPE OS Name: cpe:/o:centos:centos:7
            Kernel: Linux 3.10.0-123.el7.x86_64
      Architecture: x86_64

Method 3 : Edit /etc/hostname

This method requires a reboot of the system. View the current content of the file /etc/hostname.

# cat /etc/hostname
localhost.localdomain

To change the hostname to “geeklab”, replace the content of the /etc/hostname file with “geeklab”

# echo "geeklab" > /etc/hostname
# cat /etc/hostname
geeklab

Restart the system and verify.

# shutdown -r now
# hostname
geeklab
  • server, linux-server
  • 0 Users Found This Useful
Was this answer helpful?

Related Articles

How to delete terminal history in Linux

You can clear your bash history like this: cat /dev/null > ~/.bash_history && history...

How to change SSH Port in Linux

# vi /etc/ssh/sshd_configYou have to change port 22 to whatever you want#Port 22 #ta delete kore...

How to install wget on CentOS 7/8

The procedure to install wget command is as follows on CentOS Linux 7/8: Open a terminal...

How to install Webuzo on CentOS 7

Requirements Newly installed CentOS 6.x / 7.x or Red Hat Enterprise Linux 6.x / 7.x or...

How to block IP address on a Linux Server

To block an IP on your Linux server you need to use iptables tools  and netfilter firewall. First...