linux 路由表功能解析
本文,簡單描述一下路由的作用.簡單來說,路由器就是一個公交月台,公交月台上面有很多路公交.每條線路公交車又有很多月台(路由器),你就會選擇最近的一條公交出行方案,當然啦,站牌上面的路線是會經常更新,更新的事,有當地部門更新.這就是路由.一台電腦主機有路由表,路由器也有路由表,一般來說,一台主機電腦如果只有一個網卡的話,應該會有最少兩條路由資訊,一條是公網路由,一條是區域網路路由(它是不會經過路由器的路由表的,因為它走的是鏈路層,所以可以解析我們在區域網路內傳資料,速度是很快的).
測試環境:
主機(linux): ip:192.168.168.1.119 netmask:255.255.0.0
另一台主機(xp): ip:192.168.1.133 netmask:255.255.0.0
網關: ip:192.168.0.3 netmask: 255.255.248.0
一, 我們可以查看一下路由表.(route 命令)
[user:dvr_linux] route
Kernel IP routing table
Destination Gateway Genmask Flags Metric Ref Use Iface
default 192.168.0.3 0.0.0.0 UG 0 0 0 eth0 //公網路由資訊 //U – Up, G – Gateway, H – Host
192.168.0.0 * 255.255.0.0 U 0 0 0 eth0 //區域網路路由資訊.
[user:dvr_linux]
Flags 可能出現的標誌有:
U (route is up) 路由正常
H (target is a host) 主機路由
G (use gateway) 使用網關的間接路由
R (reinstate route for dynamic routing) 為動態選路恢複路由
D (dynamically installed by daemon or redirect) 該路由由選路進程或重新導向動態建立
M (modified from routing daemon or rederict) 該路由已由選路進程或重新導向修改
! (reject route) 阻塞路由
1.1 測試公網路由(切換到root使用者下):
[root:dvr_linux] route //列印路由表
Kernel IP routing table
Destination Gateway Genmask Flags Metric Ref Use Iface
default 192.168.0.3 0.0.0.0 UG 0 0 0 eth0
192.168.0.0 * 255.255.0.0 U 0 0 0 eth0
[root:dvr_linux] ping 8.8.8.8 // 8.8.8.8是google公司的dns.
PING 8.8.8.8 (8.8.8.8) 56(84) bytes of data.
64 bytes from 8.8.8.8: icmp_req=2 ttl=48 time=10.4 ms
64 bytes from 8.8.8.8: icmp_req=3 ttl=48 time=10.0 ms //正常
^C
--- 8.8.8.8 ping statistics ---
3 packets transmitted, 2 received, 33% packet loss, time 2013ms
rtt min/avg/max/mdev = 10.024/10.222/10.421/0.222 ms
[root:dvr_linux] route del default dev eth0 //刪除預設路由,也就公網路由,因為有網關.
[root:dvr_linux] route
Kernel IP routing table
Destination Gateway Genmask Flags Metric Ref Use Iface
192.168.0.0 * 255.255.0.0 U 0 0 0 eth0
[root:dvr_linux] ping 8.8.8.8
connect: Network is unreachable //ping 不通,沒找到路由資訊.資料包出不去.
[root:dvr_linux] route add default dev eth0 //增加預設路由,沒加網關.
[root:dvr_linux] route
Kernel IP routing table
Destination Gateway Genmask Flags Metric Ref Use Iface
default * 0.0.0.0 U 0 0 0 eth0
192.168.0.0 * 255.255.0.0 U 0 0 0 eth0
[root:dvr_linux] ping 8.8.8.8
PING 8.8.8.8 (8.8.8.8) 56(84) bytes of data.
From 192.168.1.119 icmp_seq=1 Destination Host Unreachable //ping 不通,找到路由資訊.但是找不到去8.8.8.8胡線路,資料包出不去.
From 192.168.1.119 icmp_seq=2 Destination Host Unreachable
From 192.168.1.119 icmp_seq=3 Destination Host Unreachable
^C
--- 8.8.8.8 ping statistics ---
5 packets transmitted, 0 received, +3 errors, 100% packet loss, time 4010ms
pipe 3
[root:dvr_linux] route del default dev eth0
[root:dvr_linux] route
Kernel IP routing table
Destination Gateway Genmask Flags Metric Ref Use Iface
192.168.0.0 * 255.255.0.0 U 0 0 0 eth0
[root:dvr_linux] route add default gw 192.168.0.3 dev eth0
[root:dvr_linux] route
Kernel IP routing table
Destination Gateway Genmask Flags Metric Ref Use Iface
default 192.168.0.3 0.0.0.0 UG 0 0 0 eth0
192.168.0.0 * 255.255.0.0 U 0 0 0 eth0
[root:dvr_linux] ping 8.8.8.8
PING 8.8.8.8 (8.8.8.8) 56(84) bytes of data.
64 bytes from 8.8.8.8: icmp_req=1 ttl=48 time=11.0 ms //重新添加帶網關的路由資訊後,又能ping 通了.
64 bytes from 8.8.8.8: icmp_req=3 ttl=48 time=10.0 ms
64 bytes from 8.8.8.8: icmp_req=4 ttl=48 time=10.3 ms
^C
--- 8.8.8.8 ping statistics ---
4 packets transmitted, 3 received, 25% packet loss, time 3015ms
rtt min/avg/max/mdev = 10.037/10.496/11.071/0.445 ms
[root:dvr_linux]
1.2 測試區域網路路由(切換到root使用者下):
[root:dvr_linux] route
Kernel IP routing table
Destination Gateway Genmask Flags Metric Ref Use Iface
default 192.168.0.3 0.0.0.0 UG 0 0 0 eth0
192.168.0.0 * 255.255.0.0 U 0 0 0 eth0
[root:dvr_linux] ping 192.168.1.133
PING 192.168.1.133 (192.168.1.133) 56(84) bytes of data.
64 bytes from 192.168.1.133: icmp_req=1 ttl=64 time=2.13 ms
64 bytes from 192.168.1.133: icmp_req=2 ttl=64 time=0.450 ms //正常通過.
^C
--- 192.168.1.133 ping statistics ---
2 packets transmitted, 2 received, 0% packet loss, time 1003ms
rtt min/avg/max/mdev = 0.450/1.290/2.131/0.841 ms
[root:dvr_linux] route del -net 192.168.0.0/16 dev eth0 //刪除區域網路路由
[root:dvr_linux] route
Kernel IP routing table
Destination Gateway Genmask Flags Metric Ref Use Iface
default 192.168.0.3 0.0.0.0 UG 0 0 0 eth0
[root:dvr_linux] ping 192.168.1.133
PING 192.168.1.133 (192.168.1.133) 56(84) bytes of data.
64 bytes from 192.168.1.133: icmp_req=1 ttl=64 time=5.06 ms
From 192.168.0.3: icmp_seq=1 Redirect Host(New nexthop: 192.168.1.133) //資料包通過了預設路由資訊發到了路由器,再查路由器的路由表找到了192.168.1.133的路由.再由網關發回來給192.168.1.133了.
64 bytes from 192.168.1.133: icmp_req=2 ttl=64 time=0.477 ms
64 bytes from 192.168.1.133: icmp_req=3 ttl=64 time=0.302 ms
64 bytes from 192.168.1.133: icmp_req=4 ttl=64 time=0.285 ms
64 bytes from 192.168.1.133: icmp_req=5 ttl=64 time=0.478 ms
64 bytes from 192.168.1.133: icmp_req=6 ttl=64 time=0.408 ms
^C
--- 192.168.1.133 ping statistics ---
6 packets transmitted, 6 received, 0% packet loss, time 5002ms
rtt min/avg/max/mdev = 0.285/1.169/5.066/1.744 ms
[root:dvr_linux] route
Kernel IP routing table
Destination Gateway Genmask Flags Metric Ref Use Iface
default 192.168.0.3 0.0.0.0 UG 0 0 0 eth0
[root:dvr_linux] route del default eth0 //把預設路由都刪掉的話,這就完全找不到路由資訊了,也就是沒網路了.
[root:dvr_linux] route
Kernel IP routing table
Destination Gateway Genmask Flags Metric Ref Use Iface
[root:dvr_linux] ping 192.168.1.133
connect: Network is unreachable
[root:dvr_linux]
在這個時候,192.168.1.133的xp主機去ping 192.168.1.119的話.也就ping 不通的.
C:\Documents and Settings\Administrator>ping 192.168.1.119 -t
Pinging 192.168.1.119 with 32 bytes of data:
Request timed out.
Request timed out.
Request timed out.
Request timed out.
Request timed out.
Request timed out.
Request timed out.
Request timed out.
Request timed out.
Request timed out.
Request timed out.
Request timed out.
Request timed out.
Request timed out.
Request timed out.
Request timed out.
[root:dvr_linux] service networking restart //重啟一下網路.
[warn] Running /etc/init.d/networking restart is deprecated because it may not re-enable some interfaces ... (warning).
[....] Reconfiguring network interfaces...RTNETLINK answers: No such process
done.
[root:dvr_linux]
[root:dvr_linux] route
Kernel IP routing table
Destination Gateway Genmask Flags Metric Ref Use Iface
default 192.168.0.3 0.0.0.0 UG 0 0 0 eth0
192.168.0.0 * 255.255.0.0 U 0 0 0 eth0
[root:dvr_linux]
在這個時候,192.168.1.133的xp主機去ping 192.168.1.119的話.網路恢複了.
C:\Documents and Settings\Administrator>ping 192.168.1.119 -t
Pinging 192.168.1.119 with 32 bytes of data:
Request timed out.
Request timed out.
Request timed out.
Request timed out.
Request timed out.
Reply from 192.168.1.119: bytes=32 time=2ms TTL=64
Reply from 192.168.1.119: bytes=32 time<1ms TTL=64
Reply from 192.168.1.119: bytes=32 time<1ms TTL=64
Reply from 192.168.1.119: bytes=32 time<1ms TTL=64
Reply from 192.168.1.119: bytes=32 time<1ms TTL=64
Reply from 192.168.1.119: bytes=32 time<1ms TTL=64
Reply from 192.168.1.119: bytes=32 time=6ms TTL=63
Reply from 192.168.1.119: bytes=32 time<1ms TTL=64
Reply from 192.168.1.119: bytes=32 time<1ms TTL=64
Reply from 192.168.1.119: bytes=32 time<1ms TTL=64