Saturday, 4 April 2020

hostname change

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
 
=======================================================

This document describes how to edit hostname in Oracle Linux 7.

Note: manually append "HOSTNAME=xxxxx" into file /etc/sysconfig/network and restart system will not work on Oracle Linux 7.

 

Solution

To edit hostname in Oracle Linux 7, use either of following methods:

1. use hostname control utility: hostnamectl
2. use NetworkManager command line tool: nmcli
3. edit /etc/hostname file directly (a reboot afterwards is required)


1. Use hostnamectl which is a command-line client to systemd-hostnamed service


Get current hostname status:

[root@localhost ~]# hostnamectl status
   Static hostname: localhost.localdomain
         Icon name: computer
           Chassis: n/a
        Machine ID: ed264df49f3f42b7b64ca75f763ad117
           Boot ID: 3ec1705218b9499b9619e1bdca7be737
    Virtualization: oracle
  Operating System: Oracle Linux Server 7.1
       CPE OS Name: cpe:/o:oracle:linux:7:1
            Kernel: Linux 3.8.13-55.1.6.el7uek.x86_64
      Architecture: x86_64


Set hostname:

[root@localhost ~]# hostnamectl set-hostname oraclelinux.oracle.com
[root@localhost ~]#


Re-login terminal session:

[root@oraclelinux ~]# hostnamectl status
   Static hostname: oraclelinux.oracle.com
         Icon name: computer
           Chassis: n/a
        Machine ID: ed264df49f3f42b7b64ca75f763ad117
           Boot ID: 3ec1705218b9499b9619e1bdca7be737
    Virtualization: oracle
  Operating System: Oracle Linux Server 7.1
       CPE OS Name: cpe:/o:oracle:linux:7:1
            Kernel: Linux 3.8.13-55.1.6.el7uek.x86_64
      Architecture: x86_64

2. nmcli


Check current hostname:

[root@localhost ~]# nmcli general hostname
localhost.localdomain


Modify hostname and restart ystemd-hostnamed service:

[root@localhost ~]# nmcli general hostname oraclelinux.oracle.com
[root@localhost ~]# systemctl restart systemd-hostnamed


Re-login terminal session:

[root@oraclelinux ~]# hostname
oraclelinux.oracle.com

 

3. Edit /etc/hostname


View current content:

[root@localhost ~]# cat /etc/hostname
localhost.localdomain


Replace file content with new hostname:

[root@localhost ~]# echo "oraclelinux.oracle.com" > /etc/hostname
[root@localhost ~]# cat /etc/hostname
oraclelinux.oracle.com


Restart system and verify:

[root@oraclelinux ~]# hostname
oraclelinux.oracle.com