Basic Linux Tutorial: how to modify the host name on CentOS or RHEL 7
Q: What is the correct method for modifying the host name on CentOS/RHEL 7 (either permanently or temporarily )?
In CentOS or RHEL, there are three defined host names: a, static (static), B, transient (transient), and c, flexible (pretty ). The "static" host name is also called the kernel host name. It is the host name automatically initialized from/etc/hostname at startup. The "transient" host name is a temporary host name allocated during system operation, such as through DHCP or mDNS server. Both the static host name and the transient host name follow the same character restriction rules as the Internet domain name. On the other hand, "flexible" host names allow the use of free-form (including special/blank characters) host names to be displayed to end users (such as Dan's Computer ).
In CentOS/RHEL 7, a command line tool named hostnamectl allows you to view or modify host name-related configurations.
To view host name settings:
- $ Hostnamectl status
Only view static, transient, or flexible host names. Use the "-- static", "-- transient", or "-- pretty" Options respectively.
- $ Hostnamectl status [-- static | -- transient | -- pretty]
You need to modify all three host names at the same time: static, transient, and flexible host names:
- $ Sudo hostnamectl set-hostname
As shown above, when modifying static/transient host names, any special or blank characters are removed, and any uppercase letters in the provided parameters are automatically converted to lowercase letters. Once the static host name is modified,/etc/hostname will be automatically updated. However,/etc/hosts will not be updated to save the changes, so you need to manually update/etc/hosts.
If you only want to modify a specific host name (static, transient, or flexible), you can use the "-- static", "-- transient", or "-- pretty" options.
For example, to permanently modify the host name, you can modify the static Host Name:
- $ Sudo hostnamectl -- staticset-hostname
Note that you do not have to restart the machine to activate permanent host name modification. The preceding command immediately modifies the kernel host name. After logging out and logging on again, you will be prompted on the command line to observe the new static host name.
Impact of host name modification on Oracle startup and shutdown on Linux
How to display and set host names in Linux
Linux host name change
Change the host name in Linux (Ubuntu + RedHat)
Steps for modifying the host name in Linux