常用Linux路由命令
第一組命令: ifconfig, ifup, ifdown
1) ifconfig
作用:手動啟動、觀察與修改網(wǎng)絡(luò)接口的相關(guān)參數(shù),包括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:關(guān)閉eth0
# ifconfig eth0 down
注意,修改接口的任何參數(shù)都是“暫時”的,執(zhí)行/etc/init.d/network restart (RH)后,所有修改都丟失了。
2)ifup和ifdown
僅就/etc/sysconfig/network-scripts內(nèi)的ifcfg-ethx(x為數(shù)字)進行啟動或關(guān)閉操作,并不能直接修改接口的參數(shù),而需要手工調(diào)整ifcfg-ethx文件才行。
例2.1:啟動eth0
# ifup eth0
例2.2:關(guān)閉eth0
# ifdown eth0
上面一節(jié)中,通過執(zhí)行/etc/init.d/network restart 來重啟網(wǎng)絡(luò)接口,這條命令其實也要讀取配置文件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:設(shè)置默認(rèn)Linux路由
# route add default gw 192.168.10.30
說明:gw表示后面接的是一個IP地址,而不是dev。
第三組命令: ip
1) IP link
1.1 ) ip [-s] link show [dev]
作用: 查看設(shè)備的硬件相關(guān)設(shè)置,包括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
上面的顯示內(nèi)容中還包括接收和發(fā)送的數(shù)據(jù)包的數(shù)量。
1.2) ip link set eth0 name vbird
ip link set eth0 address aa.aa.aa.aa.aa.aa
作用:修改接口eth0的硬件設(shè)備參數(shù)name和address。
2) ip address
2.1)ip address show eth0
作用:顯示網(wǎng)絡(luò)接口eth0的IP參數(shù)。
例:
# 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
作用:顯示當(dāng)前的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
作用:增加默認(rèn)Linux路由
第四組命令: ping、traceroute、netstat、host和nslookup
4.1)ping
作用:通過ICMP數(shù)據(jù)包來進行整個網(wǎng)絡(luò)的狀況報告。
例1:檢測主機168.95.1.1是否連通
# ping -c 3 168.95.1.1
例2:檢測整個網(wǎng)段有多少主機存活
# ping -c 3 -b 192.168.1.255
例3:追蹤MTU大小
# ping -c 3 -s 8000 -M do 192.168.10.10
-s參數(shù)表示發(fā)送的ICMP包大小為8000個字節(jié),-M do表示不能拆包,結(jié)果將返回錯誤信息。
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字節(jié),如果不能拆包,便無法進行傳遞。
4.2)traceroute
作用:顯示數(shù)據(jù)包到目標(biāo)主機的路徑。
這個命令會針對通往目的主機的所有router進行ICMP的超時等待,并測試回復(fù)時間。如果在默認(rèn)的5秒鐘之內(nèi)traceroute聽不到節(jié)點的回應(yīng),那么屏幕上就會出現(xiàn)一個*的符號,告知該節(jié)點無法有順利的響應(yīng)。這可能是由于有些防火墻或者主機將ICMP數(shù)據(jù)包丟掉,或者某些gateway不支持traceroute的功能。
4.3)netstat
這個命令的詳細(xì)使用方法在我的另一篇博客文章中已經(jīng)有了敘述。
參數(shù):
-r:列出Linux路由表(route table)
-n:不使用主機名稱與服務(wù)名稱,使用IP與Port Number
-a:列出所有聯(lián)機狀態(tài),包括tcp/udp/unix socket
-t:列出TCP數(shù)據(jù)包的連接
-u:列出UDP數(shù)據(jù)包的連接
-l: 列出在listen(監(jiān)聽)的服務(wù)之網(wǎng)絡(luò)狀態(tài)
-p:列出PID與Program的文件名
-c:可以設(shè)置幾秒后自動更新一次網(wǎng)絡(luò)狀態(tài)的顯示
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:查詢的類型,除了傳統(tǒng)的IP與主機的對應(yīng)外,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.