一、Netplan 配置流程
1、查看配置文件
ls /etc/netplan/
就可以看到配置文件名称。
2、打开配置文件
vim /etc/netplan/*.yaml
3、修改配置文件,这个在下面有详细说明
4、测试配置文件
sudo netplan try
如果没问题,可以继续往下应用。
5、应用配置文件
sudo netplan apply
6、重启网络服务
sudo systemctl restart system-networkd
如果是桌面版:
sudo systemctl restart network-manager
7、验证 IP 地址
ip a
至此,整个流程完成。更多参考:https://vitux.com/how-to-configure-networking-with-netplan-on-ubuntu/
二、Netplan 配置文件详解
1、使用 DHCP:
network:
version: 2
renderer: networkd
ethernets:
enp3s0:
dhcp4: true
2、使用静态 IP:
network:
version: 2
renderer: networkd
ethernets:
enp3s0:
addresses:
- 10.10.10.2/24
gateway4: 10.10.10.1
nameservers:
search: [mydomain, otherdomain]
addresses: [10.10.10.1, 1.1.1.1]
3、多个网口 DHCP:
network:
version: 2
ethernets:
enred:
dhcp4: yes
dhcp4-overrides:
route-metric: 100
engreen:
dhcp4: yes
dhcp4-overrides:
route-metric: 200
4、连接开放的 WiFi(无密码):
network:
version: 2
wifis:
wl0:
access-points:
opennetwork: {}
dhcp4: yes
5、连接 WPA 加密的 WiFi:
network:
version: 2
renderer: networkd
wifis:
wlp2s0b1:
dhcp4: no
dhcp6: no
addresses: [192.168.0.21/24]
gateway4: 192.168.0.1
nameservers:
addresses: [192.168.0.1, 8.8.8.8]
access-points:
"network_ssid_name":
password: "**********"
6、在单网卡上使用多个 IP 地址(同一网段):
network:
version: 2
renderer: networkd
ethernets:
enp3s0:
addresses:
- 10.100.1.38/24
- 10.100.1.39/24
gateway4: 10.100.1.1
7、在单网卡使用多个不同网段的 IP 地址:
network:
version: 2
renderer: networkd
ethernets:
enp3s0:
addresses:
- 9.0.0.9/24
- 10.0.0.10/24
- 11.0.0.11/24
#gateway4: # unset, since we configure routes below
routes:
- to: 0.0.0.0/0
via: 9.0.0.1
metric: 100
- to: 0.0.0.0/0
via: 10.0.0.1
metric: 100
- to: 0.0.0.0/0
via: 11.0.0.1
metric: 100
以上是最常见的一些配置,相信参考一下大家都能看懂,如果看不懂,建议也别折腾,免得把网络搞崩溃,寻求专业人士的帮助更为靠谱。
更多配置,参考:https://netplan.io/examples