傳統網絡配置命令與ip高級路由命令學習示例 by KindGeorge 2005.5.9 懂得網絡配置命令是一般技術人員必備的技術,經過一段時間的研究和學習,總結了一些常用的命令和示例以便日后查閱. 傳統的在1--3點,ip高級路由命令在4--12點,兩者部分可以通用,并達到同樣的目的,但ip的功能更強大,可以實現更多的配置目的. 首先,先了解傳統的網絡配置命令: 1. 使用ifconfig命令配置并查看網絡接口情況 示例1: 配置eth0的IP,同時激活設備: # ifconfig eth0 192.168.4.1 netmask 255.255.255.0 up 示例2: 配置eth0別名設備 eth0:1 的IP,并添加路由 # ifconfig eth0:1 192.168.4.2 # route add –host 192.168.4.2 dev eth0:1 示例3:激活(禁用)設備 # ifconfig eth0:1 up(down) 示例4:查看所有(指定)網絡接口配置 # ifconfig (eth0) 2. 使用route 命令配置路由表 示例1:添加到主機路由 # route add –host 192.168.4.2 dev eth0:1 # route add –host 192.168.4.1 gw 192.168.4.250 示例2:添加到網絡的路由 # route add –net IP netmask MASK eth0 # route add –net IP netmask MASK gw IP # route add –net IP/24 eth1 示例3:添加默認網關 # route add default gw IP 示例4:刪除路由 # route del –host 192.168.4.1 dev eth0:1 示例5:查看路由信息 # route 或 route -n (-n 表示不解析名字,列出速度會比route 快) 3.ARP 管理命令 示例1:查看ARP緩存 # arp 示例2: 添加 # arp –s IP MAC 示例3: 刪除 # arp –d IP 4. ip是iproute2軟件包里面的一個強大的網絡配置工具,它能夠替代一些傳統的網絡管理工具。例如:ifconfig、route等, 上面的示例完全可以用下面的ip命令實現,而且ip命令可以實現更多的功能.下面介紹一些示例: 4.0 ip命令的語法 ip命令的用法如下: ip [OPTIONS] OBJECT [COMMAND [ARGUMENTS]] 4.1 ip link set--改變設備的屬性. 縮寫:set、s 示例1:up/down 起動/關閉設備。 # ip link set dev eth0 up 這個等于傳統的 # ifconfig eth0 up(down) 示例2:改變設備傳輸隊列的長度。 參數:txqueuelen NUMBER或者txqlen NUMBER # ip link set dev eth0 txqueuelen 100 示例3:改變網絡設備MTU(最大傳輸單元)的值。 # ip link set dev eth0 mtu 1500 示例4: 修改網絡設備的MAC地址。 參數: address LLADDRESS # ip link set dev eth0 address 00:01:4f:00:15:f1 4.2 ip link show--顯示設備屬性. 縮寫:show、list、lst、sh、ls、l -s選項出現兩次或者更多次,ip會輸出更為詳細的錯誤信息統計。 示例: # ip -s -s link ls eth0 eth0: mtu 1500 qdisc cbq qlen 100 link/ether 00:a0:clearcase/" target="_blank" >cc:66:18:78 brd ff:ff:ff:ff:ff:ff RX: bytes packets errors dropped overrun mcast 2449949362 2786187 0 0 0 0 RX errors: length crc frame fifo missed 0 0 0 0 0 TX: bytes packets errors dropped carrier collsns 178558497 1783946 332 0 332 35172 TX errors: aborted fifo window heartbeat 0 0 0 332 這個命令等于傳統的 ifconfig eth0 5.1 ip address add--添加一個新的協議地址. 縮寫:add、a 示例1:為每個地址設置一個字符串作為標簽。為了和Linux-2.0的網絡別名兼容,這個字符串必須以設備名開頭,接著一個冒號, # ip addr add local 192.168.4.1/28 brd + label eth0:1 dev eth0 示例2: 在以太網接口eth0上增加一個地址192.168.20.0,掩碼長度為24位(155.155.155.0),標準廣播地址,標簽為eth0:Alias: # ip addr add 192.168.4.2/24 brd + dev eth1 label eth1:1 這個命令等于傳統的: ifconfig eth1:1 192.168.4.2 5.2 ip address delete--刪除一個協議地址. 縮寫:delete、del、d # ip addr del 192.168.4.1/24 brd + dev eth0 label eth0:Alias1 5.3 ip address show--顯示協議地址. 縮寫:show、list、lst、sh、ls、l # ip addr ls eth0 5.4.ip address flush--清除協議地址. 縮寫:flush、f 示例1 : 刪除屬于私網10.0.0.0/8的所有地址: # ip -s -s a f to 10/8 示例2 : 取消所有以太網卡的IP地址 # ip -4 addr flush label "eth0" 6. ip neighbour--neighbour/arp表管理命令 縮寫 neighbour、neighbor、neigh、n 命令 add、change、replace、delete、fulsh、show(或者list) 6.1 ip neighbour add -- 添加一個新的鄰接條目 ip neighbour change--修改一個現有的條目 ip neighbour replace--替換一個已有的條目 縮寫:add、a;change、chg;replace、repl 示例1: 在設備eth0上,為地址10.0.0.3添加一個permanent ARP條目: # ip neigh add 10.0.0.3 lladdr 0:0:0:0:0:1 dev eth0 nud perm 示例2:把狀態改為reachable # ip neigh chg 10.0.0.3 dev eth0 nud reachable 6.2.ip neighbour delete--刪除一個鄰接條目 示例1:刪除設備eth0上的一個ARP條目10.0.0.3 # ip neigh del 10.0.0.3 dev eth0 6.3.ip neighbour show--顯示網絡鄰居的信息. 縮寫:show、list、sh、ls 示例1: # ip -s n ls 193.233.7.254 193.233.7.254. dev eth0 lladdr 00:00:0c:76:3f:85 ref 5 used 12/13/20 nud reachable 6.4.ip neighbour flush--清除鄰接條目. 縮寫:flush、f 示例1: (-s 可以顯示詳細信息) # ip -s -s n f 193.233.7.254 7. 路由表管理 7.1.縮寫 route、ro、r 7.5.路由表 從Linux-2.2開始,內核把路由歸納到許多路由表中,這些表都進行了編號,編號數字的范圍是1到255。另外, 為了方便,還可以在/etc/iproute2/rt_tables中為路由表命名。 默認情況下,所有的路由都會被插入到表main(編號254)中。在進行路由查詢時,內核只使用路由表main。 7.6.ip route add -- 添加新路由 ip route change -- 修改路由 ip route replace -- 替換已有的路由 縮寫:add、a;change、chg;replace、repl 示例1: 設置到網絡10.0.0/24的路由經過網關193.233.7.65 # ip route add 10.0.0/24 via 193.233.7.65 示例2: 修改到網絡10.0.0/24的直接路由,使其經過設備dummy # ip route chg 10.0.0/24 dev dummy 示例3: 實現鏈路負載平衡.加入缺省多路徑路由,讓ppp0和ppp1分擔負載(注意:scope值并非必需,它只不過是告訴內核, 這個路由要經過網關而不是直連的。實際上,如果你知道遠程端點的地址,使用via參數來設置就更好了)。 # ip route add default scope global nexthop dev ppp0 nexthop dev ppp1 # ip route replace default scope global nexthop dev ppp0 nexthop dev ppp1 示例4: 設置NAT路由。在轉發來自192.203.80.144的數據包之前,先進行網絡地址轉換,把這個地址轉換為193.233.7.83 # ip route add nat 192.203.80.142 via 193.233.7.83 示例5: 實現數據包級負載平衡,允許把數據包隨機從多個路由發出。weight 可以設置權重. # ip route replace default equalize nexthop via 211.139.218.145 dev eth0 weight 1 nexthop via 211.139.218.145 dev eth1 weight 1 7.7.ip route delete-- 刪除路由 縮寫:delete、del、d 示例1:刪除上一節命令加入的多路徑路由 # ip route del default scope global nexthop dev ppp0 nexthop dev ppp1 7.8.ip route show -- 列出路由 縮寫:show、list、sh、ls、l 示例1: 計算使用gated/bgp協議的路由個數 # ip route ls proto gated/bgp |wc 1413 9891 79010 示例2: 計算路由緩存里面的條數,由于被緩存路由的屬性可能大于一行,以此需要使用-o選項 # ip -o route ls cloned |wc 159 2543 18707 示例3: 列出路由表TABLEID里面的路由。缺省設置是table main。TABLEID或者是一個真正的路由表ID或者是/etc/iproute2/rt_tables文件定義的字符串, 或者是以下的特殊值: all -- 列出所有表的路由; cache -- 列出路由緩存的內容。 ip ro ls 193.233.7.82 tab cache 示例4: 列出某個路由表的內容 # ip route ls table fddi153 示例5: 列出默認路由表的內容 # ip route ls 這個命令等于傳統的: route 7.9.ip route flush -- 擦除路由表 示例1: 刪除路由表main中的所有網關路由(示例:在路由監控程序掛掉之后): # ip -4 ro flush scope global type unicast 示例2:清除所有被克隆出來的IPv6路由: # ip -6 -s -s ro flush cache 示例3: 在gated程序掛掉之后,清除所有的BGP路由: # ip -s ro f proto gated/bgp 示例4: 清除所有ipv4路由cache # ip route flush cache *** IPv4 routing cache is flushed. 7.10 ip route get -- 獲得單個路由 .縮寫:get、g 使用這個命令可以獲得到達目的地址的一個路由以及它的確切內容。 ip route get命令和ip route show命令執行的操作是不同的。ip route show命令只是顯示現有的路由,而ip route get命令在必要時會派生出新的路由。 示例1: 搜索到193.233.7.82的路由 # ip route get 193.233.7.82 193.233.7.82 dev eth0 src 193.233.7.65 realms inr.ac cache mtu 1500 rtt 300 示例2: 搜索目的地址是193.233.7.82,來自193.233.7.82,從eth0設備到達的路由(這條命令會產生一條非常有意思的路由,這是一條到193.233.7.82的回環路由) # ip r g 193.233.7.82 from 193.233.7.82 iif eth0 193.233.7.82 from 193.233.7.82 dev eth0 src 193.233.7.65 realms inr.ac/inr.ac cache