如何在Ubuntu 18.10 Linux上配置静态IP地址

要在Ubuntu 18.10服务器上配置静态IP地址,我们需要修改 /etc/netplan/目录中的相关网络网络配置文件。

例如,您可能会发现一个名为'50 cloud init.yaml'的默认网络计划配置文件,其中包含使用'networkd'命令通过DHCP配置网络接口的以下内容:

# This file is generated from information provided by
# the datasource.  Changes to it will not persist across an instance.
# To disable cloud-init's network configuration capabilities, write a file
# /etc/cloud/cloud.cfg.d/99-disable-network-config.cfg with the following:
# network: {config: disabled}
network:
    ethernets:
        enp0s3:
            addresses: []
            dhcp4: true
    version: 2

设置静态IP地址:

# This file is generated from information provided by
# the datasource.  Changes to it will not persist across an instance.
# To disable cloud-init's network configuration capabilities, write a file
# /etc/cloud/cloud.cfg.d/99-disable-network-config.cfg with the following:
# network: {config: disabled}
network:
  version: 2
  renderer: networkd
  ethernets:
    enp0s3:
     dhcp4: no
     addresses: [192.168.1.233/24]
     gateway4: 192.168.1.1
     nameservers:
       addresses: [8.8.8.8,8.8.4.4]

注意,YAML文件的缩进很重要。如果格式错误,将会报类似下面的错误

Invalid YAML at //etc/netplan/01-netcfg.yaml line 7 column 6: did not find expected key

应用修改:

$ sudo netplan apply

如果有问题,可以使用调试模式:

$ sudo netplan --debug apply
日期:2020-07-07 20:55:15 来源:oir作者:oir