How to use hostnamectl command in centos7

Time:2020-11-27

In centos7, a new instruction of hostnamectl is added in the host name change, which is more convenient in output and change than before. Let’s take a look at the help information of hostname

Copy code

The code is as follows:

[[email protected] ~]# hostnamectl –help
hostnamectl [OPTIONS…] COMMAND …
Query or change system hostname.
-h –help Show this help
–version Show package version
–transient Only set transient hostname
–static Only set static hostname
–pretty Only set pretty hostname
-P –privileged Acquire privileges before execution
–no-ask-password Do not prompt for password
-H –host=[[email protected]]HOST Operate on remote host
Commands:
status Show current hostname settings
set-hostname NAME Set system hostname
set-icon-name NAME Set icon name for host
set-chassis NAME Set chassis type for host

View hostname information

Copy code

The code is as follows:

[[email protected] ~]# hostnamectl
Static hostname: 361way
Icon name: computer-desktop
Chassis: desktop
Machine ID: 6917731f5fe447d0bca296ed2802e250
Boot ID: e0d20bc2a3a4474c960394967d33ab53
Operating System: CentOS Linux 7 (Core)
CPE OS Name: cpe:/o:centos:centos:7
Kernel: Linux 3.10.0-123.6.3.el7.x86_64
Architecture: x86_64

Set host name

From the help above, we can see that when setting the host name, there are four kinds of host names, which are explained and explained in the man Manual of hostnamectl, as follows:

This tool distinguishes three different hostnames: the high-level “pretty” hostname which might include all kinds of special characters (e.g. “Lennart’s
Laptop”), the static hostname which is used to initialize the kernel hostname at boot (e.g. “lennarts-laptop”), and the transient hostname which might be
assigned temporarily due to network configuration and might revert back to the static hostname if network connectivity is lost and is only temporarily
written to the kernel hostname (e.g. “dhcp-47-11”).
Note that the pretty hostname has little restrictions on the characters used, while the static and transient hostnames are limited to the usually accepted
characters of Internet domain names.
Change host name

In general, set hostname is used to make changes

Copy code

The code is as follows:

[[email protected] ~]# hostnamectl set-hostname blog

Changing the host name usually involves two files, such as / etc / host name and / etc / machine info. At the same time, the man manual also explains how to modify different names

The static hostname is stored in /etc/hostname, see hostname(5) for more information. The pretty hostname, chassis type, and icon name are stored in /etc/machine-info, see machine-id(5).
The host set icon name is usually used in the graphical interface. Icon name and set chassis in the corresponding hostnamectl output specify the platform type of the host. For example, if the local computer is desktop, it can also be set to “desktop”, “laptop”, “server”, “tablet”, “handset”