Home  >  Article  >  System Tutorial  >  Steps to change hostname on CentOS or RHEL 7

Steps to change hostname on CentOS or RHEL 7

WBOY
WBOYforward
2024-01-01 11:31:29841browse

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

In CentOS or RHEL, there are three defined host names:

a, static,

b, transient,

c, flexible (pretty).

The "static" host name, also known as the kernel host name, is the host name that the system automatically initializes from /etc/hostname at startup. A "transient" hostname is a hostname that is temporarily assigned while the system is running, for example, via DHCP or an mDNS server. Both static and transient hostnames are subject to the same character limit rules as Internet domain names. "Flexible" hostnames, on the other hand, allow the use of free-form (including special/whitespace characters) hostnames for presentation to end users (such as Dan's Computer).

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

To view host name related settings:

$ hostnamectl status

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

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

To modify all three hostnames at the same time: static, transient, and flexible hostnames:

$ sudo hostnamectl set-hostname

As shown above, when modifying static/transient hostnames, any special characters or whitespace characters will be removed, and any uppercase letters in the provided parameters will automatically be converted to lowercase. Once the static hostname is modified, /etc/hostname will be automatically updated. However, /etc/hosts is not updated to save the changes, so you need to update /etc/hosts manually.

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:

$ sudo hostnamectl --staticset-hostname

Note that you do not have to reboot the machine to activate permanent hostname modification. The above command will immediately modify the kernel hostname. Log out and back in at the command prompt to observe the new static hostname.

The above is the detailed content of Steps to change hostname on CentOS or RHEL 7. For more information, please follow other related articles on the PHP Chinese website!

Statement:
This article is reproduced at:jb51.net. If there is any infringement, please contact admin@php.cn delete