Thứ Hai, 22 tháng 9, 2014

Change hostname on CentOS 7

After installing the CentOS 7 on my server, i tried to change host name by modifying the /etc/sysconfig/network; it did not take an effect of the modification. Even after multiple reboot of server, the host name remains localhost.localdomain. The procedure to change the host name in CentOS 7 is now totally different from the previous version, this guide will help you to setup the host name on CentOS 7 / RHEL 7.
CentOS 7 supports three class of Host Names:
Static – The static host name is traditional host which can be chosen by the user and is stored in /etc/hostname file.
Transient – The transient host name is maintained by kernel and can be changed by DHCP and mDNS.
Pretty – It is a free form UTF -8 host name for the presentation to the user.
HostName can be,
  • 64 character in a length
  • Recommend to have FQDN
  • Consists of a-z,A-Z, 0-9, “-”, “_” and “.” only
How to Change:
Before changing the host name, lets check the current host name.
[root@localhost ~]# hostname
localhost.localdomain
1. nmtui tool: you using command nmtui
NetworkManaget tool is used to set the static host name in /etc/hostname file.

Set the host name.



restart the hostnamed to force the hostnamectl to notice the change in static host name.

[root@localhost ~]# systemctl restart systemd-hostnamed
You can verify the change in host name.
[root@localhost ~]# hostname
web.sysadmin.com.vn
[root@localhost ~]# cat /etc/hostname
web.sysadmin.com.vn
[root@localhost ~]# cat /etc/sysconfig/network
# Created by anaconda
sysadmin
[root@localhost ~]#
2. hostnamectl:
hostnamectl is used to change the host name, with this tool we can change all the three class of host name; here we look only static host name.
Check the current host name.
[root@server ~]# hostnamectl status
[root@localhost ~]# hostnamectl status
   Static hostname: web.sysadmin.com.vn
         Icon name: computer
           Chassis: n/a
        Machine ID: 3132ab42b3b7405bbdc647ce36ca659f
           Boot ID: 0938b49f7e4045b899a110ccf17b54a7
    Virtualization: vmware
  Operating System: CentOS Linux 7 (Core)
       CPE OS Name: cpe:/o:centos:centos:7
            Kernel: Linux 3.10.0-123.6.3.el7.x86_64
      Architecture: x86_64
Set the hostname.
[root@server ~]# hostnamectl set-hostname web.sysadmin.com.vn
Check the host name again (Close the session and open new session using putty or console)
[root@client ~]# hostnamectl status
[root@localhost ~]# hostnamectl status
   Static hostname: web.sysadmin.com.vn
         Icon name: computer
           Chassis: n/a
        Machine ID: 3132ab42b3b7405bbdc647ce36ca659f
           Boot ID: 0938b49f7e4045b899a110ccf17b54a7
    Virtualization: vmware
  Operating System: CentOS Linux 7 (Core)
       CPE OS Name: cpe:/o:centos:centos:7
            Kernel: Linux 3.10.0-123.6.3.el7.x86_64
      Architecture: x86_64
If you use this command, you do not require to notify the change in host name. Close the current session and re launch the terminal.
3. nmcli tool:
It can be used to query and setup the static host name in /etc/hostname file.
Check the hostname.
[root@localhost ~]# nmcli general hostname
web.sysadmin.com.vn
Change the host name.
[root@localhost ~]# nmcli general hostname web.sysadmin.com.vn
restart the hostnamed to force the hostnamectl to notice the change in static host name.
[root@localhost ~]# systemctl restart systemd-hostnamed
4. Edit /etc/hostname
This is the simple, but requires a reboot of server to take an effect.
Note: Use the hostnamectl to change the host name, which fair better than other commands and does not require to update the kernel about the change in host name.