如何在ubuntu 18.04 Linux上更改主机名

获取当前主机名

使用hostnamectl命令检索当前系统主机名:

$ hostnamectl
   Static hostname: ubuntu
         Icon name: computer-vm
           Chassis: vm
        Machine ID: ceb8b579410b472899a95049f8a61e05
           Boot ID: 3f73eea15fb34a38937488149143d9d3
    Virtualization: oracle
  Operating System: Ubuntu Bionic Beaver
            Kernel: Linux 4.13.0-32-generic
      Architecture: x86-64

更改主机名

hostnamectl命令也可用于设置新的或者更新Ubuntu 18.04上的当前主机名。

$ sudo hostnamectl set-hostname onitroad

此外,检查是否存在 /etc/cloud/cloud.cfg配置。 如果文件存在,请编辑文件并更改其中的设置:

将
preserve_hostname: false
改为
preserve_hostname: true

上面的重启后将保留新主机名。

接下来,确认新主机名:

$ hostnamectl 
   Static hostname: onitroad
         Icon name: computer-vm
           Chassis: vm
        Machine ID: ceb8b579410b472899a95049f8a61e05
           Boot ID: 3f73eea15fb34a38937488149143d9d3
    Virtualization: oracle
  Operating System: Ubuntu Bionic Beaver
            Kernel: Linux 4.13.0-32-generic
      Architecture: x86-64

同时更新您的 /etc/hosts文件,以便系统可以通过新的主机名自行解析。

$ sudo nano /etc/hosts

示例:

$ cat /etc/hosts
127.0.0.1       localhost
127.0.1.1       ubuntu onitroad
# The following lines are desirable for IPv6 capable hosts
::1     localhost ip6-localhost ip6-loopback
ff02::1 ip6-allnodes
ff02::2 ip6-allrouters
日期:2020-07-07 20:55:12 来源:oir作者:oir