我建议采用以下方法:
- 确保网桥接口已关闭
- 配置网桥接口
- 执行
ifconfig eth0 down && ifconfig br0 up
并恢复:
- 执行
ifconfig br0 down && ifconfig eth0 up
现在对于路线,这取决于您拥有什么样的路线。如果您使用显式接口定义静态路由,您唯一的选择似乎是ip route ls
将它们解析并转换为新接口。
您还可以玩弄 up & down 命令的顺序以及多个路由表:
ip route add <whatever> table 2
ip rule add from br0 table 2
但这可能会变得棘手,所以我的建议是坚持简单的解决方案,即使它包含更多编码。
这是另一个来自 xendnetwork-bridge
脚本的示例来实现这一点:
# Usage: transfer_addrs src dst
# Copy all IP addresses (including aliases) from device $src to device $dst.
transfer_addrs () {
local src=$1
local dst=$2
# Don't bother if $dst already has IP addresses.
if ip addr show dev ${dst} | egrep -q '^ *inet ' ; then
return
fi
# Address lines start with 'inet' and have the device in them.
# Replace 'inet' with 'ip addr add' and change the device name $src
# to 'dev $src'.
ip addr show dev ${src} | egrep '^ *inet ' | sed -e "
s/inet/ip addr add/
s@\([0-9]\+\.[0-9]\+\.[0-9]\+\.[0-9]\+/[0-9]\+\)@\1@
s/${src}/dev ${dst}/
" | sh -e
# Remove automatic routes on destination device
ip route list | sed -ne "
/dev ${dst}\( \|$\)/ {
s/^/ip route del /
p
}" | sh -e
}
# Usage: transfer_routes src dst
# Get all IP routes to device $src, delete them, and
# add the same routes to device $dst.
# The original routes have to be deleted, otherwise adding them
# for $dst fails (duplicate routes).
transfer_routes () {
local src=$1
local dst=$2
# List all routes and grep the ones with $src in.
# Stick 'ip route del' on the front to delete.
# Change $src to $dst and use 'ip route add' to add.
ip route list | sed -ne "
/dev ${src}\( \|$\)/ {
h
s/^/ip route del /
P
g
s/${src}/${dst}/
s/^/ip route add /
P
d
}" | sh -e
}