我聽說在新版本的 fedora 中更改主機名稱是透過hostnamectl
命令完成的。此外,我最近(並成功)使用此方法更改了 Arch Linux 上的主機名稱。但是,運行時:
[root@localhost ~]# hostnamectl set-hostname --static paragon.localdomain
[root@localhost ~]# hostnamectl set-hostname --transient paragon.localdomain
[root@localhost ~]# hostnamectl set-hostname --pretty paragon.localdomain
重新啟動後,這些變更不會保留(與許多人的說法相反)。怎麼了?
- 我真的不想
/etc/hostname
手動編輯。
我還應該指出,這是一個完全地庫存軟呢帽。我什至還沒有開始安裝我的核心應用程式。
答案1
設定主機名的指令肯定是hostnamectl
.
root ~ # hostnamectl set-hostname --static "YOUR-HOSTNAME-HERE"
這是一個額外的來源,進一步描述了此功能,標題為:正確設定主機名稱 - Amazon EC2 上的 Fedora 20。
另外還有以下手冊頁hostnamectl
:
HOSTNAMECTL(1) hostnamectl HOSTNAMECTL(1)
NAME
hostnamectl - Control the system hostname
SYNOPSIS
hostnamectl [OPTIONS...] {COMMAND}
DESCRIPTION
hostnamectl may be used to query and change the system hostname and
related settings.
This tool distinguishes three different hostnames: the high-level
"pretty" hostname which might include all kinds of special characters
(e.g. "Lennart's Laptop"), the static hostname which is used to
initialize the kernel hostname at boot (e.g. "lennarts-laptop"), and
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.
Note that the pretty hostname has little restrictions on the characters
used, while the static and transient hostnames are limited to the
usually accepted characters of Internet domain names.
The static hostname is stored in /etc/hostname, see hostname(5) for
more information. The pretty hostname, chassis type, and icon name are
stored in /etc/machine-info, see machine-info(5).
Use systemd-firstboot(1) to initialize the system host name for mounted
(but not booted) system images.
Fedora 21 中有一個錯誤,SELinux 阻止了 hostnamectl 訪問,可以在此處找到,標題為:Bug 1133368 - SELinux 阻止 systemd-hostnam 對檔案主機名稱進行「取消連結」訪問。
這個錯誤似乎是相關的。/etc/hostname
安裝時存在 SELinux 上下文未正確應用於檔案的問題。這表現為該工具hostnamectl
無法操作該檔案/etc/hostname
。同一個執行緒提供了這個解決方法:
$sudo restorecon -v /etc/hostname
筆記:該補丁已應用於 Anaconda(安裝工具),因此這個問題將來對於新用戶來說應該會消失。
答案2
造成這種情況的可能原因是您的 dhcp 用戶端。如果您使用的是常見的 dhcpcd,解決方案是將以下行新增至/etc/dhcpcd.conf
:
nohook hostname
這可以防止 dhcpcd 呼叫更改您的系統主機名稱的腳本。
預設情況下,dhcpcd 運行所有存在的掛鉤/lib/dhcpcd/dhcpcd-run-hooks
(系統上的路徑可能有所不同)。使用nohook
禁用特定的。