我想为 Debian Linux 上运行的虚拟化环境(KVM)设置网桥。那么我该如何在 Debian Linux 服务器上的 /etc/network/interfaces
中设置桥接网络呢?
如何你想为你的虚拟机分配 IP 地址并使其可从你的局域网访问,则需要设置网络桥接器。默认情况下,虚拟机使用 KVM 创建的专用网桥。但你需要手动设置接口,避免与网络管理员发生冲突。
你需要编辑 /etc/network/interface
文件。不过,我建议在 /etc/network/interface.d/
目录下放置一个全新的配置。在 Debian Linux 配置网桥的过程如下:
1.安装 bridge-utils
apt install bridge-utils
2.查看物理网卡名称
可以使用以下命令查看
ip a
ifconfig # ifconfig 需要安装net-tools
我的网卡名称是 enp3s0
3.更新 /etc/network/interface 文件
确保只有 lo(loopback 在 /etc/network/interface 中处于活动状态)。(LCTT 译注:loopback 指本地环回接口,也称为回送地址)删除与 enp3s0
相关的任何配置。
# 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
#allow-hotplug enp3s0
#iface enp3s0 inet static
# address 192.168.3.120
# netmask 255.255.255.0
# gateway 192.168.3.1
# This is an autoconfigured IPv6 interface
#iface enp3s0 inet6 dhcp
注释掉的那些是之前的网络配置,要配置网桥就给他注释掉,保留loopback
配置。
4. /etc/network/interfaces.d/br0 中配置网桥
vim /etc/network/interfaces.d/br0
## static ip config file for br0 ##
auto br0
iface br0 inet static
address 192.168.3.120
broadcast 192.168.3.255
netmask 255.255.255.0
gateway 192.168.3.1
# If the resolvconf package is installed, you should not edit
# the resolv.conf configuration file manually. Set name server here
#dns-nameservers 192.168.3.255
# If you have muliple interfaces such as eth0 and eth1
# bridge_ports eth0 eth1
bridge_ports enp3s0 # 我的网卡名称
bridge_stp off # disable Spanning Tree Protocol
bridge_waitport 0 # no delay before a port becomes available
bridge_fd 0 # no forwarding delay
如果你想使用 DHCP 来获得 IP 地址:
## DHCP ip config file for br0 ##
auto br0
# Bridge setup
iface br0 inet dhcp
bridge_ports eno1
保存退出
5.重新启动网络服务
systemctl restart networking
执行完如果SSH 没反应了,就是配置失败了,这时候就得去物理机上检查了,所以执行重启网络服务,所以得确保配置没问题。
确认网络服务已经重新启动
systemctl status networking
6.测试查看
借助 ip 命令寻找新的 br0 接口和路由表:
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
valid_lft forever preferred_lft forever
2: enp3s0: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc mq master br0 state UP group default qlen 1000
link/ether 8c:89:a5:0e:aa:24 brd ff:ff:ff:ff:ff:ff
3: br0: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc noqueue state UP group default qlen 1000
link/ether 8e:1f:ab:f2:f9:de brd ff:ff:ff:ff:ff:ff
inet 192.168.3.120/24 brd 192.168.3.255 scope global br0
valid_lft forever preferred_lft forever
inet6 2408:1118:1110:deb0:1111:1111:1112:1111/64 scope global dynamic mngtmpaddr
valid_lft 221352sec preferred_lft 134952sec
inet6 fe80::8c1f:abff:fef2:f9de/64 scope link
valid_lft forever preferred_lft forever
ip r
default via 192.168.3.1 dev br0 onlink
169.254.0.0/16 dev br0 scope link metric 1000
192.168.3.0/24 dev br0 proto kernel scope link src 192.168.3.120
使用 brctl 命令查看网桥有关信息:
brctl show
bridge name bridge id STP enabled interfaces
br0 8000.8e1fabf2f9de no enp3s0
显示当前网桥
bridge link
2: enp3s0: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 master br0 state forwarding priority 32 cost 4
参考文章:https://linux.cn/article-9492-1.html