平常工做環境中,咱們習慣於使用ping
去測試網絡的連通性。若是ping
不通,咱們每每會去懷疑是否是路由配置錯了。
咱們知道,IP
地址是網絡世界裏的門牌號。你能夠經過IP
地址訪問遠在天邊的網站,那麼數據是如何到達網站的呢?靠的就是路徑上每一個節點的路由。
路由,簡單的說就是指導IP報文該去哪的指示牌。ubuntu
通常說來,主機會在如下兩個時機進行路由查詢服務器
注意,轉發須要開啓 net/ipv4/ip_forward
以一個典型的主機爲例,tristan
有一個外部網卡eth0
和一個內部還回網卡lo
網絡
[root@tristan]# ifconfig eth0 Link encap:Ethernet HWaddr 00:80:C8:F8:4A:51 inet addr:192.168.99.35 Bcast:192.168.99.255 Mask:255.255.255.0 UP BROADCAST RUNNING MULTICAST MTU:1500 Metric:1 RX packets:27849718 errors:1 dropped:0 overruns:0 frame:0 TX packets:29968044 errors:5 dropped:0 overruns:2 carrier:3 collisions:0 txqueuelen:100 RX bytes:943447653 (899.7 Mb) TX bytes:2599122310 (2478.7 Mb) Interrupt:9 Base address:0x1000 lo Link encap:Local Loopback inet addr:127.0.0.1 Mask:255.0.0.0 UP LOOPBACK RUNNING MTU:16436 Metric:1 RX packets:7028982 errors:0 dropped:0 overruns:0 frame:0 TX packets:7028982 errors:0 dropped:0 overruns:0 carrier:0 collisions:0 txqueuelen:0 RX bytes:1206918001 (1151.0 Mb) TX bytes:1206918001 (1151.0 Mb) [root@tristan]# route -n Kernel IP routing table Destination Gateway Genmask Flags Metric Ref Use Iface 192.168.99.0 0.0.0.0 255.255.255.0 U 0 0 0 eth0 127.0.0.0 0.0.0.0 255.0.0.0 U 0 0 0 lo 0.0.0.0 192.168.99.254 0.0.0.0 UG 0 0 0 eth0
經過route -n
咱們能夠看到主機上簡要的路由表信息(固然經過ip route
也能夠),那麼上面的路由信息中的每一表項表明什麼意思呢?負載均衡
192.168.99.0/24
這個網段,那麼它應該從eth0
進行轉發。127.0.0.1/8
這個網段,那麼它應該從lo
進行轉發。eth0
轉發,下一跳IP地址是192.168.99.254
第1,2條應該都很好理解,比較難以理解的是第3條,爲何它的Gateway
不是全0.0.0.0
, 這須要從網絡拓撲提及oop
上圖左邊是一幅典型的網絡拓撲。主機之間經過交換機組成一個小型局域網,再經過一個路由器鏈接到更大的網絡。 測試
圖中的路由器對於局域網內的主機來講,便可稱爲Gateway
,通常翻譯爲網關
。他的地位如同一個國家的海關
,出了海關
,IP
報文就在另外一個網絡之中了。網站
回到前面的路由表的第3條,當主機發現報文的目的IP地址不知足其餘表項時,就表示這個報文須要送到局域網外部,因此它會將報文發送或者轉發給網關,也就是192.168.99.254
。這種狀況下,spa
192.168.99.254
被稱爲默認網關
,這條表項也稱爲默認路由
。與這個名字對應的,第1,2條路由表項也被稱爲網段路由
翻譯
當咱們爲網卡配置IP
地址時或者由DHCP
服務器分配IP
地址,Linux
內核會根據IP
地址和掩碼自動生成對應的網段路由。這至關於告訴系統,這個網段的主機在同一個局域網內部code
舉個例子,假設咱們在tristan
主機去ping
局域網內的susan
主機。那麼這個ICMP request
報文向下面這樣組裝就OK了。
若是 tristan
還不知道susan
主機上eth0
的MAC
地址,那麼它首先要發送ARP
廣播報文去得到。
與局域網外的主機通訊就須要經過(via
)網關了
類似的例子,假設咱們在tristan
主機去ping
局域網外的paul
主機的eth0
, 在查詢路由後,內核選擇默認網關,那麼組裝的ICMP request
的報文頭將是下面這樣:
有時,系統生成的默認路由並不能知足咱們的要求,咱們須要手動設置路由
[root@tristan]# route -n Kernel IP routing table Destination Gateway Genmask Flags Metric Ref Use Iface 192.168.99.0 0.0.0.0 255.255.255.0 U 0 0 0 eth0 127.0.0.0 0.0.0.0 255.0.0.0 U 0 0 0 lo 0.0.0.0 192.168.99.254 0.0.0.0 UG 0 0 0 eth0
好比剛纔的拓撲,tristan
主機要與jerry
主機所在的網絡通訊,顯然根據現有的路由表,顯然是不行的。所以咱們須要在tristan
主機上配置額外的路由表項:
[root@tristan]# route add -net 192.168.98.0 netmask 255.255.255.0 gw 192.168.99.1 [root@tristan]# route -n Kernel IP routing table Destination Gateway Genmask Flags Metric Ref Use Iface 192.168.99.0 0.0.0.0 255.255.255.0 U 0 0 0 eth0 192.168.98.0 192.168.99.1 255.255.255.0 UG 0 0 0 eth0 127.0.0.0 0.0.0.0 255.0.0.0 U 0 0 0 lo 0.0.0.0 192.168.99.254 0.0.0.0 UG 0 0 0 eth0
新添加的路由表項表示本機與192.168.98.0/24
範圍內的主機主機通訊,須要途徑網關192.168.99.1
使用ip route
命令也能夠達到相同的效果, 經過via
設置下一跳網關,本質上沒有區別
[root@tristan]# ip route add 192.168.98.0/24 via 192.168.99.1
咱們甚至能夠添加或者刪除默認路由
[root@tristan]# route del default gw 192.168.99.254 [root@tristan]# route add default gw 192.168.99.1
除了設置整個網段,咱們還能夠僅爲某個目的IP設置路由,即主機路由
,主機路由實際上這是掩碼爲255.255.255.255
的網段路由的特殊形式
[root@tristan]# route add -host 192.168.98.42 gw 192.168.99.1
或者
[root@tristan]# route add -net 192.168.98.42 netmask 255.255.255.255 gw 192.168.99.1
均可以生成一條特定主機的路由,Flags
字段中的H
表示Host
[root@tristan]# route -n Kernel IP routing table Destination Gateway Genmask Flags Metric Ref Use Iface 192.168.99.0 0.0.0.0 255.255.255.0 U 0 0 0 eth0 192.168.98.42 192.168.99.1 255.255.255.255 UGH 0 0 0 eth0 127.0.0.0 0.0.0.0 255.0.0.0 U 0 0 0 lo 0.0.0.0 192.168.99.254 0.0.0.0 UG 0 0 0 eth0
等價路由(ECMP
)是指存在多條鏈路到達同一目的,鏈路之間能夠起到負載均衡和鏈路備份的目的,好比下面的拓撲:
在主機melo
設置的路由以下
[root@melo]# route show scope global default via 203.0.113.5 dev eth0 192.0.2.0/25 nexthop via 203.0.113.7 dev eth1 weight 1 nexthop via 203.0.113.9 dev eth2 weight 1
表示若是報文目的地址是192.0.2.0/25
,那麼它將等可能地選擇out1
或者out2
做爲出接口。
上面提到的全部路由選擇都是根據目的IP找到對應的路由表項進行的。除此以外,Linux
還提供了一種更高級靈活的方式能夠完成更加複雜的路由選擇策略,這就是策略路由
。它使得用戶能夠基於源IP等信息進行路由配置。
策略路由的基本原理是系統會根據IP報文的特徵使用不一樣的路由表。它須要在內核編譯時勾選CONFIG_IP_MULTIPLE_TABLES
Linux
內核最多支持256張路由表,其中有4張是系統默認保留的,用戶能夠新建252張表
從/etc/iproute2/rt_tables
能夠看到內核保留的4張表,其中有用的是local
表和main
表
root@ubuntu-1:/home/user1# cat /etc/iproute2/rt_tables # # reserved values # 255 local 254 main 253 default 0 unspec
local
表中存儲的是內核自動生成本機路由
和廣播路由
。
當咱們使用 route命令或者不指定路由表時的 ip route時,操做的表是main
表,因此咱們須要使用額外的參數才能操做和查看local
表
[root@real-server]# ip address show dev eth1 6: eth1: <BROADCAST,MULTICAST,UP> mtu 1500 qdisc pfifo_fast qlen 100 link/ether 00:80:c8:e8:1e:fc brd ff:ff:ff:ff:ff:ff inet 10.10.20.89/24 brd 10.10.20.255 scope global eth1 [root@real-server]# ip route show dev eth1 10.10.20.0/24 proto kernel scope link src 10.10.20.89 [root@real-server]# ip route show dev eth1 table local broadcast 10.10.20.0 proto kernel scope link src 10.10.20.89 broadcast 10.10.20.255 proto kernel scope link src 10.10.20.89 local 10.10.20.89 proto kernel scope host src 10.10.20.89 [root@real-server]# ip address add 192.168.254.254/24 brd+ dev eth1 [root@real-server]# ip address show dev eth1 6: eth1: <BROADCAST,MULTICAST,UP> mtu 1500 qdisc pfifo_fast qlen 100 link/ether 00:80:c8:e8:1e:fc brd ff:ff:ff:ff:ff:ff inet 10.10.20.89/24 brd 10.10.20.255 scope global eth1 inet 192.168.254.254/24 brd 192.168.254.255 scope global eth1 [root@real-server]# ip route show dev eth1 10.10.20.0/24 proto kernel scope link src 10.10.20.89 192.168.254.0/24 proto kernel scope link src 192.168.254.254 [root@real-server]# ip route show dev eth1 table local broadcast 10.10.20.0 proto kernel scope link src 10.10.20.89 broadcast 192.168.254.0 proto kernel scope link src 192.168.254.254 broadcast 10.10.20.255 proto kernel scope link src 10.10.20.89 local 192.168.254.254 proto kernel scope host src 192.168.254.254 local 10.10.20.89 proto kernel scope host src 10.10.20.89 broadcast 192.168.254.255 proto kernel scope link src 192.168.254.254
通常狀況下,咱們不該該也不必操做local
表,雖然內核並無禁止用戶對local
表的操做
ip route add table local local 10.10.20.64 dev eth0 proto kernel scope host src 10.10.20.67 ip route add table local local 192.168.43.12 dev eth4 proto kernel scope host src 192.168.43.14
使用ip rule
命令,咱們能夠看到系統選擇路由表的規則
[root@real-server]# ip rule show 0: from all lookup local 32766: from all lookup main 32767: from all lookup default
第一項表示默認查找的優先級,數字越小的優先級越高,因此係統默認會從local
表進行查找。規則中的from all
表示全部源地址。
咱們能夠ip rule
命令添加自定義的規則,
ip rule add from 192.168.101.0/24 table 10 ip rule add to 192.168.102.0/24 table 20 ip rule add to 192.168.103.0/24 table 30 prio 123
上面的規則表示來自192.168.101.0/24
網段的報文都查詢table 10
,目的地址爲192.168.102.0/24
網段的報文查詢table 20
,目的地址爲192.168.103.0/24
網段的報文查詢table 30
且優先級爲123
[root@real-server]# ip rule show 0: from all lookup local 123: from all to 192.168.103.0/24 lookup 30 32764: from all to 192.168.102.0/24 lookup 20 32765: from 192.168.101.0/24 lookup 10 32766: from all lookup main 32767: from all lookup default
以後,用戶就能夠經過ip route
命令向具體的table
添加路由表項了。
(完)