Linux命令學習手冊-route命令

route [-CFvnee]html

route [-v]  [-A family] add [-net|-host] target [netmask Nm] [gw Gw] [metric N] [mss M] [window W] [irtt I] [reject] [mod] [dyn] [reinstate] [[dev] If]網絡

route [-v] [-A family] del [-net|-host] target [gw Gw] [netmask Nm] [metric N] [[dev] If]oop

route [-V] [--version] [-h] [--help]測試

 

[功能]ui

設定和操做路由器表。spa

 

 

[描述].net

route用來操做管理內核的IP路由表。它的做用是在使用ifconfig命令配置好網絡接口以後,經過網絡接口設定到特定主機或者網絡的靜態路由表。unix

當使用add或者del選項的時候,route修改路由表。若是沒有這些選項,那麼route會顯示當前路由表中的內容。htm

 

經常使用項:blog

-n:不使用主機名稱而直接使用主機IP或端口號。

-ee:使用詳細信息顯示。

 

增長(add)和刪除(del)選項的參數:

-net:表示後面所指定的是爲一個網絡。

-host:表示後面所指定的是一個主機的路由。

netmask:和網絡相關,能夠設定netmask來決定網絡大小。

gw:表示gateway(網關),後面鏈接其IP。

dev:指定鏈接的設備(網卡),例如eth0。

 

 

[舉例]

一、須要預先了解的內容:

(1)路由可爲各類品牌路由(例如cisco),PC爲訪問網絡的設備(例如主機)。

(2)路由器上面至少有兩類網口,一個可鏈接WAN,一個可鏈接PC組成LAN。

(3)PC只有一個網口,要麼鏈接WAN,要麼鏈接路由組成LAN。

(4)路由可與PC直連,與之組成LAN;路由也可再與WAN相連,使其LAN內主機可經它訪問WAN;也可PC直連到WAN。

(5)內網:又叫LAN,是路由和PC組成的內部局域網,其網內主機假設爲172.168.1.*。

(6)外網:又叫WAN,是外部一個更大的局域網,其網內主機假設爲10.1.10.*。

(7)PC內網網址(LAN):可靜態指定,也可經過路由的dhcp服務動態獲取(例如172.168.1.105)。

(8)路由內網網址(LAN):鏈接到路由的PC以它做爲默認網關,PC也可經過它登錄並配置路由(如172.168.1.14)。

(9)PC外網網址(WAN):可靜態指定,也可經過WAN網中的dhcp服務動態獲取(如10.1.10.182)。(似PC從路由)

(10)路由的外網網址(WAN):可靜態指定,也可經過WAN網中的dhcp服務獲取(如10.1.10.32)。(似PC從外網)

 

 

二、當把路由器插鏈接WLAN,而後pc鏈接路由器組成LAN以後:

pc的網址(LAN):172.168.1.105(能夠pc本身指定靜態的,也可經過路由的dhcp服務動態獲取)。

cisco的網址(LAN):172.168.1.14。

pc的網關設置成(LAN):172.168.1.14。

 

*查看PC機器ip信息:

# ifconfig

eth0      Link encap:Ethernet  HWaddr 00:24:7e:03:62:14  

          inet addr:172.168.1.105  Bcast:172.168.1.255  Mask:255.255.255.0

          inet6 addr: fe80::224:7eff:fe03:6214/64 Scope:Link

          UP BROADCAST RUNNING MULTICAST  MTU:1500  Metric:1

          RX packets:1776511 errors:12 dropped:1 overruns:0 frame:8

          TX packets:1174511 errors:0 dropped:0 overruns:0 carrier:0

          collisions:0 txqueuelen:1000 

          RX bytes:934065225 (934.0 MB)  TX bytes:107898203 (107.8 MB)

          Interrupt:16 Memory:fc500000-fc520000 

 

lo        Link encap:Local Loopback  

          inet addr:127.0.0.1  Mask:255.0.0.0

          inet6 addr: ::1/128 Scope:Host

          UP LOOPBACK RUNNING  MTU:16436  Metric:1

          RX packets:310438 errors:0 dropped:0 overruns:0 frame:0

          TX packets:310438 errors:0 dropped:0 overruns:0 carrier:0

          collisions:0 txqueuelen:0 

          RX bytes:28063379 (28.0 MB)  TX bytes:28063379 (28.0 MB)

 

*查看當前路由以下:

# route

Kernel IP routing table

Destination     Gateway         Genmask         Flags Metric Ref    Use Iface

default         172.168.1.14    0.0.0.0         UG    0      0        0 eth0

link-local      *               255.255.0.0     U     1000   0        0 eth0

172.168.1.0     *               255.255.255.0   U     1      0        0 eth0

能夠看到機器路由,其中(其餘參見man手冊):

Destination:目標網絡或者主機。

Gateway:網關地址,若是沒有設置則爲*。

Genmask:目標網絡掩碼;若是目標主機則用"255.255.255.255",若是默認路由則用"0.0.0.0"。

Flags標誌說明:

U Up表示此路由當前爲啓動狀態

H Host,表示此網關爲一主機

G Gateway,表示此網關爲一路由器

R Reinstate Route,使用動態路由從新初始化的路由

D Dynamically,此路由是動態性地寫入

M Modified,此路由是由路由守護程序或導向器動態修改

! 表示此路由當前爲關閉狀態

Metric:到目標的距離(通常爲跳數),當前kernel可能不用,可是路由守護進程可能會須要它。

Ref:此路由引用數目(當前kernel不用)。

Use:對此路由的查找。

Iface:對於這個路由,數據包將要發送到那個接口(網卡)。

 

*查看路由信息的另一個方法:

# netstat -rn

Kernel IP routing table

Destination     Gateway         Genmask         Flags   MSS Window  irtt Iface

0.0.0.0         172.168.1.14    0.0.0.0         UG        0 0          0 eth0

169.254.0.0     0.0.0.0         255.255.0.0     U         0 0          0 eth0

172.168.1.0     0.0.0.0         255.255.255.0   U         0 0          0 eth0

這裏,信息相似,速度稍快,不象route須要等待一段時間,(區別是什麼?)。而後能夠用以下命令查看連通性:

# ping 10.1.10.100

PING 10.1.10.100 (10.1.10.100) 56(84) bytes of data.

From 172.168.1.14 icmp_seq=1 Destination Net Unreachable

From 172.168.1.14 icmp_seq=2 Destination Net Unreachable

可見沒法ping通(這裏10.1.10.100是外網的一臺機器),緣由是由於沒有給route插入外網網線,而是隻將route的lan口與機器相連,當將外網和route的wan口相連以後,就能夠ping通了。

 

# ping 172.168.1.14

PING 172.168.1.14 (172.168.1.14) 56(84) bytes of data.

64 bytes from 172.168.1.14: icmp_req=1 ttl=64 time=0.457 ms

64 bytes from 172.168.1.14: icmp_req=2 ttl=64 time=0.425 ms

可見能夠ping通本網段(這裏172.168.1.14是路由器)。

 

三、讓pc直接鏈接到外部網絡,不通過路由器:

pc的網址:10.1.10.182。

外網其網內主機ip是10.1.10.*。

 

*查看PC基本信息:

# ifconfig

eth0      Link encap:Ethernet  HWaddr 00:24:7e:03:62:14  

          inet addr:10.1.10.182  Bcast:10.1.10.255  Mask:255.255.255.0

          inet6 addr: fe80::224:7eff:fe03:6214/64 Scope:Link

          UP BROADCAST RUNNING MULTICAST  MTU:1500  Metric:1

          RX packets:1776739 errors:12 dropped:2 overruns:0 frame:8

          TX packets:1174650 errors:0 dropped:0 overruns:0 carrier:0

          collisions:0 txqueuelen:1000 

          RX bytes:934090706 (934.0 MB)  TX bytes:107913085 (107.9 MB)

          Interrupt:16 Memory:fc500000-fc520000 

 

lo        Link encap:Local Loopback  

          inet addr:127.0.0.1  Mask:255.0.0.0

          inet6 addr: ::1/128 Scope:Host

          UP LOOPBACK RUNNING  MTU:16436  Metric:1

          RX packets:310442 errors:0 dropped:0 overruns:0 frame:0

          TX packets:310442 errors:0 dropped:0 overruns:0 carrier:0

          collisions:0 txqueuelen:0 

          RX bytes:28063715 (28.0 MB)  TX bytes:28063715 (28.0 MB)

 

*查看PC路由信息:

# route

Kernel IP routing table

Destination     Gateway         Genmask         Flags Metric Ref    Use Iface

default         ns.amazon.esdl. 0.0.0.0         UG    0      0        0 eth0

10.1.10.0       *               255.255.255.0   U     1      0        0 eth0

link-local      *               255.255.0.0     U     1000   0        0 eth0

 

# netstat -rn

Kernel IP routing table

Destination     Gateway         Genmask         Flags   MSS Window  irtt Iface

0.0.0.0         10.1.10.254     0.0.0.0         UG        0 0          0 eth0

10.1.10.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

在這個路由信息的基礎上,測試連通性: 

# ping 10.1.10.119

PING 10.1.10.119 (10.1.10.119) 56(84) bytes of data.

64 bytes from 10.1.10.119: icmp_req=1 ttl=64 time=3.72 ms

這裏,10.1.10.119是網內另一臺主機。

 

 

*添加一個192.168.1.0網絡:

# route add -net 192.168.1.0 netmask 255.255.255.0 dev eth0

# route

Kernel IP routing table

Destination     Gateway         Genmask         Flags Metric Ref    Use Iface

default         ns.amazon.esdl. 0.0.0.0         UG    0      0        0 eth0

10.1.10.0       *               255.255.255.0   U     1      0        0 eth0

link-local      *               255.255.0.0     U     1000   0        0 eth0

192.168.1.0     *               255.255.255.0   U     0      0        0 eth0

 

*再添加192.0.0.0和192.0.0.0網絡:

# route add -net 192.0.0.0 netmask 255.255.0.0 dev eth0

# route add -net 192.0.0.0 netmask 255.0.0.0 dev eth0

# route

Kernel IP routing table

Destination     Gateway         Genmask         Flags Metric Ref    Use Iface

default         ns.amazon.esdl. 0.0.0.0         UG    0      0        0 eth0

10.1.10.0       *               255.255.255.0   U     1      0        0 eth0

link-local      *               255.255.0.0     U     1000   0        0 eth0

192.0.0.0       *               255.255.255.0   U     0      0        0 eth0

192.0.0.0       *               255.255.0.0     U     0      0        0 eth0

192.168.1.0     *               255.255.255.0   U     0      0        0 eth0

這裏,網絡地址結合子網掩碼來標識。

 

*刪除192.0.0.0:

# route del 192.0.0.0

SIOCDELRT: No such process

# route del -net 192.0.0.0

SIOCDELRT: Invalid argument

# route del -net 192.0.0.0 netmask 255.255.0.0

# route

Kernel IP routing table

Destination     Gateway         Genmask         Flags Metric Ref    Use Iface

default         ns.amazon.esdl. 0.0.0.0         UG    0      0        0 eth0

10.1.10.0       *               255.255.255.0   U     1      0        0 eth0

link-local      *               255.255.0.0     U     1000   0        0 eth0

192.0.0.0       *               255.255.255.0   U     0      0        0 eth0

192.168.1.0     *               255.255.255.0   U     0      0        0 eth0

# route del -net 192.0.0.0 netmask 255.255.255.0

# route

Kernel IP routing table

Destination     Gateway         Genmask         Flags Metric Ref    Use Iface

default         gateway.amazon. 0.0.0.0         UG    0      0        0 eth0

10.1.10.0       *               255.255.255.0   U     1      0        0 eth0

link-local      *               255.255.0.0     U     1000   0        0 eth0

192.168.1.0     *               255.255.255.0   U     0      0        0 eth0

從這裏可知,若是沒指定子網掩碼,那麼若是兩個同樣的ip的網絡就沒法刪除成功。

 

*添加一臺主機:

# route add -host 10.1.10.47 dev eth0

# route

Kernel IP routing table

Destination     Gateway         Genmask         Flags Metric Ref    Use Iface

default         gateway.amazon. 0.0.0.0         UG    0      0        0 eth0

10.1.10.0       *               255.255.255.0   U     1      0        0 eth0

quietheart      *               255.255.255.255 UH    0      0        0 eth0

link-local      *               255.255.0.0     U     1000   0        0 eth0

192.168.1.0     *               255.255.255.0   U     0      0        0 eth0

# netstat -rn

Kernel IP routing table

Destination     Gateway         Genmask         Flags   MSS Window  irtt Iface

0.0.0.0         10.1.10.254     0.0.0.0         UG        0 0          0 eth0

10.1.10.0       0.0.0.0         255.255.255.0   U         0 0          0 eth0

10.1.10.47      0.0.0.0         255.255.255.255 UH        0 0          0 eth0

169.254.0.0     0.0.0.0         255.255.0.0     U         0 0          0 eth0

192.168.1.0     0.0.0.0         255.255.255.0   U         0 0          0 eth0

# ping 10.1.10.47

PING 10.1.10.47 (10.1.10.47) 56(84) bytes of data.

64 bytes from 10.1.10.47: icmp_req=1 ttl=64 time=8.02 ms

64 bytes from 10.1.10.47: icmp_req=2 ttl=64 time=9.49 ms

從這裏咱們能夠看出,主機10.1.10.47名稱是"quietheart",注意,添加主機的時候,不指定netmask。添加以後,仍然能夠ping通。

 

*添加一個默認的網關:

# route add default gw 10.1.10.1

# route

Kernel IP routing table

Destination     Gateway         Genmask         Flags Metric Ref    Use Iface

default         10.1.10.1       0.0.0.0         UG    0      0        0 eth0

default         gateway.amazon. 0.0.0.0         UG    0      0        0 eth0

10.1.10.0       *               255.255.255.0   U     1      0        0 eth0

quietheart      *               255.255.255.255 UH    0      0        0 eth0

link-local      *               255.255.0.0     U     1000   0        0 eth0

192.168.1.0     *               255.255.255.0   U     0      0        0 eth0

這裏,默認的路由"default"在沒有其餘路由匹配的時候,會被使用到,能夠有特殊的命令修改默認路由(例如"route add default gw mango-gw"會添加mango-gw主機),具體能夠參見"man"手冊。

 

*刪除一個默認網關:

# route del default gw 10.1.10.1

# route

Kernel IP routing table

Destination     Gateway         Genmask         Flags Metric Ref    Use Iface

default         gateway.amazon. 0.0.0.0         UG    0      0        0 eth0

10.1.10.0       *               255.255.255.0   U     1      0        0 eth0

quietheart      *               255.255.255.255 UH    0      0        0 eth0

link-local      *               255.255.0.0     U     1000   0        0 eth0

192.168.1.0     *               255.255.255.0   U     0      0        0 eth0

 

*爲一臺主機添加一個特定網關:

# route add -host  10.1.10.47 gw 10.1.10.1 dev eth0

# route

Kernel IP routing table

Destination     Gateway         Genmask         Flags Metric Ref    Use Iface

default         gateway.amazon. 0.0.0.0         UG    0      0        0 eth0

10.1.10.0       *               255.255.255.0   U     1      0        0 eth0

quietheart      10.1.10.1       255.255.255.255 UGH   0      0        0 eth0

link-local      *               255.255.0.0     U     1000   0        0 eth0

192.168.1.0     *               255.255.255.0   U     0      0        0 eth0

這裏10.1.10.47的主機名稱爲quietheart,這裏若是gw後面指定的ip地址是不存在的,則命令運行失敗,例如:

# route add -host 10.1.10.47 gw 172.168.1.1 dev eth0

SIOCADDRT: No such process

這裏,172.168.1.1是不存在的,可是192.168.1.1是存在的,由於路由表中有192.168.1.0的網絡,但是實際上192.168.1.1不可達。

 

*添加一個不可達的路由也沒法正常工做,例如:

# route add -host 10.1.10.47 gw 192.168.1.1 dev eth0

# route

Kernel IP routing table

Destination     Gateway         Genmask         Flags Metric Ref    Use Iface

default         ns.amazon.esdl. 0.0.0.0         UG    0      0        0 eth0

10.1.10.0       *               255.255.255.0   U     1      0        0 eth0

quietheart      192.168.1.1     255.255.255.255 UGH   0      0        0 eth0

link-local      *               255.255.0.0     U     1000   0        0 eth0

192.168.1.0     *               255.255.255.0   U     0      0        0 eth0

# ping 10.1.10.47

PING 10.1.10.47 (10.1.10.47) 56(84) bytes of data.

由於192.168.1.1不可達,因此即便添加上去了,也不能正常工做。

 

*刪除一個主機:

# route del -host 10.1.10.47 dev eth0

# route

Kernel IP routing table

Destination     Gateway         Genmask         Flags Metric Ref    Use Iface

default         ns.amazon.esdl. 0.0.0.0         UG    0      0        0 eth0

10.1.10.0       *               255.255.255.0   U     1      0        0 eth0

link-local      *               255.255.0.0     U     1000   0        0 eth0

192.168.1.0     *               255.255.255.0   U     0      0        0 eth0

 

[其它]

待更新。

 

參考資料:

http://www.2cto.com/os/201203/125081.html

http://blog.chinaunix.net/uid-22646981-id-3048260.html

man手冊

相關文章
相關標籤/搜索