Linux 策略路由

2016-01-29 18:19 8 1 收藏

Linux 策略路由,Linux 策略路由

【 tulaoshi.com - Linux 】

传说 负载均衡
1.添加网卡 插入网卡,启动机器,修改/etc/modules.conf并加入 alias eth2 driver.o 2.配置adsl拨号 通过adsl-setup程序创建ppp0和ppp1的拨号配置文件,并保存配置。 修改/etc/sysconfig/network-script/ifcfg-ppp*文件,将其中的PIDFILE参数设为: PIDFILE=/var/run/ppp-adsl*.pid 其中*对应0、1等 如果不修改此参数将无法启动第二个ppp接口。 3.启动ppp接口 因为adsl-start 命令缺省只能启动第一的ppp接口。所以要启动两个接口,必须指定配置文件。 adsl-start /etc/sysconfig/network-script/ifcfg-ppp0 adsl-start /etc/sysconfig/network-script/ifcfg-ppp1 4.添加路由表 缺省情况下,系统只有3个路由表,local、main、default,路由规则为所有进入的数据报都参照main、defaul来决策路由,这可以通过ip rule ls来查看。其输出如下: 代码: [root@linuxrouter root]# ip rule ls 0: from all lookup local 32766: from all lookup main 32767: from all lookup 253 [root@linuxrouter root]# 要实现策略路由,添加额外的路由表是必须的。 下面两条命令分别添加名为ppp0和ppp1的路由表。 echo 201 ppp0 >> /etc/iproutes/rt_tables echo 202 ppp1 >> /etc/iproutes/rt_tables 5.创建路由表项 上面创建的两个路由表都是空表,需要在其中注入路由表项。 本人编写了如下脚本用于注入路由表项: 代码: #!/bin/bash # Name: cprt # This program copy the route from route table to route table, # exclude the default route entry. if [ -z "" -o -z "" ]; then echo $"usage: cprt " exit 1 fi SOURCE= DEST= # Clear the destination route table echo $"Clearing route table $DEST ......" echo /sbin/ip route flush table $DEST # Inject routes from source to destination echo $"Injecting route from $SOURCE to $DEST ......" /sbin/ip route ls table $SOURCE | grep -v default > /tmp/route-tmp while read line; do /sbin/ip route add table $DEST $line done < "/tmp/route-tmp" 把main表中的路由表项复制到ppp0和ppp1中。 将下面命令加入到/etc/rc.d/rc.local中。 cprt main ppp0 cprt main ppp1 此时,两个路由表中都有相同的路由表项了,除了default路由以外。缺省路由的添加要通过另外的途径添加。当ppp激活,或者掉线时,pppd守护进程会调用/etc/ppp/目录下的ip-up、ip-down脚本,这些教本又分别调用ip-up.local、ip-down.local。在此我们利用这两个脚本来对路由表和流量控制策略进行维护,此脚本需要人工创建。 下面时本例中的脚本: 代码: #!/bin/bash # Name: ip-up.local # Created by lyking@CU check_default_route () { LC_ALL=C ip route list table match 0/0 | grep -q default } # Determin device here # We should use IFNAME as the interface name.For some reason, the IFNAME maybe not # same as the LINKNAME. And the route table should associate with the IFNAME. For # some conveniency, I name the route table as "ppp0" and "ppp1". RT_TABLE=$IFNAME # Add or change static route here if [ ! `check_default_route $RT_TABLE` ] ; then ip route change default dev $IFNAME table $RT_TABLE else ip route add default dev $IFNAME table $RT_TABLE fi # Write some messages for later trubleshooting. echo >> /var/log/ifchang.log echo ": $IFNAME going up at `date`." >> /var/log/ifchang.log echo ": $IFNAME got address: $IPLOCAL, peer address is $IPREMOTE." >> /var/log/ifchang.log echo ": Table $RT_TABLE default route change to `ip route ls table $RT_TABLE | grep default`." >> /var/log/ifchang.log # Refresh routing cache to activating the routing immediately. ip route flush cache 代码: #!/bin/bash # Name: ip-down.local # Created by lyking@CU cd /etc/sysconfig/network-scripts . network-functions # Determin device here # We should use IFNAME as the interface name.For some reason, the IFNAME maybe not # same as the LINKNAME. And the route table should associate with the IFNAME. For # some conveniency, I name the route table as "ppp0" and "ppp1". RT_TABLE=$IFNAME PPPS="ppp0 ppp1" # Looking for a valide connection to Internet DEFAULT_RT="" for i in $PPPS ; do echo "Checking $i ..." >> /tmp/if-down.log if [ ! `is_available $i` ] ; then echo "$i is available." >> /tmp/if-down.log DEFAULT_RT=$i break fi done # Update default route here if [ $DEFAULT_RT != "" ] ; then ip route add default dev $DEFAULT_R

来源:https://www.tulaoshi.com/n/20160129/1505308.html

延伸阅读
标签: 电脑入门
电脑系统的安全离不开安全策略的设置,如果安全策略设置没有设置好的话电脑的安全就得不到保证,下面图老师小编就给大家介绍下Linux下安全策略设置的方法,一起来学习下吧。 安全第一对于linux管理界乃至计算机也都是一个首要考虑的问题。加密的安全性依赖于密码本身而非算法!而且,此处说到的安全是指数据的完整性,由此,数据的认证安全...
标签: 电脑入门
前面我们有提到,利用Linux可以变成一台很好的路由器,今天本文要介绍的是如何利用路由转发功能来实现共享上网,有兴趣的朋友一起来学习下吧。 一、Linux 系统开启IP转发功能 Linux 系统要达到路由器功能,首先得打开Linux 系统内核中的IP转发功能。我们可以通过以下命令来查看是否开启。less /proc/sys/net/ipv4/ip_forward该文件内容为0...
标签: 服务器
Linux下针对路由功能配置iptables的方法详解   作为公司上网的路由器需要实现的功能有nat地址转换、dhcp、dns缓存、流量控制、应用程序控制,nat地址转换通过iptables可以直 接实现,dhcp服务需要安装dhcpd,dns缓存功能需要使用bind,流量控制可以使用tc,应用程序控制:例如对qq的封锁可以使用 netfilter-layer7-v2.22+17-protoco...
很多读者也许都听说过Linux操作系统,知道它是和Windows抗衡的另一大流派的操作系统。但大家也许不知道,Linux极其灵活,不仅可以运行在大型服务器、台式机之上,也可以运行在各种小型终端设备,这其中就包括无线路由器。DD-WRT其实就是一个供无线路由器使用的嵌入版Linux,它可以在普通的家用无线路由器实现数千元的商用无线路由器功能,不仅如...
      和其他贩卖者的作业系统相比,Linux 必定可以被认为是安全的 -- 或是更安全。公认的, 随着 Linux 变得越来越普及, 将会变成很有吸引力的目标而吸引怪客集中精神致力於突破它。 随着一次一次的尝试,某些漏洞会被发现,然而 Linux 开放的本质往往使得这些漏洞很快...

经验教程

522

收藏

24
微博分享 QQ分享 QQ空间 手机页面 收藏网站 回到头部