How to modify host names on CentOS 7
In CentOS, there are three definitions of host names: static, transient (transient), and flexible (pretty). The "Static" hostname, also known as the kernel hostname, is the host name that the system automatically initializes from/etc/hostname at startup. A "transient" hostname is a host name that is temporarily assigned when the system is running, for example, through DHCP or MDNs server assignment. The static hostname and transient host name comply with the same character restriction rules as the Internet domain name. On the other hand, the "flexible" hostname allows the use of a host name of free form (including special/whitespace characters) to be displayed to the end user (such as LINUXIDC).
In CentOS 7, there is a command-line tool called Hostnamectl that allows you to view or modify the configuration associated with a host name.
1. To view the hostname-related settings:
[[[email protected] ~]# hostnamectl
Static hostname: Localhost.localdomain
Icon name:computer
chassis:n/ A
machine id:80a4fa4970614cf6be9597ecd6f097a9
Boot I D:28420E272E1847A583718262758BD0F7
virtualization:vmware
Operating system:centos Linux 7 ( Core)
CPE OS name:cpe:/o:centos:centos:7
Kernel:lin UX 3.10.0-123.el7.x86_64
architecture:x86_64
or
[Email protected] ~]# hostnamectl status
Static Hostname:localhost.localdomain
Icon Name:computer
chassis:n/a
Machine ID:80A4FA4970614CF6BE9597ECD6F097A9
Boot ID:28420E272E1847A583718262758BD0F7
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
2. View only static, transient, or flexible hostname, using the "--static", "--transient" or "--pretty" options respectively.
[Email protected] ~]# Hostnamectl--static
Localhost.localdomain
[Email protected] ~]# Hostnamectl--transient
Localhost.localdomain
[Email protected] ~]# Hostnamectl--pretty
3. To modify all three host names simultaneously: static, transient, and flexible hostname:
[Email protected] ~]# Hostnamectl set-hostname LINUXIDC
[Email protected] ~]# Hostnamectl--pretty
Linuxidc
[Email protected] ~]# Hostnamectl--static
Linuxidc
[Email protected] ~]# Hostnamectl--transient
Linuxidc
As shown above, when modifying a static/transient hostname, any special characters or whitespace characters are removed, and any uppercase letters in the supplied parameters are automatically converted to lowercase. Once the static hostname has been modified,/etc/hostname will be automatically updated. However,/etc/hosts does not update to save the changes, so you must manually update the/etc/hosts after you modify the hostname, and then restart CentOS 7. Otherwise, the system will be slow to start again.
4. Manual Update/etc/hosts
Vim/etc/hosts
127.0.0.1 LINUXIDC Hunk_zhu
#127.0.0.1 localhost localhost.localdomain localhost4 localhost4.localdomain
:: 1 localhost localhost.localdomain localhost6 localhost6.localdomai
5. After restarting CentOS 7 (reboot-f),
[Email protected] ~]# hostname
Linuxidc
[Email Protected]_zhu ~]# hostnamectl--transient
Linuxidc
[Email Protected]_zhu ~]# hostnamectl--static
Linuxidc
[Email Protected]_zhu ~]# hostnamectl--pretty
Linuxidc
6. If you only want to modify a specific hostname (static, transient or flexible), you can use the "--static", "--transient" or "--pretty" option.
For example, to permanently modify a host name, you can modify the static hostname:
[Email protected] ~]# hostnamectl--static set-hostname LINUXIDC
After restarting CentOS 7 (reboot-f),
[Email protected] ~]# Hostnamectl--static
Linuxidc
[Email Protected]_zhu ~]# hostnamectl--transient
Linuxidc
[Email Protected]_zhu ~]# hostnamectl--pretty
Linuxidc
[Email Protected]_zhu ~]# hostname
In fact, you do not have to restart the machine to activate the permanent hostname modification. The command above immediately modifies the kernel hostname. Log out and re-login to observe the new static hostname at the command prompt.
How to modify host names on CentOS 7