CentOS7 modify hostname

In CentOS, there are three defined hostnames: static, transient, and pretty. A "static" hostname, also known as the kernel hostname, is the hostname that the system automatically initializes from /etc/hostname at boot time. A "transient" hostname is a hostname that is temporarily assigned while the system is running, for example, via a DHCP or mDNS server. Both static and transient hostnames follow the same character restrictions as Internet domain names. "Flexible" hostnames, on the other hand, allow free-form (including special/whitespace characters) hostnames to be displayed to end users (eg myhostname ).

In CentOS 7, there is a command line tool called hostnamectl which allows you to view or modify hostname related configuration.

1. To view hostname related settings: # hostnamectl  

# hostnamectl status  2. To view only static, transient or flexible hostnames, use the "--static", "--transient" or "--pretty" options respectively. # hostnamectl --static # hostnamectl --transient # hostnamectl --pretty



3. To modify all three hostnames at the same time: static, transient and flexible: # hostnamectl set-hostname myhostname As shown above, when modifying static /transient hostnames, any special or whitespace characters will is removed, and any uppercase letters in the supplied argument are automatically converted to lowercase. Once the static hostname is modified, /etc/hostname will be updated automatically. However, /etc/hosts is not updated to save changes, so you must manually update /etc/hosts every time you change the hostname before restarting CentOS 7. Otherwise the system will be very slow when restarting.

4. Manually update /etc/hosts

vim /etc/hosts

127.0.0.1    myhostname#127.0.0.1  localhost localhost.localdomain localhost4 localhost4.localdomain::1        localhost localhost.localdomain localhost6 localhost6.localdomai

5.重启CentOS 7 之后(reboot -f ),# hostname# hostnamectl --transient # hostnamectl --static# hostnamectl --pretty



6. If you only want to modify a specific hostname (static, transient or flexible), you can use the "--static", "--transient" or "--pretty" options. For example, to permanently change the hostname, you can change the static hostname: # hostnamectl --static set-hostname myhostname

Actually, you don't have to reboot the machine to activate permanent hostname modification. The above command immediately modifies the kernel hostname. Log out and log back in to observe the new static hostname at the command line prompt.

 


Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325483501&siteId=291194637