Linux高級路由-路由規則


1.什么叫路由?
2.什么叫路由器?
3 路由表中的路由分為表態路由和動態路由    
 動態路由協議  rip  ospf  bgp
4.linux系統本身可以作個路由器,由內核維護了一個靜態路由表
route -n  / netstat -rn

5 內核路由判斷的依據
  規則1  子網掩碼長度越長越優先
  規則2  子網掩碼長度一樣的情況下,條目越靠前越優先
自己理解
1.路由查看路由表優先查看子網掩碼越長的越長越先查看(越精確)
2..路由查看路由表是從上往下一個一個匹配,網段一致長度一致,路由條目越上越優先
網關:網絡的出口,默認路由

6.有必要的情況下,需要人為干涉路由條目的設定

添加到網絡的路由
]#route add -net 192.168.0.0/24 netmask 255.255.255.0  dev eth0
或者簡寫# route add -net 192.168.0.0/24  dev eth0
]# route del -net 192.168.1.0/24netmask 255.255.255.0 dev eth0
或者簡寫# route add -net 192.168.1.0/24  dev eth0
添加到主機的路由
]#route add -host 192.168.1.1/32 dev eth0
]#route del  -host 192.168.1.1/32 dev eth0
或者
]# route add -host 192.168.0.188 dev eth0
]# route del -host 192.168.0.188 dev eth0
添加到默認路由 -;
]#route del default
]#route add default gw 192.168.0.5
# grep GATEWAY /etc/sysconfig/network
GATEWAY=192.168.0.5


[root@localhost ~]# ping 192.168.2.1
connect: Network is unreachable  這種信息表示沒有到達指定網絡的路由//沒有默認路由或者沒有規則
[root@localhost ~]# ping 192.168.1.5
PING 192.168.1.5 (192.168.1.5) 56(84) bytes of data.
From 192.168.1.254 icmp_seq=2 Destination Host Unreachable 這種信息味着有路由,但是目標主機可能不存在
U ---up
UG---網關
UH---主機路由

如何添加刪除路由條目:(不會永久生效)
[root@localhost ~]# route add default gw 192.168.1.254 dev eth0
[root@localhost ~]# route del default
[root@localhost ~]# route add -net 192.168.2.0 netmask 255.255.255.0  dev eth0
[root@localhost ~]# route del -net 192.168.2.0 netmask 255.255.255.0 dev eth0

[root@localhost ~]# route add -host 192.168.1.1/32 dev eth0
[root@localhost ~]# route add -host 192.168.1.2 dev eth0
[root@localhost ~]# route -n
Kernel IP routing table
Destination     Gateway         Genmask         Flags Metric Ref    Use Iface
192.168.1.1     0.0.0.0         255.255.255.255 UH    0      0        0 eth0
192.168.1.2     0.0.0.0         255.255.255.255 UH    0      0        0 eth0
192.168.1.0     0.0.0.0         255.255.255.0   U     0      0        0 eth0
169.254.0.0     0.0.0.0         255.255.0.0     U     0      0        0 eth0


   永久生效
如何配置兩個網卡的IP,對應的配置文件。
[root@localhost ~]# cd /etc/sysconfig/network-scripts/
[root@localhost network-scripts]# vim ifcfg-eth0
[root@GW1 ~]# cat /etc/sysconfig/network-scripts/ifcfg-eth0
# Realtek Semiconductor Co., Ltd. RTL-8139/8139C/8139C+
DEVICE=eth0
BOOTPROTO=none
HWADDR=00:e0:4c:43:d7:ec
ONBOOT=yes
NETMASK=255.255.255.0
IPADDR=192.168.1.2

[root@localhost network-scripts]# vim ifcfg-eth1
[root@GW1 ~]# cat /etc/sysconfig/network-scripts/ifcfg-eth1
# Realtek Semiconductor Co., Ltd. RTL8101E/RTL8102E PCI Express Fast Ethernet controller
DEVICE=eth1
BOOTPROTO=none
HWADDR=00:24:1d:9a:e6:e3
ONBOOT=yes
HOTPLUG=no
NETMASK=255.255.255.0
IPADDR=192.168.2.1
TYPE=Ethernet
]# cat  /etc/sysconfig/network
NETWORKING=yes
NETWORKING_IPV6=no
HOSTNAME=localhost.localdomain
GATEWAY=192.168.1.254
重啟網絡服務
以上就是從UNIX上繼承的功能


策略路由

內網的用戶中 不同用戶走不同的帶寬
策略路由;不同用戶用不同的路由表
系統中其實有多張路由表!默認看到的表是main
++++++實際上系統不只一張路由表,需要通過IP命令查看更多+++++++++
我們可以在查看路由條目 時,指定 表名字或者表ID
]# ip route show table default
]# ip route show table 253
默認情況下,ip route show實際上是查看名為main的表
]# ip route show table 254
]# ip route show table main
]# ip route show
]# ip route show table 254 //查看路由表可以寫名字或者ID號
192.168.2.0/24 dev eth1  proto kernel  scope link  src 192.168.2.1
192.168.1.0/24 dev eth0  proto kernel  scope link  src 192.168.1.2
169.254.0.0/16 dev eth1  scope link
]# ip route show table main
192.168.2.0/24 dev eth1  proto kernel  scope link  src 192.168.2.1
192.168.1.0/24 dev eth0  proto kernel  scope link  src 192.168.1.2
169.254.0.0/16 dev eth1  scope link
]# ip route show
192.168.2.0/24 dev eth1  proto kernel  scope link  src 192.168.2.1
192.168.1.0/24 dev eth0  proto kernel  scope link  src 192.168.1.2
169.254.0.0/16 dev eth1  scope link
以上的輸出是一致的說明平時查看的是默認表是main
]# vim /etc/iproute2/rt_tables  4-7行是有效的行
]# grep ^[^#] /etc/iproute2/rt_tables
255     local
254     main
253     default
0       unspec//不是表
-------------
二、策略路由----流量分割---- ip rule show/ip rule add
實現流量分割
1.首創建2張不同的路由表,
定義配置文件
vim /etc/iproute2/rt_tables
[root@GW1 ftp]# grep ^[^#] /etc/iproute2/rt_tables
255     local
254     main
253     default
200     table_10M
100     table_2M
0       unspec
]# ip route show table table_2M
]# ip route show table table_10M
]# ip route show table 100
]# ip route show table 200
2.分別往兩個表中各自添加對應的默認路
]# ip route add default via 1.1.1.1 dev eth0 table table_2M
RTNETLINK answers: Network is unreachable
]# ip addr add 1.1.1.11/24 dev eth0
]# ip route add default via 1.1.1.1 dev eth0 table table_2M
]# ip addr add 2.2.2.22/24 dev eth1
]# ip route add default via 2.2.2.2 dev eth1 table table_10M
----------------------
3、定義路由策略,規定從哪個源網絡來,按照 哪個表進行路由即指定一批客戶端用一張路由表,別一批客戶端用另一張路由表。
]# ip rule show //路由規則表
0:      from all lookup 255
32766:  from all lookup main
32767:  from all lookup default
數值越小優先級越高
[root@GW1 ftp]# ip rule help
Usage: ip rule [ list | add | del | flush ] SELECTOR ACTION
SELECTOR := [ from PREFIX ] [ to PREFIX ] [ tos TOS ] [ fwmark FWMARK ]
            [ dev STRING ] [ pref NUMBER ]
ACTION := [ table TABLE_ID ]
          [ prohibit | reject | unreachable ]
          [ realms [SRCREALM/]DSTREALM ]
TABLE_ID := [ local | main | default | NUMBER ]
[root@GW1 ftp]# ip rule add from 192.168.0.0/24 table table_10M
[root@GW1 ftp]# ip rule add from 192.168.1.0/24 table table_2M
[root@GW1 ftp]# ip rule show
0:      from all lookup 255
32764:  from 192.168.1.0/24 lookup table_2M
32765:  from 192.168.0.0/24 lookup table_10M
32766:  from all lookup main
32767:  from all lookup default
后添加的優先級越高
_如果想刪除路由策略,通過如下 方式____
]# ip rule del from 192.168.1.0/24
]# ip rule del from 192.168.0.0/24
]# ip rule show
0:      from all lookup 255
32766:  from all lookup main
32767:  from all lookup default
如果有跟多的匹配想描述想通過IPTANLE標志打標記。然后讓IP工具按照標記匹配
ttl值為64 按照 10M走
ttl值為128 按照 2M走
]# iptables -t mangle -A PREROUTING -m ttl --ttl-eq 64 -j MARK --set-mark 64
]# iptables -t mangle -A PREROUTING -m ttl --ttl-eq 128 -j MARK --set-mark 128
]# ip rule add fwmark 64 table table_10m
]# ip rule add fwmark 128 table table_2m
iptables -t mangle -A PREROUTING -m range --src-range 192.168.0.1-192.168.0.254 -j MARK --set-mark 190
iptables -t mangle -A PREROUTING -m range --src-range 192.168.1.1-192.168.1.254 -j MARK --set-mark 191
ip rule add  fwmark 190 table table_10m
ip rule add fwmark 191 table table_2m

雙線備用

監控腳本模型,借助任務計划
vim double.sh
#!/bin/bash
GW1_2M=2.2.2.254
GW2_10M=1.1.1.254
IF1=eth0
IF2=eth1

while :
do
          ip route del default //刪除默認路由
          ip route add default via $GW2_10M dev $IF2=eth1 //添加默認路由規則
          while ping -c 1 -W 1$GW2_10M &> /dev/null
     do
               sleep 1
     done
                ip route del default
                ip route add default via $ GW1_2M dev $IF1=eth0
                until ping -c 1 -W 1   $GW2_10M &> /dev/null                 //條件成立退出循環
                   do
                           sleep 1
                   done
done


目標:實現主機1 -- 192.168.1.11可以跟主機2 -- 192.168.4.11 通訊
學習如何在路由器上配置路由條
學習如何在主機上配置默認路由
GW1
[root@GW1 ~]# route -n
Kernel IP routing table
Destination     Gateway         Genmask         Flags Metric Ref    Use Iface
192.168.2.0     0.0.0.0         255.255.255.0   U     0      0        0 eth1
192.168.1.0     0.0.0.0         255.255.255.0   U     0      0        0 eth0

[root@GW1 ~]# cat /proc/sys/net/ipv4/ip_forward
1

[root@GW1 ~]# route add -net 192.168.4.0/24 dev eth1
[root@GW1 ~]# route add -net 192.168.3.0/24 dev eth1
[root@GW1 ~]# route -n
Kernel IP routing table
Destination     Gateway         Genmask         Flags Metric Ref    Use Iface
192.168.4.0     0.0.0.0         255.255.255.0   U     0      0        0 eth1
192.168.3.0     0.0.0.0         255.255.255.0   U     0      0        0 eth1
192.168.2.0     0.0.0.0         255.255.255.0   U     0      0        0 eth1
192.168.1.0     0.0.0.0         255.255.255.0   U     0      0        0 eth0
GW2
[root@GW2 ~]# route -n
Kernel IP routing table
Destination     Gateway         Genmask         Flags Metric Ref    Use Iface
192.168.3.0     0.0.0.0         255.255.255.0   U     0      0        0 eth1
192.168.2.0     0.0.0.0         255.255.255.0   U     0      0        0 eth0
[root@GW2 ~]# cat /proc/sys/net/ipv4/ip_forward
1


[root@GW2 ~]# route add -net 192.168.4.0/24  dev eth1
[root@GW2 ~]# route add -net 192.168.1.0/24 dev eth0
[root@GW2 ~]# route -n
Kernel IP routing table
Destination     Gateway         Genmask         Flags Metric Ref    Use Iface
192.168.4.0     0.0.0.0         255.255.255.0   U     0      0        0 eth1
192.168.3.0     0.0.0.0         255.255.255.0   U     0      0        0 eth1
192.168.2.0     0.0.0.0         255.255.255.0   U     0      0        0 eth0
192.168.1.0     0.0.0.0         255.255.255.0   U     0      0        0 eth0
GW3
[root@GW3 ~]# route -n
Kernel IP routing table
Destination     Gateway         Genmask         Flags Metric Ref    Use Iface
192.168.4.0     0.0.0.0         255.255.255.0   U     0      0        0 eth1
192.168.3.0     0.0.0.0         255.255.255.0   U     0      0        0 eth0

[root@GW3 ~]# cat /proc/sys/net/ipv4/ip_forward
1

[root@GW3 ~]# route add -net 192.168.1.0/24 dev eth0
[root@GW3 ~]# route add -net 192.168.2.0/24 dev eth0
[root@GW3 ~]# route -n
Kernel IP routing table
Destination     Gateway         Genmask         Flags Metric Ref    Use Iface
192.168.4.0     0.0.0.0         255.255.255.0   U     0      0        0 eth1
192.168.3.0     0.0.0.0         255.255.255.0   U     0      0        0 eth0
192.168.2.0     0.0.0.0         255.255.255.0   U     0      0        0 eth0
192.168.1.0     0.0.0.0         255.255.255.0   U     0      0        0 eth0

主機1 -- 192.168.1.11
[root@11 ~]# route add default gw 192.168.1.2 dev eth0
[root@11 ~]# route -n
Kernel IP routing table
Destination     Gateway         Genmask         Flags Metric Ref    Use Iface
192.168.1.0     0.0.0.0         255.255.255.0   U     0      0        0 eth0
169.254.0.0     0.0.0.0         255.255.0.0     U     0      0        0 eth0
0.0.0.0         192.168.1.2     0.0.0.0         UG    0      0        0 eth0

主機2 -- 192.168.4.11
[root@11 ~]# route add default gw 192.168.4.1 dev eth0
[root@11 ~]# route -n
Kernel IP routing table
Destination     Gateway         Genmask         Flags Metric Ref    Use Iface
192.168.4.0     0.0.0.0         255.255.255.0   U     0      0        0 eth0
169.254.0.0     0.0.0.0         255.255.0.0     U     0      0        0 eth0
0.0.0.0         192.168.4.1     0.0.0.0         UG    0      0        0 eth0

ECMP(負載均衡)

內核等值多路功能------帶寬並用 ecmp
應用場合:內核等值多路功能
負載均衡
第二個問題是如何對於通過兩個 ISP 流出的數據進行負載均衡。
與選擇兩個 ISP 中的一個作為缺省路由不同,這次是設置缺省路由為多路路由。
在缺省內核中,這會均衡兩個 ISP 的路由。象下面這樣做(基於前面的流量分割實驗):
  ip route add default  nexthop(下一條) via $P1 dev $IF1 \
                                    nexthop via $P2 dev $IF2
這樣就可以均衡兩個 ISP 的路由。通過調整“weight”參數我們可以指定其中一個 ISP 的優先權高於另一個。
應該指出,由於均衡是基於路由進行的,而路由是經過緩沖的,所以這樣的均衡並不是 100%精確。 也就是說對於一個經常訪問的站點,總是會使用同一個 ISP。進而,
如果你對此不滿意,
你可能需要參考以下 Julian Anastasov 的內核補丁:
http://www.linuxvirtualserver.org/~julian/#routes
Julian 的路由補丁會彌補上述缺陷。
ip route add default via 1.1.1.254 dev eth1
]# ip route show
]# ip route del default
]# ip route add default    nexthop via 1.1.1.254 dev eth0    nexthop via 2.2.2.254 dev eth1
]# ip route show
192.168.2.0/24 dev eth1  proto kernel  scope link  src 192.168.2.1
192.168.1.0/24 dev eth0  proto kernel  scope link  src 192.168.1.2
169.254.0.0/16 dev eth1  scope link
default
        nexthop via 192.168.1.254  dev eth0 weight 1
        nexthop via 192.168.2.254  dev eth1 weight 1

cd /usr/src/kernels/2.6.18-194.el5-i686
make menuconfig
ecmp

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM