Debian系统配置三线运营商
电信线路
IP | 192.168.1.2 |
---|---|
网关 | 192.168.1.1 |
掩码 | 255.255.255.0 |
联通线路
IP | 10.10.10.2 |
---|---|
网关 | 10.10.10.1 |
掩码 | 255.255.255.0 |
移动线路
IP | 176.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的,所以我们需要自己配置一个
- 创建一个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执行权限
- 启动rc-local服务,并设置开机自启动
systemctl enable rc-local && systemctl restart rc-local
5.将路由策略写入脚本,并将脚本添加到rc.local文件
- 编辑路由策略脚本
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 #给脚本添加执行权限
- 将脚本添加到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的,所以我们需要自己配置一个
- 创建一个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
- 创建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执行权限
- 启动rc-local服务,并设置开机自启动
systemctl enable rc-local && systemctl restart rc-local
5.将路由策略写入脚本,并将脚本添加到rc.local文件
- 编辑路由策略脚本
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 #给脚本添加执行权限
- 将脚本添加到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