如何在 Fedora 21 中永久更改主机名

如何在 Fedora 21 中永久更改主机名

我听说在新版本的 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禁用特定的。

相关内容