CentOS 7 host name modification and viewing command details

Source: Internet
Author: User

CentOS 7 host name modification and viewing command details

In CentOS 6, the host name modification method is simple. You can use the hostname command to temporarily modify the Host Name and permanently change the host name to the file/etc/sysconfig/network.

However, in CentOS 7, the host name is not that simple, and it involves some bending.

In CentOS 7, host names are divided into three categories: static (static host name), pretty (nice-looking, easy-to-read host name), and transient (temporary ). The file related to the host name in CentOS 7 is/etc/hostname, which is read during system initialization and the kernel sets the transient host name based on its content.

Where:
1. The host name of the static class is what we often call the host name, which is determined by the/etc/hostname file.
2. The host name of the transient class is also known as the temporary host name, which is dynamically maintained by the kernel. By default, the static host names in the/etc/hostname file are initialized at system startup.
3. The host name of the pretty class is displayed. It can provide a non-standard host name, which is not available in previous versions (such as CentOS 6. It can contain special characters, such as spaces. For example, the pretty name is named "MaYun's Host", which is not allowed in the previous Host name (that is, static Host name.

The static host name in the/etc/hostname file takes effect instantly and permanently. After modification, use the hostname command or uname-n to directly read the file. After restart, initialize the file according to the host name of the file.

When the/etc/hostname file does not have a host name, the kernel will initialize transient as localhost. localdomain at system startup.

The/etc/sysconfig/network file is invalid.

Modify and view CentOS 7 host names
1. Run the hostname command to modify the host name. The modification is the transient host name, that is, the host name temporarily takes effect.
2. directly modify the/etc/hostname file, which takes effect instantly and takes effect after restart (because the kernel will initialize the transient host name based on it ).
3. Run the nmtui command to modify the host name on the GUI. It directly modifies the/etc/hostname file, so it also takes effect instantly + permanently.
4. Use the hostnamectl command. It can modify and view three host names: static, transient, and pretty. When the static host name is modified, it is directly written into the/etc/hostname file, so it also takes effect instantly + permanently.

Hostnamectl command

1. view the Host Name
Hostnamectl
# Or
Hostnamectl status
# Or
Hostnamectl [-- pretty | -- static | -- transient] status

For example, the current host name is "bkjia.longshuai.com ".
[Root @ bkjia ~] # Uname-n
Bkjia.longshuai.com
[Root @ bkjia ~] # Hostname name1
[Root @ bkjia ~] # Hostnamectl
Static hostname: bkjia.longshuai.com
Transient hostname: name1
Icon name: computer-vm
Chassis: vm
Machine ID: d13bce5e247540a5b5886f2bf8aabb35
Boot ID: d34a4222469e4f1cbe20c27aca174e10
Virtualization: vmware
Operating System: CentOS Linux 7 (Core)
Cpe OS Name: cpe:/o: centos: 7
Kernel: Linux 3.10.0-327. el7.x86 _ 64
Architecture: x86-64

You can see that the transient has changed after you use the hostname command to modify the host name.

2. Modify the three host names at the same time.

When both pretty and (static | transient) are modified, the simplified section of the pretty name is used as the static host name.
Hostnamectl set-hostname NAME

For example:
[Root @ bkjia ~] # Hostnamectl set-hostname name2
[Root @ bkjia ~] # Hostname
Name2
[Root @ bkjia ~] # Cat/etc/hostname
Name2
[Root @ bkjia ~] # Hostnamectl status
Static hostname: name2
Icon name: computer-vm
Chassis: vm
Machine ID: d13bce5e247540a5b5886f2bf8aabb35
Boot ID: d34a4222469e4f1cbe20c27aca174e10
Virtualization: vmware
Operating System: CentOS Linux 7 (Core)
Cpe OS Name: cpe:/o: centos: 7
Kernel: Linux 3.10.0-327. el7.x86 _ 64
Architecture: x86-64
[Root @ bkjia ~] # Hostnamectl -- pretty

[Root @ bkjia ~] #

We can see from the results that only static and transient are changed (kernel Dynamic Maintenance will definitely change), but pretty is not set successfully. This is because the host name "name2" is a name that complies with the host name standard. If you specify a non-standard host name, such as containing special characters, pretty is also set.

For example:
[Root @ bkjia ~] # Hostnamectl set-hostname "name22 name22"
[Root @ bkjia ~] # Hostnamectl
Static hostname: name22name22
Pretty hostname: name22 name22
Icon name: computer-vm
Chassis: vm
Machine ID: d13bce5e247540a5b5886f2bf8aabb35
Boot ID: d34a4222469e4f1cbe20c27aca174e10
Virtualization: vmware
Operating System: CentOS Linux 7 (Core)
Cpe OS Name: cpe:/o: centos: 7
Kernel: Linux 3.10.0-327. el7.x86 _ 64
Architecture: x86-64

The pretty hostname has been changed, and the static hostname is its "simplified version ".

3. modify a host name Type
Hostnamectl set-name NAME name -- static
Hostnamectl set-name NAME -- transient
Hostnamectl set-name NAME -- pretty

Use the example above.

4. Modify the two names at the same time.
Hostnamectl set-name NAME -- static -- transient
Hostnamectl set-name NAME -- static -- pretty
Hostnamectl set-name NAME -- transient -- pretty

Use the example above. However, when you modify the pretty host name and the other one, the pretty "simplified version" is used ".

5. Modify and view the host Name of the remote host, and use the "-H" or "-- host" option. The connection is based on SSH.

Note: you cannot remotely modify the Host Name of CentOS 5 or 6 because it uses the systemd command to modify the host name.
Hostnamectl-H [USER @] HOST set-hostname NAME
Hostnamectl-H [USER @] HOST status

For example, use the root user to connect to host 192.168.100.59 and modify its host name.
Hostnamectl-H root@192.168.100.59 set-hostname hello59
Hostnamectl-H root@192.168.100.59 status

Https://www.bkjia.com/topicnews.aspx? Tid = 14

This article permanently updates link: https://www.bkjia.com/Linux/2018-03/151464.htm

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.