centos 修改主机名
1 | [root@local-server ~]# hostnamectl status |
如果有Transient hostname
,则显示Transient hostname
,否则显示Static hostname
设置Transient hostname
使用如下命令
1 | hostnamectl set-hostname --transient local-server |
有些软件会偷偷的改
Transient hostname
由上面hostnamectl的显示信息可知,hostname只改变了Transient hostname(临时主机名)。其原理就是临时修改了/proc/sys/kernel/hostname文件(可以用cat /proc/sys/kernel/hostname查看),懂内核的或许就知道,其文件是Linux系统启动时生成的虚拟文件(proc文件系统),系统重启后此hostname又将以静态hostname覆盖。
主机名分类
1 | man hostnamectl |
基于上面命令,我们可以进一步思考是否可以修改主机名呢?是否用hostname、hostnamectl这两条命令就能修改主机名呢。其实,Linux主机名分为静态主机名(static hostname)、临时主机名(transient hostname)以及pretty 主机名(理解为:优雅的主机名),下面将详细介绍其区别:(摘自man hostnamectl)
- The high-level “pretty” hostname which might include all kinds of special characters (e.g. “Lennart’s Laptop”);–pretty主机名对字符几乎没有限制,可以设置为你喜欢的任意主机名;
- The static hostname which is used to initialize the kernel hostname at boot (e.g. “lennarts-laptop”);–静态主机名用于系统启动时设置内核参数;存储于/etc/hostname中。
- The transient hostname which is a default received from network configuration. If a static hostname is set, and is valid (something other than localhost), then the transient hostname is not used.—临时主机名默认从网络配置中获取,如果静态主机名生效,则临时主机名没用。
参考文献
打赏