Linux 多线路多网卡配置

#!/bin/bash

# 在/etc/iproute2/rt_tables中default和unspec之间添加eth0和eth1两个表
# 255     local
# 254     main
# 253     default
# 252     eth0
# 251     eth1
# 0       unspec
#
# 假如eth0为主网卡,则eth0正常配置,其余网卡只配置静态IP和掩码,不配置网关。
# eth0 10.0.0.2/24 gateway 10.0.0.1
# eth1 10.0.1.2/24 gateway 10.0.1.1

dev=(
    eth0
    eth1
    eth2
    eth3
)
addr=(
    10.0.0.2
    10.0.1.2
    10.0.2.2
    10.0.3.2
)
gw=(
    10.0.0.1
    10.0.1.1
    10.0.2.1
    10.0.3.1
)
subnet=(
    10.0.0.0/24
    10.0.1.0/24
    10.0.2.0/24
    10.0.3.0/24
)

for ((i=0; i<${#dev[*]}; i++)); do
    ip route flush table ${dev[i]}
    ip route add default via ${gw[i]} dev ${dev[i]} src ${addr[i]} table ${dev[i]}
    ip route add ${subnet[i]} dev ${dev[i]} src ${addr[i]} table ${dev[i]}
    ip route add 127.0.0.0/8 dev lo table ${dev[i]}
    ip rule add from ${addr[i]} table ${dev[i]}
done

# 如果其余网卡配置了网关,需要添加下面一条命令,更改默认网关
# ip route change default via ${gw[0]} dev ${dev[0]}

你可能感兴趣的:(Linux 多线路多网卡配置)