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

Pyt*_*Nut 35 fedora hostname

我听说在新版本的 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
Run Code Online (Sandbox Code Playgroud)

重新启动后不会保留更改(与许多人声称的相反)。怎么了?

  • 我真的不想/etc/hostname手动编辑。

我还应该注意,这是一个完全库存的软呢帽。我什至还没有开始安装我的核心应用程序。

som*_*ing 39

设置主机名的命令肯定是,hostnamectl。

root ~ # hostnamectl set-hostname --static "YOUR-HOSTNAME-HERE"
Run Code Online (Sandbox Code Playgroud)

这是一个额外的来源,该来源更详细地描述了此功能,标题为:正确设置主机名 - 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.
Run Code Online (Sandbox Code Playgroud)

Fedora 21 中存在一个错误,其中 SELinux 阻止主机名访问,可在此处找到,标题为:错误 1133368 - SELinux 阻止 systemd-hostnam 对文件 hostname 进行“取消链接”访问。

这个错误似乎是相关的。/etc/hostname安装时SELinux 上下文未正确应用于文件存在问题。这表明该工具hostnamectl无法操作文件/etc/hostname。同一个线程提供了这个解决方法:

$sudo restorecon -v /etc/hostname
Run Code Online (Sandbox Code Playgroud)

注意:该补丁已应用于 Anaconda(安装工具),因此该问题在未来对于新用户应该会消失。