centos 修改主机名

centos 修改主机名

1
2
3
4
5
6
7
8
9
10
11
12
13
[root@local-server ~]# hostnamectl status
Static hostname: localhost.localdomain
Pretty hostname: mini-host
Transient hostname: local-server
Icon name: computer-laptop
Chassis: laptop
Machine ID: 831ba05f9eb04b7db36ed8a76d77e833
Boot ID: 7f5fad8ac55549f5851de6f3a5bcbeeb
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

如果有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)

  1. The high-level “pretty” hostname which might include all kinds of special characters (e.g. “Lennart’s Laptop”);–pretty主机名对字符几乎没有限制,可以设置为你喜欢的任意主机名;
  2. The static hostname which is used to initialize the kernel hostname at boot (e.g. “lennarts-laptop”);–静态主机名用于系统启动时设置内核参数;存储于/etc/hostname中。
  3. 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.—临时主机名默认从网络配置中获取,如果静态主机名生效,则临时主机名没用。

参考文献

Linux修改主机名(静态主机名、临时主机名)_transient hostname_那个少年的博客-CSDN博客

打赏
  • 版权声明: 本博客所有文章除特别声明外,著作权归作者所有。转载请注明出处!
  • Copyrights © 2018-2024 Outsrkem
  • 访问人数: | 浏览次数:

      请我喝杯咖啡吧~

      支付宝
      微信