Modify the Host Name of CentOS 7

Source: Internet
Author: User

Modify the Host Name of CentOS 7

How to modify the host name on CentOS 7

In CentOS, there are three defined host names: static, transient, and flexible ). 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, the "flexible" host name allows the use of free form (including special/blank characters) host name, to display to end users (such as Linuxidc ).

In CentOS 7, a command line tool named hostnamectl allows you to view or modify host name-related configurations.

1. To view host name settings:
[Root @ localhost ~] # Hostnamectl
Static hostname: localhost. localdomain
Icon name: computer
Chassis: n/
Machine ID: 80a4fa4970614cf6be9597ecd6f097a9
Boot ID: 28420e272e1847a583718262758bd0f7
Virtualization: vmware
Operating System: CentOS Linux 7 (Core)
Cpe OS Name: cpe:/o: centos: 7
Kernel: Linux 3.10.0-123. el7.x86 _ 64
Architecture: x86_64
Or

[Root @ localhost ~] # Hostnamectl status
Static hostname: localhost. localdomain
Icon name: computer
Chassis: n/
Machine ID: 80a4fa4970614cf6be9597ecd6f097a9
Boot ID: 28420e272e1847a583718262758bd0f7
Virtualization: vmware
Operating System: CentOS Linux 7 (Core)
Cpe OS Name: cpe:/o: centos: 7
Kernel: Linux 3.10.0-123. el7.x86 _ 64
Architecture: x86_64

2. Only view static, transient, or flexible host names. Use the "-- static", "-- transient", or "-- pretty" Options respectively.
[Root @ localhost ~] # Hostnamectl -- static
Localhost. localdomain
[Root @ localhost ~] # Hostnamectl -- transient
Localhost. localdomain
[Root @ localhost ~] # Hostnamectl -- pretty

3. modify all three host names at the same time: static, transient, and flexible host names:
[Root @ localhost ~] # Hostnamectl set-hostname Linuxidc
[Root @ localhost ~] # Hostnamectl -- pretty
Linuxidc
[Root @ localhost ~] # Hostnamectl -- static
Linuxidc
[Root @ localhost ~] # Hostnamectl -- transient
Linuxidc


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 modifications. Therefore, you must manually update/etc/hosts each time you modify the host name, and then restart CentOS 7. Otherwise, the system will be slow again.

4. manually update/etc/hosts

Vim/etc/hosts

127.0.0.1 Linuxidc hunk_zhu
#127.0.0.1 localhost. localdomain localhost4 localhost4.localdomain
: 1 localhost. localdomain localhost6 localhost6.localdomai

5. After CentOS 7 is restarted (reboot-f ),
[Root @ Linuxidc ~] # Hostname
Linuxidc
[Root @ hunk_zhu ~] # Hostnamectl -- transient
Linuxidc
[Root @ hunk_zhu ~] # Hostnamectl -- static
Linuxidc
[Root @ hunk_zhu ~] # Hostnamectl -- pretty
Linuxidc

6. If you only want to modify a specific host name (static, transient, or flexible), you can use the "-- static", "-- transient", or "-- pretty" option.
For example, to permanently modify the host name, you can modify the static Host Name:
[Root @ localhost ~] # Hostnamectl -- static set-hostname Linuxidc
After CentOS 7 is restarted (reboot-f ),
[Root @ Linuxidc ~] # Hostnamectl -- static
Linuxidc
[Root @ Hunk_zhu ~] # Hostnamectl -- transient
Linuxidc
[Root @ Hunk_zhu ~] # Hostnamectl -- pretty
Linuxidc
[Root @ Hunk_zhu ~] # Hostname

In fact, 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.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.