一些ubuntu-server中的网络配置栗子

免责声明

请谨慎在生产环境中参考本篇内容的配置,建议移步Netplan documentation参考 netplan 团队自己编写的文档。

请注意所有的空格和换行符,我的意思是先换个屏幕宽一点的设备,以防自动换行导致配置错误

禁用 Cloud-init 网络配置

Cloud init会在重启后初始化网络配置,每次重启就丢失配置,必须要禁用掉。

1
2
# 编辑配置文件(固定就是这个,不可更改路径与文件名)
sudo vim /etc/cloud/cloud.cfg.d/99-disable-network-config.cfg

在文件中写入

1
network: {config: disabled}

使用端口聚合

在几乎所有企业中,端口聚合是必须的,需要使用bond中的802.3ad模式以兼容大部分交换机的lacp配置。凡是注释下方的字段都需要手动更改成你所需要的。
打开文件

1
sudo vim /etc/netplan/01-qspf25g-bond.yml
1
2
3
4
5
6
7
/etc/netplan/01-qspf25g-bond.yml
| | | |
| | | |
| | | |_____自定义备注
| | |__自定义名称
| |__优先级
|____文件路径

写入以下内容

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
# ######## ! WARNING ! ########
# this file: /etc/netplan/01-qspf25g-bond.yml
# if you don’t understand this configuration below
# It is forbidden to modify any text
# DO NOT EDIT THIS FILE !
# DO NOT EDIT THIS FILE !
# DO NOT EDIT THIS FILE !
# you have been warned
# All consequences, good or bad, are your own responsibility

networks:
bonds:
bond0:
addresses:
# IP Address
- 1.10.172.192/8
interfaces:
# Network Interfaces
- ens11f0np0
- ens11f1np1
nameservers:
addresses:
# DNS Server IP
- 223.5.5.5
- 223.6.6.6
parameters:
lacp-rate: fast
mode: 802.3ad
transmit-hash-policy: layer2
routes:
# Network Gateway IP
- to: default
via: 1.10.172.254/8
ehernets:
# Networks interfaces
ens11f0np0: {}
ens11f1np1: {}
version: 2

静态IP配置

打开文件

1
sudo vim /etc/netplan/01-ethernet-static.yml

输入内容

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
# ######## ! WARNING ! ########
# this file: /etc/netplan/01-ethernet-static.yml
# if you don’t understand this configuration below
# It is forbidden to modify any text
# DO NOT EDIT THIS FILE !
# DO NOT EDIT THIS FILE !
# DO NOT EDIT THIS FILE !
# you have been warned
# All consequences, good or bad, are your own responsibility
network:
version: 2
renderer: networkd
ethernets:
ens33:
dhcp4: no
addresses:
- 172.17.20.20/17
routes:
- to: default
via: 172.17.255.254
nameservers:
addresses: [119.29.29.29,114.114.114.114,8.8.8.8]

如何应用网络配置

编辑完所有配置文件后,执行以下命令即可应用。

1
2
3
4
5
netplan try
netplan generate
netplan apply
systemctl daemon-reload
systemctl restart systemd-networkd