正確設定主機名稱 - Amazon EC2 上的 Fedora 20

正確設定主機名稱 - Amazon EC2 上的 Fedora 20

情境

我有一個Fedora 20 雲鏡像在 Amazon EC2 上執行(以下稱為「執行個體」)。我對持續設定其主機名稱有一些不確定性。

目標

在本例中,假設我想將實例的主機名稱設定為penpen.homelinux.org。 (這個名稱也將使用 註冊在 DynDNS 上ddclient,但這是我們在這裡不感興趣的另一個方面。)

當然,可以在啟動完成後手動設定主機名稱(使用hostnamectl其他)。但我們希望在第一次登入之前設定正確的主機名稱。

傳統上,要持久配置主機名,需要修改/etc/hostname.很遺憾這裡不起作用。

預設系統行為

預設情況下,執行個體將其主機名稱設定為內部 EC2 名稱。啟動後,我們可以查看產生主機名稱的所有不同的小地方,我們發現:

Kernel hostname via 'sysctl'                      : ip-10-164-65-105.ec2.internal
Kernel domainname via 'sysctl'                    : (none)
File '/etc/hostname'                              : contains 'ip-10-164-65-105.ec2.internal'
File '/etc/sysconfig/network'                     : exists but has no 'HOSTNAME' line
According to the shell                            : HOSTNAME = ip-10-164-65-105.ec2.internal
Nodename given by 'uname --nodename'              : ip-10-164-65-105.ec2.internal
Hostname ('hostname')                             : ip-10-164-65-105.ec2.internal
Short hostname ('hostname --short')               : ip-10-164-65-105
NIS domain name ('domainname')                    : (none)
YP default domain ('hostname --yp')               : [hostname --yp failed]
DNS domain name ('hostname --domain')             : ec2.internal
Fully qualified hostname ('hostname --fqdn')      : ip-10-164-65-105.ec2.internal
Hostname alias ('hostname --alias')               : 
By IP address ('hostname --ip-address')           : 10.164.65.105
All IPs ('hostname --all-ip-addresses')           : 10.164.65.105 
All FQHNs via IPs ('hostname --all-ip-addresses') : ip-10-164-65-105.ec2.internal 
Static hostname via 'hostnamectl'                 : ip-10-164-65-105.ec2.internal
Transient hostname via 'hostnamectl'              : ip-10-164-65-105.ec2.internal
Pretty hostname via 'hostnamectl'                 : 

那麼讓我們試著寫入 /etc/hostname ...

如果將所需的主機名稱寫入/etc/hostname,則此變更將在下次引導時再次遺失。讓我們檢查一下由 執行的引導過程systemd

運行範例

寫入,rorororoor.homelinux.org然後/etc/hostname重新啟動。

使用紀錄我們發現(請注意,日誌行並不完全按時間排序):

啟動程序以主機名稱開始本機然後切換 root,此時主機名稱變為rorororoor.homelinux.org

Dec 26 15:12:08 localhost systemd[1]: Starting Cleanup udevd DB...
Dec 26 15:12:08 localhost systemd[1]: Started Cleanup udevd DB.
Dec 26 15:12:08 localhost systemd[1]: Starting Switch Root.
Dec 26 15:12:08 localhost systemd[1]: Reached target Switch Root.
Dec 26 15:12:08 localhost systemd[1]: Starting Switch Root...
Dec 26 15:12:08 localhost systemd[1]: Switching root.
Dec 26 15:12:08 localhost systemd-journal[67]: Journal stopped
Dec 26 15:12:12 rorororoor.homelinux.org systemd-journal[155]: Runtime journal is using 8.0M
Dec 26 15:12:12 rorororoor.homelinux.org systemd-journal[155]: Runtime journal is using 8.0M
Dec 26 15:12:12 rorororoor.homelinux.org systemd-journald[67]: Received SIGTERM
...........
Dec 26 15:12:12 rorororoor.homelinux.org kernel: SELinux: initialized
Dec 26 15:12:12 rorororoor.homelinux.org systemd-journal[155]: Journal started
Dec 26 15:12:08 rorororoor.homelinux.org systemd-cgroups-agent[128]: Failed to get D-Bus connection: Failed to connect to socket /run/systemd/private: No such file or directory
Dec 26 15:12:10 rorororoor.homelinux.org systemd[1]: systemd 208 running in system mode.
Dec 26 15:12:10 rorororoor.homelinux.org systemd[1]: Detected virtualization 'xen'.
Dec 26 15:12:10 rorororoor.homelinux.org systemd[1]: Set hostname to <rorororoor.homelinux.org>.
Dec 26 15:12:10 rorororoor.homelinux.org systemd[1]: Failed to open private bus connection: Failed to connect to socket /var/run/dbus/system_bus_socket: No such file or directory
Dec 26 15:12:11 rorororoor.homelinux.org systemd[1]: Mounted Debug File System.

我們看到將systemd主機名稱設定為rorororoor.homelinux.org,隨著日誌的主機列發生變化,顯然成功了。發出一些錯誤,可能是因為hostnamectl此時無法聯絡 DBus。

我不確定是誰在這裡命名的; systemd 的一些內部部分?不管怎樣,繼續查看日誌,我們發現主機名稱很快就被設定回 EC2 內部名稱:

Dec 26 15:12:33 rorororoor.homelinux.org cloud-init[485]: [CLOUDINIT] util.py[DEBUG]: Running command ('resize2fs', '/dev/xvda1') with allowed return codes [0] (shell=False, capture=True)
Dec 26 15:12:33 rorororoor.homelinux.org cloud-init[485]: [CLOUDINIT] cc_resizefs.py[DEBUG]: Resizing took 0.067 seconds
Dec 26 15:12:33 rorororoor.homelinux.org cloud-init[485]: [CLOUDINIT] cc_resizefs.py[DEBUG]: Resized root filesystem (type=ext4, val=True)
Dec 26 15:12:33 rorororoor.homelinux.org cloud-init[485]: [CLOUDINIT] helpers.py[DEBUG]: config-set_hostname already ran (freq=once-per-instance)
Dec 26 15:12:33 rorororoor.homelinux.org cloud-init[485]: [CLOUDINIT] helpers.py[DEBUG]: Running config-update_hostname using lock (<cloudinit.helpers.DummyLock object at 0x2559210>)
Dec 26 15:12:33 rorororoor.homelinux.org cloud-init[485]: [CLOUDINIT] cc_update_hostname.py[DEBUG]: Updating hostname to ip-10-164-65-105.ec2.internal (ip-10-164-65-105)
Dec 26 15:12:33 rorororoor.homelinux.org cloud-init[485]: [CLOUDINIT] util.py[DEBUG]: Running command ['hostname'] with allowed return codes [0] (shell=False, capture=True)
Dec 26 15:12:33 rorororoor.homelinux.org cloud-init[485]: [CLOUDINIT] __init__.py[DEBUG]: Attempting to update hostname to ip-10-164-65-105.ec2.internal in 1 files
Dec 26 15:12:33 rorororoor.homelinux.org cloud-init[485]: [CLOUDINIT] util.py[DEBUG]: Running command ['hostnamectl', 'set-hostname', 'ip-10-164-65-105.ec2.internal'] with allowed return codes [0] (shell=False, capture=True)
Dec 26 15:12:33 rorororoor.homelinux.org dbus-daemon[226]: dbus[226]: [system] Activating via systemd: service name='org.freedesktop.hostname1' unit='dbus-org.freedesktop.hostname1.service'
Dec 26 15:12:33 rorororoor.homelinux.org dbus[226]: [system] Activating via systemd: service name='org.freedesktop.hostname1' unit='dbus-org.freedesktop.hostname1.service'
Dec 26 15:12:34 rorororoor.homelinux.org systemd[1]: Starting Hostname Service...
Dec 26 15:12:34 rorororoor.homelinux.org dbus-daemon[226]: dbus[226]: [system] Successfully activated service 'org.freedesktop.hostname1'
Dec 26 15:12:34 rorororoor.homelinux.org dbus[226]: [system] Successfully activated service 'org.freedesktop.hostname1'
Dec 26 15:12:34 rorororoor.homelinux.org systemd[1]: Started Hostname Service.
Dec 26 15:12:34 rorororoor.homelinux.org systemd-hostnamed[598]: Changed static host name to 'ip-10-164-65-105.ec2.internal'
Dec 26 15:12:34 ip-10-164-65-105.ec2.internal systemd-hostnamed[598]: Changed host name to 'ip-10-164-65-105.ec2.internal'
Dec 26 15:12:34 ip-10-164-65-105.ec2.internal systemd[1]: Started Initial cloud-init job (metadata service crawler).
Dec 26 15:12:34 ip-10-164-65-105.ec2.internal systemd[1]: Starting Cloud-config availability.
Dec 26 15:12:34 ip-10-164-65-105.ec2.internal systemd[1]: Reached target Cloud-config availability.
Dec 26 15:12:34 ip-10-164-65-105.ec2.internal systemd[1]: Starting Apply the settings specified in cloud-config...
Dec 26 15:12:34 ip-10-164-65-105.ec2.internal [485]: [CLOUDINIT] helpers.py[DEBUG]: Running config-update_etc_hosts using lock (<cloudinit.helpers.DummyLock object at 0x2559350>)
Dec 26 15:12:34 ip-10-164-65-105.ec2.internal [485]: [CLOUDINIT] cc_update_etc_hosts.py[DEBUG]: Configuration option 'manage_etc_hosts' is not set, not managing /etc/hosts in module update_etc_hosts
Dec 26 15:12:34 ip-10-164-65-105.ec2.internal [485]: [CLOUDINIT] helpers.py[DEBUG]: config-rsyslog already ran (freq=once-per-instance)
Dec 26 15:12:34 ip-10-164-65-105.ec2.internal [485]: [CLOUDINIT] helpers.py[DEBUG]: config-users-groups already ran (freq=once-per-instance)

這裡的主機名稱設定是透過「systemd-hostnamed」單元完成的。 “systemd-hostnamed”的“單元檔案”是/usr/lib/systemd/system/systemd-hostnamed.service並包含:

[Unit]
Description=Hostname Service
Documentation=man:systemd-hostnamed.service(8) man:hostname(5) man:machine-info(5)
Documentation=http://www.freedesktop.org/wiki/Software/systemd/hostnamed

[Service]
ExecStart=/usr/lib/systemd/systemd-hostnamed
BusName=org.freedesktop.hostname1
CapabilityBoundingSet=CAP_SYS_ADMIN CAP_DAC_OVERRIDE CAP_SYS_PTRACE

上面調用的程式/usr/lib/systemd/systemd-hostnamed實際上是一個二進位(為什麼!)。然而原始碼可以被找尋。

重點是我們回到了ip-10-164-65-105.ec2.internal

做什麼?

答案1

你需要做兩件事(但只做了其中一件):

  1. 在 中設定主機名稱/etc/hostname
  2. 編輯/etc/cloud/cloud.cfg並設定preserve_hostnameTrue. (您也可以將此選項與您的用戶資料一起傳遞。)

需要第二步是因為 Fedora 用於cloud-init從 EC2 環境引入使用者資料來配置實例,並且cloud-init需要告知主機名稱應該保留。

答案2

另一個選擇是透過設定主機名使用者資料

例如

#cloud-config
hostname: foo
fqdn: foo.bar.net

這將在啟動時設定主機名,但是我不確定這是否總是在第一次登入之前發生。

答案3

看起來答案就在 hostnamectl 手冊頁中,現在有 3 個主機名,靜態主機名稱、瞬態主機名稱和漂亮主機名稱。

要設定我認為是您想要的靜態主機名,

hostnamectl --static set-hostname somehost.tld

您可以將它們全部設定為相同

hostnamectl set-hostname somehost.tld

答案4

這其實是使用 SystemD 的類似 RHEL 發行版上的 cloud-init 中的錯誤。有一個補丁可以在https://bugs.launchpad.net/cloud-init/+bug/1424710

相關內容