# Debian网络配置
修改网络配置文件:/etc/network/interfaces
配置为DHCP:
auto enp0s3
allow-hotplug enp0s3
iface enp0s3 inet dhcp
2
3
配置为静态IP:
auto enp0s9
allow-hotplug enp0s9
iface enp0s9 inet static
address 192.168.1.10
netmask 255.255.255.0
gateway 192.168.1.1
2
3
4
5
6
注意点:
- auto 是在系统启动的时候启动网络接口,无论网络接口是否插入网线,都会去检查,比如配置了DHCP,则无论是否有网线,系统都会去执行DHCP。
- allow-hotplug 只有当内核从该接口检测到热插拔事件后才启动该接口,如果系统开机没有插入网线,则系统不会启动该接口,系统启动后,插入网线则会自动启动该接口。
# 常用命令
# 启动接口
ifup <interface_name>
# 关掉接口
ifdown <interface_name>
# 网络重启
/etc/init.d/networking restart
service networking restart
2
3
4
5
6
7
# Centos网络配置
Centos的网络配置路径为:/etc/sysconfig/network-scripts,注意默认情况下centos的网络并不是默认打开的,如果需要打开则需要把对应的ONBOOT改为yes。
如果要配置静态ip
ONBOOT=yes
BOOTPROTO=static
IPADDR="10.0.100.195"
NETMASK="255.255.255.0"
GATEWAY="10.0.100.1"
DNS1="114.114.114.114"
2
3
4
5
6
如果出现ping能够出现地址但是ping不通的情况,需要关闭NetworkManager:
systemctl stop NetworkManager
systemctl disable NetworkManager
2
# Ubuntu网络配置
参考文档:
- Netplan配置示例 (opens new window)
- 负载均衡 (opens new window)
- Netplan网络配置工具的学习 (opens new window)
- Ubuntu网络配置 (opens new window)
Ubuntu的网络配置文件为:/etc/netplan/00-installer-config.yaml
network:
version: 2
renderer: networkd
ethernets:
enp3s0:
addresses:
- 10.10.10.2/24
gateway4: 10.10.10.1
optional: true
nameservers:
search: [mydomain, otherdomain]
addresses: [10.10.10.1, 1.1.1.1]
2
3
4
5
6
7
8
9
10
11
12
注意:
- optional标识引导不需要可选设备。正常情况下,networkd 将等待一段时间来配置设备,然后再继续启动。但是,如果设备被标记为可选,网络将不会等待它。这只有 networkd 支持,默认值为 false。
# 开机等待问题
Ubuntu开机会在网络连接检查那等待好久,可以设置时间:
cd /etc/systemd/system/network-online.target.wants/
vi systemd-networkd-wait-online.service
在[Service]栏最后加入如下命令
TimeoutStartSec=2sec
2
3
4
5
# DHCP连接多接口
许多系统都包含多个网络接口。服务器通常将需要连接到多个网络,并且尽管所有服务器都提供有效的网关,但它们可能仍要求到Internet的流量通过特定的接口。通过为通过DHCP检索的路由指定度量标准,可以实现DHCP所需的精确路由,这将确保某些路由优先于其他路由。在此示例中,“enred”优于“engreen”,因为它具有较低的路由度量:
network:
version: 2
ethernets:
enred:
dhcp4: yes
dhcp4-overrides:
route-metric: 100
engreen:
dhcp4: yes
dhcp4-overrides:
route-metric: 200
2
3
4
5
6
7
8
9
10
11
# 连接开放的无线网络
network:
version: 2
wifis:
wl0:
access-points:
opennetwork: {}
dhcp4: yes
2
3
4
5
6
7
# 连接到WPA个人无线网络
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: "**********"
2
3
4
5
6
7
8
9
10
11
12
13
14
# 使用WPA-EAP连接
network:
version: 2
wifis:
wl0:
access-points:
workplace:
auth:
key-management: eap
method: ttls
anonymous-identity: "@internal.example.com"
identity: "joe@internal.example.com"
password: "v3ryS3kr1t"
dhcp4: yes
2
3
4
5
6
7
8
9
10
11
12
13
# 使用TLS连接
network:
version: 2
wifis:
wl0:
access-points:
university:
auth:
key-management: eap
method: tls
anonymous-identity: "@cust.example.com"
identity: "cert-joe@cust.example.com"
ca-certificate: /etc/ssl/cust-cacrt.pem
client-certificate: /etc/ssl/cust-crt.pem
client-key: /etc/ssl/cust-key.pem
client-key-password: "d3cryptPr1v4t3K3y"
dhcp4: yes
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
# 单接口使用多个地址
network:
version: 2
renderer: networkd
ethernets:
enp3s0:
addresses:
- 10.100.1.38/24
- 10.100.1.39/24
gateway4: 10.100.1.1
2
3
4
5
6
7
8
9
# 通过多个网关使用多个地址
鉴于有多个地址,每个地址都有自己的网关,我们在此不指定gateway4,而是使用子网的网关地址将单个路由配置为0.0.0.0/0(任何地方)。该metric值应进行调整,以使路由按预期进行。DHCP可用于接收接口的IP地址之一。在这种情况下,该地址的默认路由将自动配置metric为100。作为这些条目的简写形式routes,gateway4可以设置为其中一个子网的网关地址。在这种情况下,可以从中省略该子网的路由routes。它metric会被设置为100。
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
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
# 主备绑定
network:
version: 2
renderer: networkd
bonds:
bond0:
dhcp4: yes
interfaces:
- enp3s0
- enp4s0
parameters:
mode: active-backup
primary: enp3s0
2
3
4
5
6
7
8
9
10
11
12
# 网桥
参考文档:
在物理世界中,如果你有一台多网卡的主机,那么你就可以让这台主机替代交换机的功能,你可以在这台多网卡主机上创建一个Linux网桥,把部分或者全部所有网卡都加入到网桥里。再把其它主机连接到这台多网卡的主机上,那就形成了一个标准的局域网络。
# Let NetworkManager manage all devices on this system
network:
version: 2
renderer: NetworkManager
meihualing@xx-HP-PC:~$ cat /etc/netplan/br0.yaml
network:
version: 2
renderer: NetworkManager
ethernets:
eno1:
dhcp4: no
dhcp6: no
enp2s0:
dhcp4: no
dhcp6: no
enp3s0:
dhcp4: no
dhcp6: no
enp4s0:
dhcp4: no
dhcp6: no
bridges:
br0:
interfaces:
- enp2s0
- enp3s0
- enp4s0
- eno1
dhcp4: yes
dhcp6: yes
#addresses:
# - 192.168.3.166/24
#routes:
# - to: default
# via: 192.168.3.1
#nameservers:
# addresses: [192.168.3.1, 8.8.8.8]
parameters:
stp: false
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
39
# Vlan连接网络
network:
version: 2
renderer: networkd
ethernets:
mainif:
match:
macaddress: "de:ad:be:ef:ca:fe"
set-name: mainif
addresses: [ "10.3.0.5/23" ]
gateway4: 10.3.0.1
nameservers:
addresses: [ "8.8.8.8", "8.8.4.4" ]
search: [ example.com ]
vlans:
vlan15:
id: 15
link: mainif
addresses: [ "10.3.99.5/24" ]
vlan10:
id: 10
link: mainif
addresses: [ "10.3.98.5/24" ]
nameservers:
addresses: [ "127.0.0.1" ]
search: [ domain1.example.com, domain2.example.com ]
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
# 配置源路由
在下面的示例中,ens3在192.168.3.0/24网络上,ens5在192.168.5.0/24网络上。这使任一网络上的客户端都可以连接到另一网络,并使响应来自正确的接口。默认路由仍分配给ens5,允许任何其他流量通过。
network:
version: 2
renderer: networkd
ethernets:
ens3:
addresses:
- 192.168.3.30/24
dhcp4: no
routes:
- to: 192.168.3.0/24
via: 192.168.3.1
table: 101
routing-policy:
- from: 192.168.3.0/24
table: 101
ens5:
addresses:
- 192.168.5.24/24
dhcp4: no
gateway4: 192.168.5.1
routes:
- to: 192.168.5.0/24
via: 192.168.5.1
table: 102
routing-policy:
- from: 192.168.5.0/24
table: 102
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