Debian系统配置三线

Debian系统配置三线运营商

电信线路

IP192.168.1.2
网关192.168.1.1
掩码255.255.255.0

联通线路

IP10.10.10.2
网关10.10.10.1
掩码255.255.255.0

移动线路

IP176.10.10.2
网关176.10.10.1
掩码255.255.255.0

三网卡配置三线

1.修改网卡配置文件

这里我们以电信网关作为默认网关,所以只需要配置电信的网关,移动和联通不需要配置网关

vi /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
auto eth0
iface eth0 inet static
        address 192.168.1.2  #电信IP
        gateway 192.168.1.1  #电信网关
        netmask 255.255.255.0 #掩码
        # dns-* options are implemented by the resolvconf package, if installed
        dns-nameservers 114.114.114.114
auto eth1
iface eth1 inet static
        address 10.10.10.2  #联通IP
        netmask 255.255.255.0  #掩码
        # dns-* options are implemented by the resolvconf package, if installed
        dns-nameservers 114.114.114.114
auto eth2
iface eth2 inet static
        address 172.10.10.2  #移动IP
        netmask 255.255.255.0  #掩码
        # dns-* options are implemented by the resolvconf package, if installed
        dns-nameservers 114.114.114.114

2.重启网络

systemctl restart networking

3.配置路由表

在路由表中添加
252 ct
251 cnc
250 cmcc

vi /etc/iproute2/rt_tables

#
# reserved values
#
255     local
254     main
253     default
252     ct
251     cnc
250     cmcc
0       unspec
#
# local
#
#1      inr.ruhep

3.在命令行刷路由策略

ip route add default dev eth1 via 10.10.10.1 table cnc
ip route add default dev eth2 via 172.10.10.1 table cmcc
ip rule add from 10.10.10.2 table cnc
ip rule add from 172.10.10.2 table cmcc

现在三线已经都可以通外网了,走的是电信的网关,但是现在的配置只是临时的,所以需要让系统开机自动运行这些路由策略

4.创建rc.local开机自启文件

Debian系统默认是没有rc.local的,所以我们需要自己配置一个

  1. 创建一个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
  1. 创建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执行权限
  1. 启动rc-local服务,并设置开机自启动
systemctl enable rc-local && systemctl restart rc-local

5.将路由策略写入脚本,并将脚本添加到rc.local文件

  1. 编辑路由策略脚本
cat > /opt/ct_cnc_cmcc.sh <<EOF  
#!/bin/bash
ip route add default dev eth1 via 10.10.10.1 table cnc
ip route add default dev eth2 via 172.10.10.1 table cmcc
ip rule add from 10.10.10.2 table cnc
ip rule add from 172.10.10.2 table cmcc
EOF
chmod +x /opt/ct_cnc_cmcc.sh #给脚本添加执行权限
  1. 将脚本添加到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
systemctl restart rc-local

单网卡配置三线

1.修改网卡配置文件

这里我们以电信网关作为默认网关,所以只需要配置电信的网关,移动和联通不需要配置网关

vi /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
auto eth0
iface eth0 inet static
        address 192.168.1.2  #电信IP
        gateway 192.168.1.1  #电信网关
        netmask 255.255.255.0 #掩码
        # dns-* options are implemented by the resolvconf package, if installed
        dns-nameservers 114.114.114.114
auto eth0:0
iface eth0:0 inet static
        address 10.10.10.2  #联通IP
        netmask 255.255.255.0  #掩码
        # dns-* options are implemented by the resolvconf package, if installed
        dns-nameservers 114.114.114.114
auto eth0:1
iface eth0:0 inet static
        address 172.10.10.2  #移动IP
        netmask 255.255.255.0  #掩码
        # dns-* options are implemented by the resolvconf package, if installed
        dns-nameservers 114.114.114.114

2.重启网络

systemctl restart networking

3.配置路由表

在路由表中添加
252 ct
251 cnc
250 cmcc

vi /etc/iproute2/rt_tables

#
# reserved values
#
255     local
254     main
253     default
252     ct
251     cnc
250     cmcc
0       unspec
#
# local
#
#1      inr.ruhep

3.在命令行刷路由策略

ip route add default dev eth0:0 via 10.10.10.1 table cnc
ip route add default dev eth0:1 via 172.10.10.1 table cmcc
ip rule add from 10.10.10.2 table cnc
ip rule add from 172.10.10.2 table cmcc

现在三线已经都可以通外网了,走的是电信的网关,但是现在的配置只是临时的,所以需要让系统开机自动运行这些路由策略

4.创建rc.local开机自启文件

Debian系统默认是没有rc.local的,所以我们需要自己配置一个

  1. 创建一个rc-local服务
cat > /etc/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
  1. 创建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执行权限
  1. 启动rc-local服务,并设置开机自启动
systemctl enable rc-local && systemctl restart rc-local

5.将路由策略写入脚本,并将脚本添加到rc.local文件

  1. 编辑路由策略脚本
cat > /opt/ct_cnc_cmcc.sh <<EOF  
#!/bin/bash
ip route add default dev eth0:0 via 10.10.10.1 table cnc
ip route add default dev eth0:1 via 172.10.10.1 table cmcc
ip rule add from 10.10.10.2 table cnc
ip rule add from 172.10.10.2 table cmcc
EOF
chmod +x /opt/ct_cnc_cmcc.sh #给脚本添加执行权限
  1. 将脚本添加到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
systemctl restart rc-local
  • 0
    点赞
  • 8
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

花前月下猴

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值