修改主机名

Peter Lilley, Peter Boy (pboy) Version F36 onwards Last review: 2021-01-30
新安装好的 Fedora 系统默认会设置好主机名。为了方便在网络中识别您的主机,您可能要改一下这个名字。

Fedora 系统中的主机名分成了三种形式:

  1. static静态)主机名默认在系统启动时使用。此名称通常都比较短,只包含字母、数字和连字符。

  2. 另一种形式叫 pretty装饰)主机名,像它的意思一样,名字可以更长一点,更具描述性,比如“艾米丽的第二台开发用笔记本电脑”。

  3. 还有一种叫 transient临时)主机名,由网络分配。它很有可能跟静态名称相同,除非局域网上存在多个静态名称相同的主机。例如,如果有两个主机的静态名称都叫“localhost”,其中一个的临时主机名可能会是“localhost-1”。

Displaying your current hostname

For Fedora Workstation, using the default GNOME desktop, open the Settings application and choose About.

GNOME Settings - About

To see the hostname from the command line, use the command hostnamectl with no options. The example output below shows the static and transient hostnames. Your output may be slightly different depending on which hostname types have been set.

   Static hostname: localhost.localdomain
Transient hostname: fedora
         Icon name: computer-laptop
           Chassis: laptop
        Machine ID: 15fc9e69d007013025f31bc5272c4ed1
           Boot ID: 41ac938872bae052294bcb277241ac93
  Operating System: Fedora 33 (Workstation Edition)
       CPE OS Name: cpe:/o:fedoraproject:fedora:33
            Kernel: Linux 5.10.10-200.fc33.x86_64
      Architecture: x86-64

To see the current static, transient or pretty hostname, you can use the hostnamectl command with options, such as:

hostnamectl --static
hostnamectl --transient
hostnamectl --pretty

Changing the hostname

For Fedora Workstation, using the default GNOME desktop, open the Settings application and choose About.

GNOME Settings - About

You can replace the value in the Device name field with the name of your choosing. The effect of this field is as follows:

  • If you use a name that is shorter, contains only lowercase letters, numbers and/or dashes ("-"), this will set the host’s static name, and the pretty name will be left blank.

  • If you enter a name that is more descriptive, contains mixed-case and other types of characters, this will set the pretty name, and a static name will be derived from that automatically.

You can see the effect of the change by using the hostnamectl command again:

   Static hostname: emilys-2nd-dev-laptop
   Pretty hostname: Emily's 2nd dev laptop
         Icon name: computer-laptop
           Chassis: laptop
        Machine ID: 15fc9e69d007013025f31bc5272c4ed1
           Boot ID: 41ac938872bae052294bcb277241ac93
  Operating System: Fedora 33 (Workstation Edition)
       CPE OS Name: cpe:/o:fedoraproject:fedora:33
            Kernel: Linux 5.10.10-200.fc33.x86_64
      Architecture: x86-64

In the previous example, "Emily’s 2nd dev laptop" was entered via the Settings app, and the static hostname "emilys-2nd-dev-laptop" was set automatically.

Hostnames can also be set at the command line with the hostnamectl set-hostname command. For example:

sudo hostnamectl set-hostname --pretty "Emily's 2nd dev laptop"
sudo hostnamectl set-hostname --static emily-dev-2