常用Linux路由命令
第一組命令: ifconfig, ifup, ifdown
1) ifconfig
作用:手動啟動、觀察與修改網絡接口的相關參數,包括IP地址以及MTU大小等。
例1.1:暫時修改IP地址
# ifconfig eth0 192.168.100.100
例1.2:修改IP地址、掩碼和MTU
# ifconfig eth0 192.168.100.100 netmask 255.255.255.128 mtu 8000
例1.3:關閉eth0
# ifconfig eth0 down
注意,修改接口的任何參數都是“暫時”的,執行/etc/init.d/network restart (RH)后,所有修改都丟失了。
2)ifup和ifdown
僅就/etc/sysconfig/network-scripts內的ifcfg-ethx(x為數字)進行啟動或關閉操作,并不能直接修改接口的參數,而需要手工調整ifcfg-ethx文件才行。
例2.1:啟動eth0
# ifup eth0
例2.2:關閉eth0
# ifdown eth0
上面一節中,通過執行/etc/init.d/network restart 來重啟網絡接口,這條命令其實也要讀取配置文件ifcfg-ethx。
第二組命令: route
作用:查看和更改主機Linux路由表。
例1:查看主機的Linux路由表
# route -n
Destination Gateway Genmask Flags Metric Ref Use Iface
192.168.1.0 0.0.0.0 255.255.255.0 U 1 0 0 eth0
169.254.0.0 0.0.0.0 255.255.0.0 U 1000 0 0 eth0
0.0.0.0 192.168.1.1 0.0.0.0 UG 0 0 0 eth0
例2:Linux路由的添加
#route add -net 192.168.100.0 netmask 255.255.255.0 dev eth0
例3:Linux路由的刪除
# route del -net 169.254.0.0 netmask 255.255.0.0 dev eth0
例4:設置默認Linux路由
# route add default gw 192.168.10.30
說明:gw表示后面接的是一個IP地址,而不是dev。
第三組命令: ip
1) IP link
1.1 ) ip [-s] link show [dev]
作用: 查看設備的硬件相關設置,包括MAC地址、MTU等。
例1:查看接口eth0
# ip -s link show eth0
2: eth0:
link/ether 00:0c:29:e7:53:6c brd ff:ff:ff:ff:ff:ff
RX: bytes packets errors dropped overrun mcast
86168 892 0 0 0 0
TX: bytes packets errors dropped carrier collsns
184674 797 0 0 0 0
上面的顯示內容中還包括接收和發送的數據包的數量。
1.2) ip link set eth0 name vbird
ip link set eth0 address aa.aa.aa.aa.aa.aa
作用:修改接口eth0的硬件設備參數name和address。
2) ip address
2.1)ip address show eth0
作用:顯示網絡接口eth0的IP參數。
例:
# ip address show eth0
2: eth0:
link/ether 00:0c:29:e7:53:6c brd ff:ff:ff:ff:ff:ff
inet 192.168.1.3/24 brd 192.168.1.255 scope global eth0
inet6 fe80::20c:29ff:fee7:536c/64 scope link tentative
valid_lft forever preferred_lft forever
3) ip route
3.1) ip route show
作用:顯示當前的Linux路由配置。
例:
# ip route show
192.168.1.0/24 dev eth0 proto kernel scope link src 192.168.1.3 metric 1
169.254.0.0/16 dev eth0 scope link metric 1000
default via 192.168.1.1 dev eth0 proto static
可見上面的輸出信息與route -n 的輸出差不多。
3.2) ip route add 192.168.10.0/24 via 192.168.5.100 dev eth0
作用:增加可以通往外部的Linux路由
3.3) ip route add default via 192.168.1.2 dev eth0
作用:增加默認Linux路由
第四組命令: ping、traceroute、netstat、host和nslookup
4.1)ping
作用:通過ICMP數據包來進行整個網絡的狀況報告。
例1:檢測主機168.95.1.1是否連通
# ping -c 3 168.95.1.1
例2:檢測整個網段有多少主機存活
# ping -c 3 -b 192.168.1.255
例3:追蹤MTU大小
# ping -c 3 -s 8000 -M do 192.168.10.10
-s參數表示發送的ICMP包大小為8000個字節,-M do表示不能拆包,結果將返回錯誤信息。
PING 192.168.1.2 (192.168.1.2) 8000(8028) bytes of data.
From 192.168.1.3 icmp_seq=1 Frag needed and DF set (mtu = 1500)
From 192.168.1.3 icmp_seq=1 Frag needed and DF set (mtu = 1500)
--- 192.168.1.2 ping statistics ---
0 packets transmitted, 0 received, +2 errors
上面的錯誤信息表示MTU為1500字節,如果不能拆包,便無法進行傳遞。
4.2)traceroute
作用:顯示數據包到目標主機的路徑。
這個命令會針對通往目的主機的所有router進行ICMP的超時等待,并測試回復時間。如果在默認的5秒鐘之內traceroute聽不到節點的回應,那么屏幕上就會出現一個*的符號,告知該節點無法有順利的響應。這可能是由于有些防火墻或者主機將ICMP數據包丟掉,或者某些gateway不支持traceroute的功能。
4.3)netstat
這個命令的詳細使用方法在我的另一篇博客文章中已經有了敘述。
參數:
-r:列出Linux路由表(route table)
-n:不使用主機名稱與服務名稱,使用IP與Port Number
-a:列出所有聯機狀態,包括tcp/udp/unix socket
-t:列出TCP數據包的連接
-u:列出UDP數據包的連接
-l: 列出在listen(監聽)的服務之網絡狀態
-p:列出PID與Program的文件名
-c:可以設置幾秒后自動更新一次網絡狀態的顯示
5)host
作用:查出某個主機名稱的IP,利用/etc/resolv.conf文件里的DNS主機查詢主機的IP地址
例1:
# host yahoo.com.cn
yahoo.com.cn has address 202.165.102.205
yahoo.com.cn mail is handled by 10 mta-v1.mail.vip.cnb.yahoo.com.
6)nslookup
作用:功能與host基本相同。
語法:nslookup [-query=[type]] [hostname|IP]
-query=type:查詢的類型,除了傳統的IP與主機的對應外,DNS還有很多信息。如mx,cname。
例1:host-->IP
# nslookup www.google.cn
Server: 192.168.1.1
Address: 192.168.1.1#53
Non-authoritative answer:
www.google.cn canonical name = google.cn.
Name: google.cn
Address: 203.208.37.104
Name: google.cn
Address: 203.208.37.99
例2:IP-->host
# nslookup 168.95.1.1
server: 192.168.1.1
Address: 192.168.1.1#53
Non-authoritative answer:
1.1.95.168.in-addr.arpa name = dns.hinet.net.
Authoritative answers can be found from:
1.95.168.in-addr.arpa nameserver = dns.hinet.net.
1.95.168.in-addr.arpa nameserver = hntp1.hinet.net.
1.95.168.in-addr.arpa nameserver = hntp3.hinet.net.