• Skip to primary navigation
  • Skip to main content
  • Skip to primary sidebar
  • Skip to footer navigation

The Geek Diary

  • OS
    • Linux
    • CentOS/RHEL
    • Solaris
    • Oracle Linux
    • VCS
  • Interview Questions
  • Database
    • oracle
    • oracle 12c
    • ASM
    • mysql
    • MariaDB
  • DevOps
    • Docker
    • Shell Scripting
  • Big Data
    • Hadoop
    • Cloudera
    • Hortonworks HDP

How to change hostname in CentOS/RHEL 7

by admin

Unlike CentOS/RHEL 6, manually appending “HOSTNAME=xxxxx” into file /etc/sysconfig/network and restarting system will not work on CentOS/RHEL 7, in order to change/set the hostname. There 4 ways to change the hostname in CentOS/RHEL 7 :

You can use either of following methods to change the hostname
1. use hostname control utility: hostnamectl
2. use NetworkManager command line tool: nmcli
3. use NetworkManager text user interface tool : nmtui
4. edit /etc/hostname file directly (a reboot afterwards is required)

hostname types
We can configure 3 hostname types is CentOS/RHEL 7 :

Hostname Type Description
Static Assigned by the system admin
Dynamic Assigned by DHCP or mDNS server at runtime
Pretty Assigned by the system admin. Its can be used as Description like “Oracle DB server”

Out of these 3, only static hostname is mandatory. Other 2 are optional.

Method 1 : hostnamectl

To get the current hostname of the system :

# hostnamectl status
   Static hostname: localhost.localdomain
         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

To set new hostname (geeklab) for the machine :

# hostnamectl set-hostname geeklab    ## static
# hostnamectl set-hostname "Geeks LAB"   ## pretty

Re-login and verify the new hostname :

# hostnamectl
   Static hostname: geekslab
   Pretty hostname: Geeks LAB
         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 2 : 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 3 : 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 4 : 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

Filed Under: CentOS/RHEL 7, Linux

Some more articles you might also be interested in …

  1. What are the Network Bonding Modes In CentOS / RHEL
  2. How to make CentOS/RHEL 7 FIPS 140-2 compliant
  3. How to Create, restore, and delete snapshots of virtual machine images in RedHat Virtualization
  4. perf Command Examples in Linux
  5. diff: command not found
  6. a2enmod Command Examples in Linux
  7. gsettings Command Examples in Linux
  8. “yum history” command examples to display, rollback, redo, undo yum transactions
  9. How to Configure Remote Rsyslog To Accept TLS and Non-TLS in CentOS/RHEL
  10. nsnake: command not found

You May Also Like

Primary Sidebar

Recent Posts

  • raw: command not found
  • raw Command Examples in Linux
  • rankmirrors Command Examples in Linux
  • radeontop: command not found

© 2023 · The Geek Diary

  • Archives
  • Contact Us
  • Copyright