1. Introduction
hostnamectl is a newly added command in centos7. It is used to manage the three types of hostnames used in a given host.
There are three types of hostnames defined in CentOS7: static, transient, and pretty. The "static" hostname is a traditional hostname that can be selected by the user and is stored in the /etc/hostname file. The "transient" hostname is a dynamic hostname maintained by the kernel. It is initially the default static host name, and its value defaults to "localhost". The default value can be changed by DHCP or mDNS at runtime. The "pretty" hostname is a UTF8 host name in any format provided to the user.
Two, usage
1. Parameters
-h --help 显示帮助
--version 显示安装包的版本
--transient 修改临时主机名
--static 修改瞬态主机名
--pretty 修改灵活主机名
-P --privileged 在执行之前获得的特权
--no-ask-password 输入密码不提示
-H --host=[USER@]HOST 操作远程主机
Commands:
status 显示当前主机名设置
set-hostname NAME 设置系统主机名
set-icon-name NAME 为主机设置icon名
set-chassis NAME 设置主机平台类型名
2. Specific usage
2.1 View all hostnames
[root@localhost ~]# hostnamectl status
Static hostname: localhost.localdomain
Icon name: computer-vm
Chassis: vm
Machine ID: e9b05280560a4af4a6af98b31c11abda
Boot ID: 0130f463c0fa4d429d117169a2e895e3
Virtualization: vmware
Operating System: CentOS Linux 7 (Core)
CPE OS Name: cpe:/o:centos:centos:7
Kernel: Linux 3.10.0-1127.el7.x86_64
Architecture: x86-64
If no option is specified, the status option is used by default.
hostnamectl <==> hostnamectl status
2.2 Set all hostnames
[root@localhost ~]# hostnamectl set-hostname 主机名
This will make pretty, static and transient hostnames similar. Static and transient host names will be simplified to pretty host name format. Use "-" to replace spaces and delete special characters.
process
[root@localhost ~]# hostname
localhost.localdomain
[root@localhost ~]# hostnamectl set-hostname xsnq //此时已近修改成功
[root@localhost ~]# hostname //查看主机名已近修改成功
xsnq
//这里远程登录(CRT)重新连接服务器也可以
[root@localhost ~]# init 6 //重启服务器
[root@xsnq ~]# //主机名显示也修改完成
2.3 Set/clear specific hostname
2.3.1 Settings
[root@localhost ~]# hostnamectl set-hostname name option..
name: host name.
option: is one or more options among -pretty, -static, and -transient.
If the --static or --transient option is used with the --pretty option, the static and transient host names will be simplified to the pretty host name format. Use "-" to replace spaces and delete special characters. If the --pretty option is not used, no simplification will occur.
2.3.2 Clear the specific hostname and restore it to the default form
[root@localhost ~]# hostnamectl set-hostname "" option...
process
[root@xsnq ~]# hostname
xsnq
[root@xsnq ~]# hostnamectl set-hostname "" --static
[root@xsnq ~]# hostname //此时清除成功,但还未生效
xsnq
[root@lxsnq ~]# init 6 //重启
[root@localhost ~]# hostname //已还原成默认形式
localhost.localdomain
Three, the difference between hostname and hostnamectl
hostname modifies the host name temporarily;
hostnamectl modifies the host name permanently, which is to modify the content of the /etc/hostname file.
If there are any errors in this article, please leave a comment and correct me.