debian12 三网卡配置三线 多路由配置
假设 服务器有三块网卡分别接入 移动, 联通 电信
电信网关 192.168.0.1 服务器内网 IP 192.168.0.6 网卡名 enp9s0
联通网关 192.168.1.1 服务器内网 IP 192.168.1.6 网卡名 enp6s0
移动网关 192.168.2.1 服务器内网 IP 192.168.2.6 网卡名 enp8s0
假设 服务器默认走 联通线路
则编辑 /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
最终文件为
————————————————————————————————————————————————————————————————————————————————
#
# reserved values
#
255 local
254 main
253 default
252 ct
251 cnc
250 cmcc
0 unspec
#
# local
#
#1 inr.ruhep
——————————————————————————————————————————————————
然后 命令行下执行如下命令
ip route add default dev enp9s0 via 192.168.0.1 table ct
ip route add default dev enp8s0 via 192.168.2.1 table cmcc
ip rule add from 192.168.0.6 table ct
ip rule add from 192.168.2.6 table cmcc
cat > /etc/rc.local <<EOF
#!/bin/sh -e
#
# rc.local
#
# This script is executed at the end of each multiuser runlevel.
# Make sure that the script will "exit 0" on success or any other
# value on error.
#
# In order to enable or disable this script just change the execution
# bits.
#
# By default this script does nothing.
# bash /root/bindip.sh
编辑路由策略脚本
命令行下执行
cat > /opt/ct_cnc_cmcc.sh <<EOF
#!/bin/bash
ip route add default dev enp9s0 via 192.168.0.1 table ct
ip route add default dev enp8s0 via 192.168.2.1 table cmcc
ip rule add from 192.168.0.6 table ct
ip rule add from 192.168.2.6 table cmcc
EOF
设置执行权限
命令行下执行
chmod +x /opt/ct_cnc_cmcc.sh #给脚本添加执行权限
将脚本添加到rc.local文件中去
命令行下执行
cat > /etc/rc.local <<EOF
#!/bin/sh -e
#
# rc.local
#
# This script is executed at the end of each multiuser runlevel.
# Make sure that the script will "exit 0" on success or any other
# value on error.
#
# In order to enable or disable this script just change the execution
# bits.
#
# By default this script does nothing.
# bash /root/bindip.sh
bash /opt/ct_cnc_cmcc.sh
exit 0
EOF
重启rc-local
命令行下执行
systemctl restart rc-local