温馨提示×

温馨提示×

您好,登录后才能下订单哦!

密码登录×
登录注册×
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》

rhel6.5中双网卡双网关如何配置

发布时间:2021-11-16 16:51:52 来源:亿速云 阅读:121 作者:小新 栏目:云计算

这篇文章主要为大家展示了“rhel6.5中双网卡双网关如何配置”,内容简而易懂,条理清晰,希望能够帮助大家解决疑惑,下面让小编带领大家一起研究并学习一下“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、配置网卡信息

vi /etc/sysconfig/network-scripts/ifcfg-eth22

DEVICE=eth22

HWADDR=00:90:FA:76:A5:BC

TYPE=Ethernet

UUID=ebd54026-4412-4cc3-9f74-e065d4328072

ONBOOT=yes

NM_CONTROLLED=no

BOOTPROTO=static

IPADDR=114.80.10.79

NETMASK=255.255.255.128

vi /etc/sysconfig/network-scripts/ifcfg-eth24

DEVICE=eth24

HWADDR=00:90:FA:76:A5:98

TYPE=Ethernet

UUID=ebd54026-4412-4cc3-9f74-e065d4328479

ONBOOT=yes

NM_CONTROLLED=no

BOOTPROTO=static

IPADDR=112.65.20.23

NETMASK=255.255.255.128

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

[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 eth24

114.80.10.0     0.0.0.0         255.255.255.128   U     0      0        0 eth22

112.65.20.0     0.0.0.0         255.255.255.0     U     0      0        0 eth24

114.80.10.0     0.0.0.0         255.255.255.0     U     0      0        0 eth22

169.254.0.0     0.0.0.0         255.255.0.0       U     1016   0        0 eth24

169.254.0.0     0.0.0.0         255.255.0.0       U     1016   0        0 eth22

0.0.0.0         112.65.20.1     255.255.255.128    UG    0      0        0 eth24

2、修改rc.local

可以直接增加这两条路由,但是重启后会丢失。

route add -net 114.80.10.0/25 gw 114.80.10.1 dev eth22

route add -net 112.65.20.0/25 gw 112.65.20.1 dev eth24

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

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 eth22

route add -net 112.65.20.0/25 gw 112.65.20.1 dev eth24

然后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 eth24

114.80.10.0     114.80.10.1     255.255.255.128   UG     0      0       0 eth22

112.65.20.0     0.0.0.0         255.255.255.0     U     0      0        0 eth24

114.80.10.0     0.0.0.0         255.255.255.0     U     0      0        0 eth22

169.254.0.0     0.0.0.0         255.255.0.0       U     1016   0        0 eth24

169.254.0.0     0.0.0.0         255.255.0.0       U     1016   0        0 eth22

(如果只需要添加默认路由可以这样设置:
  route add default gw 112.65.20.1
  route del default gw 112.65.20.1 (可以删除默认路由,用此方法改变后几分钟就可以生效.)

还有另外一种方法就是通过策略性路由配置iproute2工具包来实现。这个软件包是由Alexey Kuznetsov开发的,软件包所在的主要网址为ftp://ftp.inr.ac.ru/ip-routing/。

1.增加2个路由表分别是电信:tel 联通:cnc 

# vi /etc/iproute2/rt_tables 
252 tel 
251 cnc

保存并推出

2.增加路由规则 
# ip route flush table tel 
# ip route add default via 114.80.10.1 dev eth22 src 114.80.10.4 table tel 
# ip ruleadd from 114.80.10.4 table tel

此处是设置电信的网关,并可实现让电信的资源访问只从eth22网卡出去

# ip route flush table cnc 
# ip route add default via 112.65.20.1 dev eth24 src 112.65.20.2 table cnc 
# ip rule add from 112.65.20.2 table cnc

此处是设置联通的网关,并可实现让联通的资源访问只从eth24网卡出去

3.配置network启动脚本文件 在结尾之前增加如下内容

# vi /etc/rc.d/init.d/network

ip route flush table tel 
ip route add default via 114.80.10.1 dev eth22 src 114.80.10.4 table tel 
ip rule add from 114.80.10.4 table tel

ip route flush table cnc 
ip route add default via 112.65.20.1 dev eth24 src 112.65.20.2 table cnc 
ip rule add from 112.65.20.2 table cnc

exit 0

5,退出并重启网络

# /etc/rc.d/init.d/network restart

此时再测试机器网络情况,就会发现电信和联通的地址都可以正常访问了。此方法还可以实现让从电信IP过来的请求按照电信路由返回,从网通IP过来的请求从网通路由返回。

6,服务器重启,上述的路由规则就失效了,所以你需要把上面这段命令写入系统启动脚本


RedHat/CentOS,系统启动脚本是/etc/rc.d/rc.local

以上是“rhel6.5中双网卡双网关如何配置”这篇文章的所有内容,感谢各位的阅读!相信大家都有了一定的了解,希望分享的内容对大家有所帮助,如果还想学习更多知识,欢迎关注亿速云行业资讯频道!

向AI问一下细节

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

AI