
1.1 主机名配置
1.1.1 设置主机名
1.概述
主机名(Hostname)为计算机系统、交换机、路由器等设备的名称,在网络中可用于对设备进行标识。
在一个局域网中,每台机器都有一个主机名,便于主机间的区分,同一网络中不能有两个主机名相同的系统。主机名通常使用容易记忆的方法进行设置,可以根据每台机器的功能对其进行命名,此方法便于主机间的相互访问,尤其在部署集群的时候更为方便。
在Linux系统中,有多种方式设置主机名,以下介绍两种常见版本主机名的配置方式。
2.目标
在Linux操作系统中设置主机名。
3.准备
操作系统:CentOS 7.3。
4.考点1:临时配置命令hostname
hostname命令用来显示或设置当前系统的主机、域或节点名,只改变临时主机名(transient hostname),对应修改的文件为/proc/sys/kernel/hostname,系统重启之后该文件被静态主机名(static hostname)进行覆盖。
语法格式:

常用参数如表1-1所示。
表1-1 hostname常用参数

使用hostname命令修改主机名为qingjiao,如图1-1所示。

图1-1 修改临时主机名
5.考点2:永久配置命令hostnamectl
hostnamectl(hostname control)用于查询和更改系统主机名和相关配置,修改静态主机名,通过该方式配置主机名后,系统会自动将设置信息写入配置文件/etc/hostname,无须额外编辑,修改后静态主机名和临时主机名都发生变化,永久生效。
使用hostnamectl命令修改主机名后,可以使用bash命令使配置生效。
语法格式:

常用参数如表1-2所示。
表1-2 hostnamectl常用参数

使用hostnamectl查看系统主机名,如图1-2所示。

图1-2 查看系统主机名
从上图1-2可以看到,静态主机名为iz8vb2j5hsvhkkoce3qrg4z,临时主机名为qingjiao。
使用hostnamectl命令修改静态主机名为qingjiao,如图1-3所示。

图1-3 修改系统主机名
从图1-3可以看到,此时系统静态主机名已经修改为qingjiao。为了便于使用,可使用bash命令即时生效该主机名,如图1-4所示。

图1-4 生效主机名