返回列表 发布新帖

【笔记】 debian12 三网卡配置三线 多路由配置

152 0
发表于 2025-1-8 04:15:20 | 查看全部 阅读模式

马上注册,结交更多好友,享用更多功能,让你轻松玩转社区。

您需要 登录 才可以下载或查看,没有账号?立即注册

×
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


# The primary network interface
allow-hotplug enp6s0
iface enp6s0 inet static
address 192.168.1.6
netmask 255.255.255.0
gateway 192.168.1.1




auto enp9s0
iface enp9s0 inet static
address 192.168.0.6
netmask 255.255.255.0
dns-nameservers 114.114.114.114


auto enp8s0
iface enp8s0 inet static
address 192.168.2.6
netmask 255.255.255.0
dns-nameservers 114.114.114.114


————————————————————————————————————————————————————————————————————————————
然后 重启网络
命令  sudo /etc/init.d/networking restart


然后 编译  /etc/iproute2/rt_tables
增加


252 ct
251 cnc
250 cmcc


最终文件为
————————————————————————————————————————————————————————————————————————————————
#
# 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   


目前已经可以三网外网通信正常了。 重启后会失效。
解决重启失效方法如下。 服务器重启后,继续如下操作


创建rc.local开机自启文件
命令行下执行  
cat > /lib/systemd/system/rc-local.service <<EOF
[Unit]
Description=/etc/rc.local
ConditionPathExists=/etc/rc.local
[Service]
Type=forking
ExecStart=/etc/rc.local start
TimeoutSec=0
StandardOutput=tty
RemainAfterExit=yes
SysVStartPriority=99
[Install]
WantedBy=multi-user.target
EOF




创建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


exit 0
EOF


赋予执行权限  
命令行下执行
chmod +x /etc/rc.local  




启动rc-local服务,并设置开机自启动
命令行下执行
systemctl enable rc-local && systemctl restart rc-local


编辑路由策略脚本
命令行下执行
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


重启服务器后测试, 一切正常即可。



















回复

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

头条资讯换一批
扫一扫访问小程序
获取最新资讯
关灯 在本版发帖
扫一扫添加微信客服
QQ客服返回顶部
快速回复 返回顶部 返回列表