系统环境

  • 系统版本:Debian 12 (bookworm)
  • 内核版本:6.1.0-17-amd64 #1 SMP PREEMPT_DYNAMIC Debian 6.1.69-1 (2023-12-30) x86_64 GNU/Linux

网络配置

配置前的准备(重点)

能访问公网的情况

先确认系统是否有安装从网卡配置文件中读取 dns 配置参数的包 resolvconf 或者 openresov 包(二者选一):

1
2
3
4
5
root@debian:~# apt list --installed | egrep 'resolvconf|openresolv'

WARNING: apt does not have a stable CLI interface. Use with caution in scripts.

resolvconf/stable,now 1.91+nmu1 all [installed]

从上面获取到的内容可以看出,系统已经安装好了 resolvconf 包,所以不需要再安装 openresolv 包。

不能访问公网的情况

编辑 /etc/resolv.conf 文件,添加 dns 配置内容:

1
2
nameserver 223.5.5.5
nameserver 114.114.114.114

然后安装下 resolvconf 或者 openresov 包!

静态 IP 地址配置

1.编辑网卡配置文件 /etc/network/interfaces ,配置内容如下:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
leazhi@debian:~$ cat /etc/network/interfaces
# This file describes the network interfaces available on your system
# and how to activate them. For more information, see interfaces(5).

source /etc/network/interfaces.d/*

# The loopback network interface
auto lo
iface lo inet loopback

# The primary network interface
allow-hotplug eth0
##iface eth0 inet dhcp
iface eth0 inet manual
address 192.168.3.23
network 255.255.255.0
gateway 192.168.3.1
broadcast 192.168.3.255
dns-nameserver 114.114.114.114

2.重启网络服务:

1
leazhi@debian:~$ sudo systemctl restart networking

3.验证 IP 地址是否配置成功:

1
2
3
4
5
6
7
8
9
10
11
12
13
leazhi@debian:~$ ip a
1: lo: <LOOPBACK,UP,LOWER_UP> mtu 65536 qdisc noqueue state UNKNOWN group default qlen 1000
link/loopback 00:00:00:00:00:00 brd 00:00:00:00:00:00
inet 127.0.0.1/8 scope host lo
valid_lft forever preferred_lft forever
inet6 ::1/128 scope host noprefixroute
valid_lft forever preferred_lft forever
2: eth0: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc mq state UP group default qlen 1000
link/ether 00:15:5d:03:73:03 brd ff:ff:ff:ff:ff:ff
inet 192.168.137.23/24 brd 192.168.137.255 scope global eth0
valid_lft forever preferred_lft forever
inet6 fe80::215:5dff:fe03:7303/64 scope link
valid_lft forever preferred_lft forever

网络桥接配置

需忽略上面 静态 IP 地址配置 步骤,因为上面配置的是 eth0 网卡,而这里需要配置的是 br0 网桥。

1.安装必要的软件包:

1
leazhi@debian:~$ sudo apt-get install bridge-utils

2.编辑网卡配置文件 /etc/network/interface ,配置内容如下:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
leazhi@debian:~$ cat /etc/network/interfaces
# This file describes the network interfaces available on your system
# and how to activate them. For more information, see interfaces(5).

source /etc/network/interfaces.d/*

# The loopback network interface
auto lo
iface lo inet loopback


# The primary network interface
#allow-hotplug eth0
##iface eth0 inet dhcp

3.在 /etc/network/interfaces.d/ 目录下创建一个名为 br0 的文件,并添加以下内容:

1
2
3
4
5
6
7
8
9
10
11
12
13
leazhi@debian:~$ cat /etc/network/interfaces.d/br0
auto br0
iface br0 inet static
address 192.168.137.24
netmask 255.255.255.0
gateway 192.168.137.1
broadcast 192.168.137.255
bridge_ports eth0
bridge_stp off
bridge_waitport 0
bridge_fd 0
dns-nameserver 192.168.137.1
hwaddress ether 00:15:5d:03:73:03

在配置网桥的配置文件中,一定要加上 hwaddress ether 参数,且该参数的 MAC 地址一定要指向桥接的网卡 (eth0) 的 MAC。否则有可能出现网桥无法正常工作的情况(比如无法 ping 通网关,无法访问公网,宿主机无法 ping 虚拟机 等等)。

4.重启网络服务:

1
leazhi@debian:~$ sudo systemctl restart networking

5.验证网桥是否成功创建:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
leazhi@debian:~$ ip a
1: lo: <LOOPBACK,UP,LOWER_UP> mtu 65536 qdisc noqueue state UNKNOWN group default qlen 1000
link/loopback 00:00:00:00:00:00 brd 00:00:00:00:00:00
inet 127.0.0.1/8 scope host lo
valid_lft forever preferred_lft forever
inet6 ::1/128 scope host noprefixroute
valid_lft forever preferred_lft forever
2: eth0: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc mq master br0 state UP group default qlen 1000
link/ether 00:15:5d:03:73:03 brd ff:ff:ff:ff:ff:ff
8: br0: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc noqueue state UP group default qlen 1000
link/ether 00:15:5d:03:73:03 brd ff:ff:ff:ff:ff:ff
inet 192.168.137.24/24 brd 192.168.137.255 scope global br0
valid_lft forever preferred_lft forever
inet6 fe80::c0d7:6bff:fed1:db58/64 scope link
valid_lft forever preferred_lft forever

注意看,这里 eth0br0 的 MAC 地址是一样的。