如何在 Ubuntu 20.04 Focal Fossa Linux 上使用 netplan 添加静态路由
在本教程中,您将学习如何使用 netplan 在 Ubuntu 20.04 Focal Fossa Linux 上永久添加静态路由。
在本教程中您将学习:
如何添加永久静态路由
如何应用新的 netplan 配置
如何从命令行检查静态路由
如何在 Ubuntu 20.04 Focal Fossa Linux 上使用 netplan 添加静态路由
如何在 Ubuntu 20.04 上使用 netplan 添加静态路由分步说明
第一步是使用管理权限打开主 netplan 配置文件:
$ sudoedit /etc/netplan/50-cloud-init.yaml
找到与您要添加静态路由的网络接口相关的配置节。在此示例中,我们将通过接口
enp0s3192.168.1.100
添加到目标网络子网172.16.0.0/24
的静态路由代码>.示例:# 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: dhcp4: false addresses: [192.168.1.202/24] gateway4: 192.168.1.1 nameservers: addresses: [8.8.8.8,8.8.4.4,192.168.1.1] routes: - to: 172.16.0.0/24 via: 192.168.1.100 version: 2
完成所有必要的更改以添加静态路由后,使用以下命令添加所有新的 netplan 配置:
$ sudo netplan apply
检查 Ubuntu 系统上所有可用的静态路由:
$ ip route s default via 192.168.1.1 dev enp0s3 proto static 172.16.0.0/24 via 192.168.1.100 dev enp0s3 proto static 192.168.1.0/24 dev enp0s3 proto kernel scope link src 192.168.1.202