How to change hostname on CentOS or RHEL 7

Question: What is a proper way to change hostname on CentOS / RHEL 7 (permanently or temporarily)?

In CentOS or RHEL, there are three kinds of hostnames defined: (1) static, (2) transient, and (3) pretty. The "static" hostname is also known as kernel hostname, which is initialized from /etc/hostname automatically at boot time. The "transient" hostname is 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., Dan's Computer).

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

To view hostname related settings:

$ hostnamectl status

To view static, transient or pretty hostname only, use "--static", "--transient" or "--pretty" option, respectively.

$ hostnamectl status [--static|--transient|--pretty]

To change all three hostnames: static, transient, and pretty, simultaneously:

$ sudo hostnamectl set-hostname <host-name>

As shown above, in case of static/transient hostnames, any special or whitespace character will be removed, and any uppercase letter will automatically be converted to lowercase in the supplied <host-name> argument. Once the static hostname is changed, /etc/hostname will automatically be updated accordingly. However, /etc/hosts will not be updated to reflect the change, so you need to update /etc/hosts manually.

If you want to change a particular hostname only (static, transient or pretty), you can use "--static", "--transient" or "--pretty" option.

For example, to change hostname permanently, you can change the static hostname:

$ sudo hostnamectl --static set-hostname <host-name>

Note that you do not have to reboot the machine to activate permanent hostname change. The above command will change kernel hostname immediately. Log out, and log back in to see the new static hostname in the command-line prompt.

Final note: the official recommendation by Red Hat is that both static and transient names of a host match the FQDN (fully-qualified domain name) of the host (e.g., myhost.xmodulo.com). FQDN is always preferred to avoid any confusion in host naming in any kind of service deployment. Thus, if a host is assigned by DNS myhost.xmodulo.com as its FQDN, use the same FQDN as its hostname:

$ sudo hostnamectl set-hostname myhost.xmodulo.com
Download this article as ad-free PDF (made possible by your kind donation): 
Download PDF

Subscribe to Ask Xmodulo

Do you want to receive Linux related questions & answers published at Ask Xmodulo? Enter your email address below, and we will deliver our Linux Q&A straight to your email box, for free. Delivery powered by Google Feedburner.


Support Xmodulo

Did you find this tutorial helpful? Then please be generous and support Xmodulo!

One thought on “How to change hostname on CentOS or RHEL 7

Leave a comment

Your email address will not be published. Required fields are marked *