如何在Ubuntu 17.10服务器上配置静态IP地址

Netplan是Ubuntu 17.10中引入的新网络配置工具,以管理网络设置。

它可以通过编写简单的yaml描述所需的网络接口与它们应该配置的操作进行使用;它将为所选渲染器工具生成所需的配置。

此新工具替换先前已用于配置Ubuntu网络接口的静态接口(/etc/network/interfaces)文件。
现在,我们必须使用/etc/netplan/*.yaml配置Ubuntu接口。

新的接口配置文件现在位于/etc/netplan目录中。有两个渲染器:NetworkManager和networkd。
NetworkManager渲染器主要用于桌面计算机和服务器上的networkd。
如果希望NetworkManager控制网络接口,请使用NetworkManager作为渲染器,否则请使用networkd

当我们使用NetworkManager作为渲染器时,我们将使用NetworkManager GUI来管理接口。

下面是使用NetworkD作为使用DHCP的渲染器的网络接口的示例文件。

NetworkD使用命令行配置网络接口。

# 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

要保存更改,请运行以下命令。

sudo netplan apply

使用NetworkD(服务器)配置静态IP地址

要在Ubuntu服务器上使用新的Netplan工具配置静态IP地址,该文件看起来像下面这样:

例如,我们可能在/etc/netplan目录中找到一个名为50-cloud-init.yaml的默认Netplan配置文件,
使用NetworkD Deamon 通过DHCP配置网络接口:

sudo nano /etc/netplan/50-cloud-init.yaml

配置示例:

# 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地址,参考如下

sudo nano /etc/netplan/50-cloud-init.yaml

配置IPV4地址:

# 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: [192.168.1.2/24]
            gateway4: 192.168.1.1
            nameservers:
              addresses: [8.8.8.8,8.8.4.4]
            dhcp4: no
    version: 2

退出并通过运行下面的命令来保存更改:

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