社区微信群开通啦,扫一扫抢先加入社区官方微信群
社区微信群
之前写过一篇博客“深入理解Linux修改hostname”,里面总结了RHEL 5.7下面如何修改hostname,当然这篇博客的内容其实也适用于CentOS 6,但是自CentOS 7开始,已经跟CentOS 6已经有很大不同了,一直没有总结CentOS 7下修改hostname的相关知识点,今天恰好遇到了这个问题,处理完后遂总结一下。
CentOS 7中的启动跟CentOS 6有所区别,CentOS 6启动过程中,会执行/etc/rc.d/rc.sysinit init执行的第一个脚本 这个脚本主要是初始化工作,如设置系统字体,启动swapping,设置主机名等等。CentOS7和CentOS6启动流程差不多,只不过到init程序时候,改为了systemd启动了(并行启动),也就是说CentOS 7不会去执行/etc/rc.d/rc.sysinit这个文件(当然也没有这个文件了)读取hostname的配置,CentOS 7新增了配置文件/etc/hostname,系统启动的时候会读取/etc/hostname这个配置文件来初始化内核主机名。
另外,我们可以通过配置/etc/hostname修改hostname。也可以通过新增的hostnamectl命令修改。在CentOS 7中,主机名可以分为下面三种类型:
· 静态主机名(static):静态主机名也称为内核主机名,是系统在启动时初始化内核的主机名,默认从/etc/hostname读取配置自动初始化静态主机名
· 瞬态主机名(transient):瞬时主机名是在系统运行时临时分配的主机名,例如,由DHCP等一些系统临时分配的主机名,如果系统存在静态主机名且有效,则不会用到瞬态主机名。
· 灵活主机名(pretty):静态和瞬态主机名都是要符合域名的字符串,而pretty主机名则可以包含其他一些特殊字符。
There are three 3 types of hostnames.
The static (configured) host name is the one configured in /etc/hostname or a similar file. It is chosen by the local user. It is not always in sync with the current host name as returned by the gethostname() system call. If no host name is configured this property will be the empty string. Setting this property to the empty string will remove /etc/hostname. This hostname should be an internet-style hostname, 7bit ASCII, no special chars/spaces, lower case.
The transient (dynamic) host name is the one configured via the kernel's sethostbyname(). It can be different from the static hostname in case DHCP or mDNS have been configured to change the name based on network information. This property is never empty. If no host name is set this will default to "localhost". Setting this property to the empty string will reset the dynamic hostname to the static host name. If no static host name is configured the dynamic host name will be reset to "localhost". This hostname should be an internet-style hostname, 7bit ASCII, no special chars/spaces, lower case.
The pretty host name is a free-form UTF8 host name for presentation to the user. UIs should ensure that the pretty hostname and the static hostname stay in sync. I.e. when the former is "Lennart's Computer" the latter should be "lennarts-computer". If no pretty host name is set this setting will be the empty string. Applications should then find a suitable fallback, such as the dynamic hostname.
如上英文介绍,静态主机名保存在/etc/hostname中,灵活主机名保存在/etc/machine-info,而瞬态主机名一般由内核参数维护,重启后会丢失。
查看主机名(hostname)
我们可以有很多方式查看主机名,但是我们只能使用命令hostnamectl查看静态、瞬态或灵活主机名,分别使用--static,--transient或--pretty参数。
[root@MyDB ~]# hostname
MyDB
[root@MyDB ~]# cat /etc/hostname
MyDB
[root@MyDB ~]# hostnamectl status
Static hostname: localhost.localdomain
Transient hostname: MyDB
Icon name: computer-desktop
Chassis: desktop
Machine ID: 955dde0d8f7341ebb19a1e247577c410
Boot ID: 4f2df049135e41c795a655cdf36c1c40
Operating System: CentOS Linux 7 (Core)
CPE OS Name: cpe:/o:centos:centos:7
Kernel: Linux 3.10.0-862.el7.x86_64
Architecture: x86-64
[root@MyDB ~]# hostnamectl
Static hostname: MyDB
Pretty hostname: kerry's db
Icon name: computer-desktop
Chassis: desktop
Machine ID: 955dde0d8f7341ebb19a1e247577c410
Boot ID: 459eb877eeb34d7e910f4eec8ef4a42f
Operating System: CentOS Linux 7 (Core)
CPE OS Name: cpe:/o:centos:centos:7
Kernel: Linux 3.10.0-862.el7.x86_64
Architecture: x86-64
[root@MyDB ~]# hostnamectl --static #查看静态主机名
MyDB
[root@MyDB ~]# hostnamectl --transient #查看瞬时主机名
MyDB
[root@MyDB ~]# hostnamectl --pretty #查看灵活主机名
kerry's db
设置静态主机名
[root@MyDB ~]# hostnamectl set-hostname kerrydb
或
[root@MyDB ~]# hostnamectl set-hostname --static kerrydb
设置静态主机名立对新连接的会话立即生效,但是对于当前连接则不生效(例如,SecureCRT新开一个窗口就能看到修改结果)。如下测试所,修改静态主机名会立即修改内核中的kernel.hostname
[root@MyDB ~]# hostnamectl set-hostname MyDB
[root@MyDB ~]#
[root@MyDB ~]# cat /proc/sys/kernel/hostname
mydb
[root@MyDB ~]# hostnamectl set-hostname kerrydb
[root@MyDB ~]# cat /proc/sys/kernel/hostname
kerrydb
[root@MyDB ~]# hostnamectl set-hostname --static yourdb
[root@MyDB ~]# cat /proc/sys/kernel/hostname
yourdb
[root@MyDB ~]#
另外,hostnamectl命令是永久修改hostname,这个命令修改静态主机名,不关会设置内核参数kernel.hostname,它还会立即修改配置文件/etc/hostname,有兴趣可以自己测试一下。
[root@MyDB ~]# more /etc/hostname
yourdb
设置瞬态主机名
[root@yourdb ~]# hostnamectl set-hostname --transient "KerryDB"
注意:如果系统存在静态主机名且有效,则不会用到瞬态主机名。
设置灵活主机名
[root@yourdb etc]# hostnamectl set-hostname --pretty "kerry's db"
没有设置灵活主机名前,此文件可能不存在(如下所示)
[root@yourdb ~]# cat /etc/machine-info
cat: /etc/machine-info: No such file or directory
设置后,就能查看此文件(如下所示)
[root@yourdb etc]# cat /etc/machine-info
PRE
如果觉得我的文章对您有用,请随意打赏。你的支持将鼓励我继续创作!