Linux双网卡双网关的配置

Linux网络配置

rhel6.5双网卡双网关的配置

采用一个网卡配置电信地址,另一个网卡配置联通地址,安装好rhel6.5系统后配置完IP,发现联通地址和电信地址只能有一个可以ping通,若电信地址配置网关,联通地址不配网关,则只能ping通电信地址,反之只能ping通联通地址,若同时配置联通和电信地址则两个都不通。

rhel6.5中实现双网卡双网关

服务器环境如下:
系统:RHEL6.5
电信IP(TEL):114.80.10.79 netmask 255.255.255.128 gateway 114.80.10.1
联通IP(CNC):112.65.20.23 netmask 255.255.255.128 gateway 112.65.20.1

1、配置网卡信息

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
vi /etc/sysconfig/network-scripts/ifcfg-eth12
DEVICE=eth12
TYPE=Ethernet
ONBOOT=yes
BOOTPROTO=static
IPADDR=114.80.10.79
NETMASK=255.255.255.128
vi /etc/sysconfig/network-scripts/ifcfg-eth14
DEVICE=eth14
TYPE=Ethernet
ONBOOT=yes
BOOTPROTO=static
IPADDR=112.65.20.23
NETMASK=255.255.255.128

注意:两个网卡配置文件里不加网关.如果加网关,那么在route -n中只会显示一条默认路由,另一个网段是不通的。

1
2
3
4
5
6
7
8
9
10
[root@test network-scripts]# route -n
Kernel IP routing table
Destination Gateway Genmask Flags Metric Ref Use Iface
112.65.20.0 0.0.0.0 255.255.255.128 U 0 0 0 eth14
114.80.10.0 0.0.0.0 255.255.255.128 U 0 0 0 eth12
112.65.20.0 0.0.0.0 255.255.255.0 U 0 0 0 eth14
114.80.10.0 0.0.0.0 255.255.255.0 U 0 0 0 eth12
169.254.0.0 0.0.0.0 255.255.0.0 U 1016 0 0 eth14
169.254.0.0 0.0.0.0 255.255.0.0 U 1016 0 0 eth12
0.0.0.0 112.65.20.1 255.255.255.128 UG 0 0 0 eth14

2、修改rc.local
可以直接增加这两条路由,但是重启后会丢失。

1
2
route add -net 114.80.10.0/25 gw 114.80.10.1 dev eth12
route add -net 112.65.20.0/25 gw 112.65.20.1 dev eth14

所以为永久生效,还是修改rc.local

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
vi /etc/rc.d/rc.local
[root@test network-scripts]# cat /etc/rc.d/rc.local
#!/bin/sh
#
# This script will be executed *after* all the other init scripts.
# You can put your own initialization stuff in here if you don't
# want to do the full Sys V style init stuff.
touch /var/lock/subsys/local
route add -net 114.80.10.0/25 gw 114.80.10.1 dev eth12
route add -net 112.65.20.0/25 gw 112.65.20.1 dev eth14
然后route -n
[root@test network-scripts]# route -n
Kernel IP routing table
Destination Gateway Genmask Flags Metric Ref Use Iface
112.65.20.0 112.65.20.1 255.255.255.128 UG 0 0 0 eth14
114.80.10.0 114.80.10.1 255.255.255.128 UG 0 0 0 eth12
112.65.20.0 0.0.0.0 255.255.255.0 U 0 0 0 eth14
114.80.10.0 0.0.0.0 255.255.255.0 U 0 0 0 eth12
169.254.0.0 0.0.0.0 255.255.0.0 U 1016 0 0 eth14
169.254.0.0 0.0.0.0 255.255.0.0 U 1016 0 0 eth12
#如果只需要添加默认路由可以这样设置:
  route add default gw 112.65.20.1
#可以删除默认路由,用此方法改变后几分钟就可以生效
  route del default gw 112.65.20.1

本文出自 “滴水穿石孙杰” 博客,请务必保留此出处http://xjsunjie.blog.51cto.com/999372/1604759