Wenjian's Blog

Live and Learn

Tag Archives: CentOS

HOWTO: Change hostname in CentOS 7

In CentOS 7, there are three kinds of hostnames defined: static, transient, and pretty. The static hostname is also known as kernel hostname, which is initialized from /etc/hostname automatically at boot time. The transient hostname – a temporary hostname assigned at run time, for example, by a DHCP or mDNS server. Both static and transient hostnames follow the same character restriction rules as Internet domain names. On the other hand, the pretty hostname is allowed to have a free-form (including special/whitespace characters) hostname, presented to end users (e.g., Wenjian’s VPS).

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

1. To view hostname related settings

root@vps-wenjian ~>hostnamectl status

hostnamectl_status

To view static, transient or pretty hostname only, use “–static”, “–transient” or “–pretty” option, respectively.

2. To change all three hostnames simultaneously

root@vps-wenjian ~ >hostnamectl set-hostname vps-wenjian

3. To update /etc/hosts manually

root@vps-wenjian ~ >vim /etc/hosts
#127.0.0.1 localhost localhost.localdomain localhost4 localhost4.localdomain4
127.0.0.1 vps-wenjian
::1 localhost localhost.localdomain localhost6 localhost6.localdomain6