How to change hostname on CentOS or RHEL 7

Last updated on September 18, 2020 by Dan Nanni

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 or later, 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, note that /etc/hosts will not be updated to reflect the change, so you need to update /etc/hosts manually yourself.

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., 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 as its FQDN, use the same FQDN as its hostname:

$ sudo hostnamectl set-hostname

To change hostnames on other Linux distributions, you can also check out this more general tutorial.

Support Xmodulo

This website is made possible by minimal ads and your gracious donation via PayPal or credit card

Please note that this article is published by under a Creative Commons Attribution-ShareAlike 3.0 Unported License. If you would like to use the whole or any part of this article, you need to cite this web page at as the original source.

Xmodulo © 2021 ‒ AboutWrite for UsFeed ‒ Powered by DigitalOcean