之前寫過一篇博客“深入理解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 hostname is the most important one, and it’s stored in the /etc/hostname file. This hostname is used among machines to identify a particular server.
- The pretty hostname got its name because it allows for more characters and punctuation. It’s more user-friendly, but since it uses non-standard characters, it is not permitted for machine code. The pretty hostname is stored in the /etc/machine-info directory.
- The transient hostname is one maintained in the Linux kernel. It is dynamic, meaning it will be lost after a reboot. This approach might be useful if you have a minor job requiring a temporary hostname, but you don’t want to risk making a permanent change that might be confusing.
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 ~]# hostnameMyDB
[root@MyDB ~]# cat /etc/hostnameMyDB
[root@MyDB ~]# hostnamectl statusStatic 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 dbIcon 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/hostnamemydb
[root@MyDB ~]# hostnamectl set-hostname kerrydb[root@MyDB ~]# cat /proc/sys/kernel/hostnamekerrydb
[root@MyDB ~]# hostnamectl set-hostname --static yourdb[root@MyDB ~]# cat /proc/sys/kernel/hostnameyourdb
[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
PRETTY_HOSTNAME="kerry's db"
一些問題測驗
問題:如果同時設定了/etc/hosts和/etc/hostname,那么服務器重啟時,它會讀取哪個檔案? 我們設定一下這兩個后(如下所示),然后重啟服務器
[root@MyDB ~]# more /etc/hosts127.0.0.1 localhost localhost.localdomain localhost4 localhost4.localdomain4
::1 localhost localhost.localdomain localhost6 localhost6.localdomain6
10.20.57.24 MyDB MyDB.localdomain
[root@MyDB ~]# more /etc/hostnameYourDB
重啟過后,你會發現靜態主機名為YourDB,也就是說重啟時讀取/etc/hostname這個組態檔來初始化內核主機名,那么我們將/etc/hostname中的靜態主機名置空,然后重啟服務器,
[root@YourDB ~]# cat /dev/null > /etc/hostname[root@YourDB ~]# more /etc/hostname[root@YourDB ~]# reboot
測驗驗證發現,靜態主機名變為了n/a,但是顯示的主機名為MyDB,那么是否讀取了/etc/hosts中的配置呢?我們查看日志,發現下面一些資訊,
[root@MyDB ~]# hostnameMyDB
[root@MyDB ~]# hostnamectlStatic hostname: n/a
Pretty hostname: kerry's db
Transient hostname: MyDB
Icon name: computer-desktop
Chassis: desktop
Machine ID: 955dde0d8f7341ebb19a1e247577c410
Boot ID: dfdaa6d51f3942b18d2de98dea2b8906
Operating System: CentOS Linux 7 (Core)
CPE OS Name: cpe:/o:centos:centos:7
Kernel: Linux
我們發現系統啟動的時候,在NetworkManager中重新設定hostname的值,具體如下所示:
[root@MyDB ~]# journalctl -xb | grep hostname
Nov 04 16:03:03 localhost.localdomain systemd[1]: Set hostname to <localhost.localdomain>.
Nov 04 16:03:15 localhost.localdomain dbus[716]: [system] Activating via systemd: service name='org.freedesktop.hostname1' unit='dbus-org.freedesktop.hostname1.service'
-- Subject: Unit systemd-hostnamed.service has begun start-up
-- Unit systemd-hostnamed.service has begun starting up.
Nov 04 16:03:15 localhost.localdomain dbus[716]: [system] Successfully activated service 'org.freedesktop.hostname1'
-- Subject: Unit systemd-hostnamed.service has finished start-up
-- Unit systemd-hostnamed.service has finished starting up.
Nov 04 16:03:15 localhost.localdomain NetworkManager[743]: <info> [1572854595.8531] hostname: hostname: using hostnamed
Nov 04 16:03:15 localhost.localdomain nm-dispatcher[856]: req:1 'hostname': new request (2 scripts)
Nov 04 16:03:15 localhost.localdomain nm-dispatcher[856]: req:1 'hostname': start running ordered scripts...
Nov 04 16:03:20 localhost.localdomain NetworkManager[743]: <info> [1572854600.1230] policy: set-hostname: set hostname to 'MyDB' (from address lookup)
Nov 04 16:03:20 MyDB systemd-hostnamed[836]: Changed host name to 'MyDB'
Nov 04 16:03:20 MyDB nm-dispatcher[856]: req:4 'hostname': new request (2 scripts)
Nov 04 16:03:20 MyDB nm-dispatcher[856]: req:4 'hostname': start running ordered scripts...

然后我們從NetworkManager/src/nm-policy.c找到如下代碼,簡單擼了一下代碼,雖然還沒有找到直接讀取/etc/hosts中hostname的直接證據(C語言已經遺忘的七七八八了,也不想花太多時間深入!),但是實驗測驗,當/etc/hostname為空時,確實會讀取/etc/hosts下面的主機名資訊,另外,有點可以確認的是,當/etc/hostname為空時,代碼里面首先會將hostname設定為“localhost.localdomain”, 然后,呼叫_set_hostnam重新設定,
#define FALLBACK_HOSTNAME4 "localhost.localdomain"static void
_set_hostname (NMPolicy *self,
const char *new_hostname,
const char *msg)
{NMPolicyPrivate *priv = NM_POLICY_GET_PRIVATE (self);
gs_free char *old_hostname = NULL;const char *name;
/* The incoming hostname *can* be NULL, which will get translated to * 'localhost.localdomain' or such in the hostname policy code, but we * keep cur_hostname = NULL in the case because we need to know that * there was no valid hostname to start with. */ /* Clear lookup addresses if we have a hostname, so that we don't * restart the reverse lookup thread later. */ if (new_hostname)g_clear_object (&priv->lookup.addr);
/* Update the DNS only if the hostname is actually * going to change. */ if (!nm_streq0 (priv->cur_hostname, new_hostname)) {g_free (priv->cur_hostname);
priv->cur_hostname = g_strdup (new_hostname);
/* Notify the DNS manager of the hostname change so that the domain part, if * present, can be added to the search list. */nm_dns_manager_set_hostname (priv->dns_manager, priv->cur_hostname,
all_devices_not_active (self));
}
/* Finally, set kernel hostname */ if (!new_hostname)name = FALLBACK_HOSTNAME4;
else if (!new_hostname[0]) {
g_warn_if_reached ();
name = FALLBACK_HOSTNAME4;
} elsename = new_hostname;
/* Don't set the hostname if it isn't actually changing */ if ( (old_hostname = _get_hostname (self)) && (nm_streq (name, old_hostname))) { _LOGT (LOGD_DNS, "set-hostname: hostname already set to '%s' (%s)", name, msg); return;}
/* Keep track of the last set hostname */g_free (priv->last_hostname);
priv->last_hostname = g_strdup (name);
priv->changing_hostname = TRUE; _LOGI (LOGD_DNS, "set-hostname: set hostname to '%s' (%s)", name, msg); /* Ask NMSettings to update the transient hostname using its * systemd-hostnamed proxy */nm_hostname_manager_set_transient_hostname (priv->hostname_manager,
name,
settings_set_hostname_cb,
g_object_ref (self));
}


參考資料:
https://github.com/NetworkManager/NetworkManager/blob/master/src/nm-policy.c
轉載請註明出處,本文鏈接:https://www.uj5u.com/caozuo/157892.html
標籤:Linux
