在继续本教程之前,请确保以具有 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)作为系统主机名。
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 服务器主机名。