优雅的在 Debian 9 上更改主机名

2021-06-16 Note 2,701

先决条件

在继续本教程之前,请确保以具有 sudo 权限的用户身份登录。

显示当前主机名

要查看当前主机名,请输入以下命令:

hostnamectl
   Static hostname: SJ-FRP
         Icon name: computer-vm
           Chassis: vm
        Machine ID: 02d19577ad9146a58d2558fcf0becf6c
           Boot ID: cb58027076c0416ba3dab9775b26d2fd
    Virtualization: qemu
  Operating System: Debian GNU/Linux 9 (stretch)
            Kernel: Linux 4.9.0-13-amd64
      Architecture: x86-64

如上所示,当前主机名设置为 SJ-FRP

更改主机名

主机名是标识网络上的计算机的标签,在网络基础结构中应该是唯一的。建议使用完全限定的域名 (FQDN)作为系统主机名。

  • 在 Debian 9 中,要更改系统主机名和相关设置,可以使用 hostnamectl 命令。例如,要将系统主机名更改为 host.example.com ,可以使用以下命令:
sudo hostnamectl set-hostname host.example.com

命令 hostnamectl 不产生输出。成功时,返回 0 ,否则返回非零故障代码。

  • 编辑 /etc/hosts 文件。打开 /etc/hosts 文件并将旧主机名替换为新主机名。
127.0.0.1   localhost
127.0.0.1   host.example.com
# The following lines are desirable for IPv6 capable hosts
::1     localhost ip6-localhost ip6-loopback
ff02::1 ip6-allnodes
ff02::2 ip6-allrouters

验证更改

要验证主机名是否已成功更改,请再次使用以下 hostnamectl 命令:

   Static hostname: host.example.com
         Icon name: computer-vm
           Chassis: vm
        Machine ID: 02d19577ad9146a58d2558fcf0becf6c
           Boot ID: cb58027076c0416ba3dab9775b26d2fd
    Virtualization: qemu
  Operating System: Debian GNU/Linux 9 (stretch)
            Kernel: Linux 4.9.0-13-amd64
      Architecture: x86-64

结论

本教程我们向您展示了如何在不重新启动计算机的情况下轻松更改 Debian 服务器主机名。

2 条评论

发布评论

© iDongLei All Rights Reserved 浙ICP备2021013583号-5

Huaweicloud 强力驱动 / RunTime Day / Files / Status / About / XML