现在的位置: 首页 > linux > 正文
ubuntu 双线双网卡双IP实现方式
2012年08月29日 linux ⁄ 共 1684字 暂无评论

ubuntu双网卡双IP.不同网关.不同子网.如何同时ping通两块网卡的解决方法,

服务器环境如下:、

系统:ubuntu9.04 X64 server

电信IP(TEL):114.80.227.34 netmask 255.255.255.128 gateway 114.80.227.33

联通IP(CNC):112.65.227.2 netmask 255.255.255.0 gateway 112.65.227.1

1.配置网卡信息

# vi /etc/network/interfaces
auto lo
iface lo inet loopback

# The primary network interface
auto eth0
iface eth0 inet static
    address 114.80.227.34
    netmask 255.255.255.128
    gateway 114.80.227.33
# dns-* options are implemented by the resolvconf package, if installed
dns-nameservers 202.96.209.133

auto eth1
iface eth1 inet static
    address 112.65.227.2
    netmask 255.255.255.0

保存配置信息并重新启动网卡

# /etc/init.d/networking restart

2.增加2个路由表分别是电信:tel  联通:cnc
# vi /etc/iproute2/rt_tables
    252 tel
    251 cnc

保存并推出

3.增加路由规则
#  ip route flush table tel
#  ip route add default via 114.80.227.33 dev eth0 src 114.80.227.34 table tel
#  ip ruleadd from 114.80.227.34 table tel

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

#   ip route flush table cnc
#   ip route add default via 112.65.227.1 dev eth1 src 112.65.227.2 table cnc
#   ip rule add from 112.65.227.2 table cnc

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

4.配置networking启动脚本文件 在结尾exit 0之前增加如下内容

# vi /etc/init.d/networking
ip route flush table tel
ip route add default via 114.80.227.33 dev eth0 src 114.80.227.34 table tel
ip rule add from 114.80.227.34 table tel

ip route flush table cnc
ip route add default via 112.65.227.1 dev eth1 src 112.65.227.2 table cnc
ip rule add from 112.65.227.2 table cnc

exit 0

5,退出并重启网络

# /etc/init.d/networking restart

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

补充:网上有些大神说如果服务器重启,或者网络服务重启,上述的路由规则就失效了,所以你需要把上面这段命令写入系统启动脚本和网络启动脚本

如果是ubuntu/debian,系统启动脚本是/etc/rc.local
如果是RedHat/centos,系统启动脚本是/etc/rc.d/rc.local
如果是ubuntu/debian,网络启动脚本是/etc/init.d/networking
如果是RedHat/centos,网络启动脚本是/etc/rc.d/init.d/network

对于系统启动脚本本次并没有做更改只是更改了网络启动脚本,有兴趣的可以测试下

给我留言

您必须 [ 登录 ] 才能发表留言!

×