修改host name 即時生效: hostname fc2 啟動生效: vim /etc/sysconfig/network 形式如下: NETWORKING=yes HOSTNAME=rh-linux.fc.org ########################################################## 首先,先了解傳統的網路配置命令: 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 [OPTIONS] OBJECT [COMMAND [ARGUMENTS]]
4.1 ip link set?改變設備的屬性. 縮寫:set、s
示例1:up/down 起動/關閉設備。 ip link set dev eth0 up 這個等於傳統的 ifconfig eth0 up ifconfig eth0 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 這個命令等於傳統的 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 等價於 ifconfig eth0:5 192.168.4.1/28 ip addr add local 192.168.4.1/28 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/24
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
示例1: 通過路由表inr.ruhep路由來自源地址為192.203.80/24的數據包 ip ru add from 192.203.80/24 table inr.ruhep prio 220
示例2:把源地址為193.233.7.83的數據報的源地址轉換為192.203.80.144,並通過表1進行路由 ip ru add from 193.233.7.83 nat 192.203.80.144 table 1 prio 320
示例3:刪除無用的預設規則 ip ru del prio 32767
8.7. ip rule show — 列出路由規則 縮寫:show、list、sh、ls、l 示例1: ip ru ls 0: from all lookup local 32762: from 192.168.4.89 lookup fddi153 32764: from 192.168.4.88 lookup fddi153 32766: from all lookup main 32767: from all lookup 253 ————————— 9. ip maddress — 多播地址管理 縮寫:show、list、sh、ls、l 9.3.ip maddress show — 列出多播地址 示例1: ip maddr ls dummy
9.4. ip maddress add — 加入多播地址 ip maddress delete — 刪除多播地址 縮寫:add、a;delete、del、d 使用這兩個命令,我們可以添加/刪除在網路介面上監聽的鏈路層多播地址。這個命令只能管理鏈路層地址。
示例1: 增加 ip maddr add 33:33:00:00:00:01 dev dummy 示例2: 查看 ip -O maddr ls dummy 2: dummy link 33:33:00:00:00:01 users 2 static link 01:00:5e:00:00:01 示例3: 刪除 ip maddr del 33:33:00:00:00:01 dev dummy ————————— 10.ip mroute — 多播路由緩存管理
10.4. ip mroute show — 列出多播路由緩存條目 縮寫:show、list、sh、ls、l
示例1:查看 ip mroute ls (193.232.127.6, 224.0.1.39) Iif: unresolved (193.232.244.34, 224.0.1.40) Iif: unresolved (193.233.7.65, 224.66.66.66) Iif: eth0 Oifs: pimreg 示例2:查看 ip -s mr ls 224.66/16 (193.233.7.65, 224.66.66.66) Iif: eth0 Oifs: pimreg 9383 packets, 300256 bytes ————————— 11. ip tunnel — 通道配置 縮寫 tunnel、tunl
11.4.ip tunnel add — 添加新的通道 ip tunnel change — 修改現有的通道 ip tunnel delete — 刪除一個通道 縮寫:add、a;change、chg;delete、del、d 示例1:建立一個點對點通道,最大TTL是32 ip tunnel add Cisco mode sit remote 192.31.7.104 local 192.203.80.1 ttl 32
11.4.ip tunnel show — 列出現有的通道 縮寫:show、list、sh、ls、l 示例1: ip -s tunl ls Cisco ————————— 12. ip monitor和rtmon — 狀態監視 ip命令可以用於連續地監視設備、地址和路由的狀態。這個命令選項的格式有點不同,命令選項的名字叫做monitor,接著是操作對象: ip monitor [ file FILE ] [ all | OBJECT-LIST ] 示例1: rtmon file /var/log/rtmon.log 示例2: ip monitor file /var/log/rtmon.log r