ubuntu配置双网卡双网段IP走不同网关
2021/9/6 7:08:24
本文主要是介绍ubuntu配置双网卡双网段IP走不同网关,对大家解决编程问题具有一定的参考价值,需要的程序猿们随着小编来一起学习吧!
ubuntu配置双网卡双网段IP走不同网关
以前在使用centos时,得益于三层网络设备上层的配置,两个网段直接可以通过电信网关出去。现在要求联通走联通的网关,电信走电信的网关,对此,网络上的解决方法大体相同——加路由,对于ubuntu而言,就是在路由表(/etc/iproute2/rt_tables)中增加路由,把路由写进启动脚本(/etc/rc.local以及/etc/init.d/networking)。
比如,为了保密,我们选2个特别的地址来实验:
电信IP:172.18.33.20 netmask 255.255.255.128 gateway 172.18.33.1
联通IP:100.100.100.2 netmask 255.255.255.192 gateway 100.100.100.1
首先需要配置网卡信息,即在/etc/network/interfaces中写入以下内容:
auto lo
iface lo inet loopback
#The primary network interface
auto eth0
iface eth0 inet static
address 172.18.33.20
netmask 255.255.255.128
gateway 172.18.33.1
#dns-* options are implemented by the resolvconf package, if installed
dns-nameservers 8.8.8.8
auto eth1
iface eth1 inet static
address 100.100.100.2
netmask 255.255.255.192
保存后退出,其中联通网关是不用配的,执行sudo /etc/init.d/networking restart 使配置生效,此时,只能通一个IP。
然后需要在/etc/iproute2中增加2个路由表分别是电信:tel 联通:cnc ,这个表中有预留的内容,不能与之重复,一般从252往前到1是没被使用的,可以在 0 之前增加两条:
252 tel
251 cnc
然后保存退出。
现在我们可以增加路由规则了,直接在控制台输入命令:
#ip route flush table tel
#ip route add default via 172.18.33.1 dev eth0 src 172.18.33.20 table tel
#ip rule add from 172.18.33.20 table tel
此举可实现让电信的资源访问只从eth0网卡出去。
#ip route flush table cnc
#ip route add default via 100.100.100.1 dev eth1 src 100.100.100.2 table cnc
#ip rule add from 100.100.100.2 table cnc
此举可实现让联通的资源访问只从eth1网卡出去。
到现在为止,双线应该已经通了,但是重启之后,路由规则会失效,所以我们还要把路由规则写进两个脚本里——/etc/init.d/networking和/etc/rc.local,两个脚本操作方法相同,需要在结尾exit 0之前增加路由规则:
#ip route flush table tel
#ip route add default via 172.18.33.1 dev eth0 src 172.18.33.20 table tel
#ip ruleadd from 172.18.33.20 table tel
#ip route flush table cnc
#ip route add default via 100.100.100.1 dev eth1 src 100.100.100.2 table cnc
#ip rule add from 100.100.100.2 table cnc
exit 0
这样,系统重启和网络服务重启,都会自动加载路由规则。
为了以后方便配置更多服务器,简化配置过程,减少人为配置失误,笔者写了一个自动配置脚本,只需输入2个网络的IP地址,掩码和网关,就可以自动完成配置,已在ubuntu 12.04 server上试验成功。脚本如下:
#!/bin/bash
#d_net_auto.sh
#this script is used for “two networks with two interfaces” on Ubuntu Linux.
#the backup configuration files will be named “*.daibak” in the same directory.
#this script was tested on ubuntu 12.04 server…DaiSuchuan.2015
if [ whoami
!= “root” ];then
echo “run as root !”
else
#define colours
RED_COLOR=’\E[1;31m’
GREEN_COLOR=’\E[1;32m’
RES=’\E[0m’
#define directorys
interfacesDIR="/etc/network"
rt_tablesDIR="/etc/iproute2"
rc_localDIR="/etc"
networkingDIR="/etc/init.d"
#read configuration from basic input
echo -n “Enter CT IP address:”
read CTaddress
echo -n “Enter CT netmask:”
read CTnetmask
echo -n “Enter CT gateway:”
read CTgateway
echo
echo -n “Enter CNC IP address:”
read CNCaddress
echo -n “Enter CNC netmask:”
read CNCnetmask
echo -n “Enter CNC gateway:”
read CNCgateway
#print configurations
echo
echo “###############################”
echo “Please check the configurations”
echo -e “
R
E
D
C
O
L
O
R
C
T
{RED_COLOR}CT
REDCOLORCT{RES}:”
echo -e “address:
G
R
E
E
N
C
O
L
O
R
{GREEN_COLOR}
GREENCOLORCTaddress${RES}”
echo -e “netmask:
G
R
E
E
N
C
O
L
O
R
{GREEN_COLOR}
GREENCOLORCTnetmask${RES}”
echo -e “gateway:
G
R
E
E
N
C
O
L
O
R
{GREEN_COLOR}
GREENCOLORCTgateway
R
E
S
"
e
c
h
o
−
e
"
{RES}" echo -e "
RES"echo−e"{RED_COLOR}CNC${RES}:”
echo -e “address:
G
R
E
E
N
C
O
L
O
R
{GREEN_COLOR}
GREENCOLORCNCaddress${RES}”
echo -e “netmask:
G
R
E
E
N
C
O
L
O
R
{GREEN_COLOR}
GREENCOLORCNCnetmask${RES}”
echo -e “gateway:
G
R
E
E
N
C
O
L
O
R
{GREEN_COLOR}
GREENCOLORCNCgateway${RES}”
#check configurations
echo “Are all those above right ? (y/n)”
read chk
echo
if [ KaTeX parse error: Expected 'EOF', got '#' at position 62: …ns......" echo #̲1.backup files …{RED_COLOR}1
R
E
S
.
B
a
c
k
u
p
c
o
n
f
i
g
u
r
a
t
i
o
n
s
.
.
.
.
.
.
"
e
c
h
o
i
f
[
!
−
f
"
{RES}.Backup configurations......" echo if [ ! -f "
RES.Backupconfigurations......"echoif[!−f"interfacesDIR/interfaces.daibak" ];then
cp “
i
n
t
e
r
f
a
c
e
s
D
I
R
/
i
n
t
e
r
f
a
c
e
s
"
"
interfacesDIR/interfaces" "
interfacesDIR/interfaces""interfacesDIR/interfaces.daibak”
else
echo “interfaces.daibak has existed ! Nothing to do.”
fi
if [ ! -f “
r
t
t
a
b
l
e
s
D
I
R
/
r
t
t
a
b
l
e
s
.
d
a
i
b
a
k
"
]
;
t
h
e
n
c
p
"
rt_tablesDIR/rt_tables.daibak" ];then cp "
rttablesDIR/rttables.daibak"];thencp"rt_tablesDIR/rt_tables” “
r
t
t
a
b
l
e
s
D
I
R
/
r
t
t
a
b
l
e
s
.
d
a
i
b
a
k
"
e
l
s
e
e
c
h
o
"
r
t
t
a
b
l
e
s
.
d
a
i
b
a
k
h
a
s
e
x
i
s
t
e
d
!
N
o
t
h
i
n
g
t
o
d
o
.
"
f
i
i
f
[
!
−
f
"
rt_tablesDIR/rt_tables.daibak" else echo "rt_tables.daibak has existed ! Nothing to do." fi if [ ! -f "
rttablesDIR/rttables.daibak"elseecho"rttables.daibakhasexisted!Nothingtodo."fiif[!−f"rc_localDIR/rc.local.daibak” ];then
cp “
r
c
l
o
c
a
l
D
I
R
/
r
c
.
l
o
c
a
l
"
"
rc_localDIR/rc.local" "
rclocalDIR/rc.local""rc_localDIR/rc.local.daibak”
else
echo “rc.local.daibak has existed ! Nothing to do.”
fi
if [ ! -f “
n
e
t
w
o
r
k
i
n
g
D
I
R
/
n
e
t
w
o
r
k
i
n
g
.
d
a
i
b
a
k
"
]
;
t
h
e
n
c
p
"
networkingDIR/networking.daibak" ];then cp "
networkingDIR/networking.daibak"];thencp"networkingDIR/networking” “
n
e
t
w
o
r
k
i
n
g
D
I
R
/
n
e
t
w
o
r
k
i
n
g
.
d
a
i
b
a
k
"
e
l
s
e
e
c
h
o
"
n
e
t
w
o
r
k
i
n
g
.
d
a
i
b
a
k
h
a
s
e
x
i
s
t
e
d
!
N
o
t
h
i
n
g
t
o
d
o
.
"
f
i
e
c
h
o
−
e
"
networkingDIR/networking.daibak" else echo "networking.daibak has existed ! Nothing to do." fi echo -e "
networkingDIR/networking.daibak"elseecho"networking.daibakhasexisted!Nothingtodo."fiecho−e"{GREEN_COLOR}Done.KaTeX parse error: Expected 'EOF', got '#' at position 18: …ES}" echo echo #̲2.start configu…{RED_COLOR}2${RES}.Configure interfaces…”
echo
touch .interfaces
#Primery Network
echo “auto lo” > .interfaces
echo “iface lo inet loopback” >> .interfaces
echo “auto eth0” >> .interfaces
echo “iface eth0 inet static” >> .interfaces
echo “address $CTaddress” >> .interfaces
echo “netmask $CTnetmask” >> .interfaces
echo “gateway $CTgateway” >> .interfaces
echo “#dns-* options are implemented by the resolvconf package,if installed” >> .interfaces
echo “dns-nameservers 8.8.8.8” >> .interfaces
#Secondary Network
echo “auto eth1” >> .interfaces
echo “iface eth1 inet static” >> .interfaces
echo “address $CNCaddress” >> .interfaces
echo “netmask
C
N
C
n
e
t
m
a
s
k
"
>
>
.
i
n
t
e
r
f
a
c
e
s
c
h
m
o
d
644.
/
.
i
n
t
e
r
f
a
c
e
s
c
p
−
f
.
/
.
i
n
t
e
r
f
a
c
e
s
"
CNCnetmask" >> .interfaces chmod 644 ./.interfaces cp -f ./.interfaces "
CNCnetmask">>.interfaceschmod644./.interfacescp−f./.interfaces"interfacesDIR/interfaces”
echo -e “
G
R
E
E
N
C
O
L
O
R
D
o
n
e
.
{GREEN_COLOR}Done.
GREENCOLORDone.{RES}”
echo
echo
#3.configure rt_tables
echo -e “
R
E
D
C
O
L
O
R
3
{RED_COLOR}3
REDCOLOR3{RES}.Configure rt_tables…”
echo
sed ‘/^0/i\252\ttel’ “KaTeX parse error: Undefined control sequence: \2 at position 51: …es sed '/^0/i\̲2̲51\tcnc' ._rt_t…rt_tablesDIR/rt_tables”
echo -e “
G
R
E
E
N
C
O
L
O
R
D
o
n
e
.
{GREEN_COLOR}Done.
GREENCOLORDone.{RES}”
echo
echo
#4.configure rc.local
echo -e “
R
E
D
C
O
L
O
R
4
{RED_COLOR}4
REDCOLOR4{RES}.Configure rc.local…”
echo
#Primary route
sed ‘/^exit/i\ip route flush table tel’ “KaTeX parse error: Undefined control sequence: \ip at position 53: … sed '/^exit/i\̲i̲p̲ ̲route add defau…CTgateway’ dev eth0 src ‘KaTeX parse error: Undefined control sequence: \ip at position 66: … sed '/^exit/i\̲i̲p̲ ̲rule add from 'CTaddress’ table tel’ ._rc.local.2 > ._rc.local.3
#Secondary route
sed ‘/^exit/i\ip route flush table cnc’ ._rc.local.3 > ._rc.local.4
sed ‘/^exit/i\ip route add default via ‘
C
N
C
g
a
t
e
w
a
y
′
d
e
v
e
t
h
1
s
r
c
′
CNCgateway' dev eth1 src '
CNCgateway′deveth1src′CNCaddress’ table cnc’ ._rc.local.4 > ._rc.local.5
sed '/^exit/i\ip rule add from '
C
N
C
a
d
d
r
e
s
s
′
t
a
b
l
e
c
n
c
′
.
r
c
.
l
o
c
a
l
.
5
>
.
r
c
.
l
o
c
a
l
c
h
m
o
d
755.
/
.
r
c
.
l
o
c
a
l
c
p
−
f
.
/
.
r
c
.
l
o
c
a
l
"
CNCaddress' table cnc' ._rc.local.5 > .rc.local chmod 755 ./.rc.local cp -f ./.rc.local "
CNCaddress′tablecnc′.rc.local.5>.rc.localchmod755./.rc.localcp−f./.rc.local"rc_localDIR/rc.local”
echo -e “
G
R
E
E
N
C
O
L
O
R
D
o
n
e
.
{GREEN_COLOR}Done.
GREENCOLORDone.{RES}”
echo
echo
#5.configure networking
echo -e “
R
E
D
C
O
L
O
R
5
{RED_COLOR}5
REDCOLOR5{RES}.Configure networking…”
echo
#Primary route
sed ‘/^exit/i\ip route flush table tel’ "KaTeX parse error: Undefined control sequence: \ip at position 59: … sed '/^exit/i\̲i̲p̲ ̲route add defau…CTgateway’ dev eth0 src ‘KaTeX parse error: Undefined control sequence: \ip at position 70: … sed '/^exit/i\̲i̲p̲ ̲rule add from 'CTaddress’ table tel’ ._networking.2 > ._networking.3
#Secondary route
sed ‘/^exit/i\ip route flush table cnc’ ._networking.3 > ._networking.4
sed ‘/^exit/i\ip route add default via ‘
C
N
C
g
a
t
e
w
a
y
′
d
e
v
e
t
h
1
s
r
c
′
CNCgateway' dev eth1 src '
CNCgateway′deveth1src′CNCaddress’ table cnc’ ._networking.4 > .networking.5
sed '/^exit/i\ip rule add from '
C
N
C
a
d
d
r
e
s
s
′
t
a
b
l
e
c
n
c
′
.
n
e
t
w
o
r
k
i
n
g
.
5
>
.
n
e
t
w
o
r
k
i
n
g
c
h
m
o
d
755.
/
.
n
e
t
w
o
r
k
i
n
g
c
p
.
/
.
n
e
t
w
o
r
k
i
n
g
"
CNCaddress' table cnc' ._networking.5 > .networking chmod 755 ./.networking cp ./.networking "
CNCaddress′tablecnc′.networking.5>.networkingchmod755./.networkingcp./.networking"networkingDIR/networking"
echo -e “
G
R
E
E
N
C
O
L
O
R
D
o
n
e
.
{GREEN_COLOR}Done.
GREENCOLORDone.{RES}”
echo
echo
#6.start network
echo -e “
R
E
D
C
O
L
O
R
6
{RED_COLOR}6
REDCOLOR6{RES}.Start networking…”
/etc/init.d/networking restart
echo -e “
G
R
E
E
N
C
O
L
O
R
D
o
n
e
.
{GREEN_COLOR}Done.
GREENCOLORDone.{RES}”
#remove cache
rm -f ./.* ./.interfaces ./.rt_tables ./.rc.local ./.networking
else
echo “Configuration is stop ! Please restart this script and reconfigure !”
exit 1
fi
echo “All configurations are complete! Maybe they will work after rebooting your system.”
fi
echo
echo “This script is created by DaiSuchuan with Ubuntu Linux…2015.06.12”
exit 0
原文链接:https://blog.csdn.net/saintdsc/article/details/46470619
这篇关于ubuntu配置双网卡双网段IP走不同网关的文章就介绍到这儿,希望我们推荐的文章对大家有所帮助,也希望大家多多支持为之网!
- 2024-11-23Springboot应用的多环境打包入门
- 2024-11-23Springboot应用的生产发布入门教程
- 2024-11-23Python编程入门指南
- 2024-11-23Java创业入门:从零开始的编程之旅
- 2024-11-23Java创业入门:新手必读的Java编程与创业指南
- 2024-11-23Java对接阿里云智能语音服务入门详解
- 2024-11-23Java对接阿里云智能语音服务入门教程
- 2024-11-23JAVA对接阿里云智能语音服务入门教程
- 2024-11-23Java副业入门:初学者的简单教程
- 2024-11-23JAVA副业入门:初学者的实战指南