Modify hostname under CentOS 7

View the current hostname

Method 1

[root@localhost ~]# hostname

Method 2: centos7 and above

[root@localhost ~]# hostnamectl
   Static hostname: localhost.localdomain
         Icon name: computer-vm
           Chassis: vm
        Machine ID: e147b422673549a3b4fda77127bd4bcc
           Boot ID: 0fdf38042c5e43c28ef3f1ec5cba182a
    Virtualization: oracle
  Operating System: CentOS Linux 7 (Core)
       CPE OS Name: cpe:/o:centos:centos:7
            Kernel: Linux 3.10.0-327.4.5.el7.x86_64
      Architecture: x86-64

Temporary host name change

Temporary changes that do not take effect after restarting the server.

[root@localhost ~]# hostname 
[root@localhost ~]# hostname 

Check the IP address corresponding to this machine name.

[root@localhost ~]# hostname -i

Permanent host name change

Method 1: Use the hostnamectl command, contos7 and above are applicable

[root@localhost ~]# hostnamectl set-hostname

Method 2: Modify the configuration file /etc/hostname to save and exit

[root@localhost ~]# vi /etc/hostname

Note: Both of the above permanent modification methods require a reboot of the machine to take effect.

[root@localhost ~]# reboot

Leave a Reply