iproute是用於linux下網絡配置工具,該工具包包含如下組件linux
# rpm -ql iproute | grep bin /sbin/cbq #流量控制 /sbin/ifcfg #網絡地址配置管理 /sbin/ip #網絡配置命令 /sbin/rtmon #rtmon listens on netlink socket and monitors routing table changes. /sbin/tc #進行流量控制的命令 /usr/sbin/arpd #收集arp信息保存到本地cache daemon /usr/sbin/lnstat #網絡統計信息 /usr/sbin/nstat #顯示網絡統計信息 /usr/sbin/rtacct #查看數據包流量狀態 【nstat and rtacct are simple tools to monitor kernel snmp counters and network interface statistics.】 /usr/sbin/ss #相似netstat命令,顯示活動鏈接
iproute的中心是ip
這個命令,相似arp
、ifconfig
、route
命令雖然這些工具可以工做,可是在Linux2.2和更高版本的內核上就有點out了。數據庫
# ip --help Usage: ip [ OPTIONS ] OBJECT { COMMAND | help } ip [ -force ] -batch filename where OBJECT := { link | addr | addrlabel | route | rule | neigh | ntable | tunnel | maddr | mroute | mrule | monitor | xfrm } OPTIONS := { -V[ersion] | -s[tatistics] | -d[etails] | -r[esolve] | -f[amily] { inet | inet6 | ipx | dnet | link } | -o[neline] | -t[imestamp] | -b[atch] [filename] | -rc[vbuf] [size]}
# ip -V #打印iproute信息 ip utility, iproute2-ss091226
顯示鏈路信息緩存
# ip link 1: lo: <LOOPBACK,UP,LOWER_UP> mtu 16436 qdisc noqueue state UNKNOWN link/loopback 00:00:00:00:00:00 brd 00:00:00:00:00:00 2: eth0: <BROADCAST,MULTICAST> mtu 1500 qdisc pfifo_fast state DOWN qlen 1000 link/ether 00:0c:29:3b:9c:6f brd ff:ff:ff:ff:ff:ff # ip link show dev eth0 2: eth0: <BROADCAST,MULTICAST> mtu 1500 qdisc pfifo_fast state DOWN qlen 1000 link/ether 00:0c:29:3b:9c:6f brd ff:ff:ff:ff:ff:ff
顯示IP地址bash
# ip addr 1: lo: <LOOPBACK,UP,LOWER_UP> mtu 16436 qdisc noqueue state UNKNOWN link/loopback 00:00:00:00:00:00 brd 00:00:00:00:00:00 inet 127.0.0.1/8 scope host lo inet6 ::1/128 scope host valid_lft forever preferred_lft forever 2: eth0: <BROADCAST,MULTICAST> mtu 1500 qdisc pfifo_fast state DOWN qlen 1000 link/ether 00:0c:29:3b:9c:6f brd ff:ff:ff:ff:ff:ff inet 192.168.0.10/24 brd 192.168.0.255 scope global eth0
顯示路由ip route
[相似route -n]網絡
# ip route | column -t 192.168.0.0/24 dev eth1 scope link 10.2.0.0/16 dev eth2 proto kernel scope link src 10.2.0.111 default via 10.2.255.254 dev eth2 # ip route del 192.168.0.0/24 dev eth1 # ip route add 192.168.0.0/24 dev eth1 # ip route del via 10.2.255.254 //刪除默認路由 # ip route add via 10.2.255.254 //增長默認路由 # ip route add 192.168.1.0/24 via 192.168.0.1 //增長靜態路由,192.168.0.1爲下一跳地址 # ip route del 192.168.1.0/24 via 192.168.0.1 //刪除靜態路由
顯示arp信息ip neigh
[能夠取代arp -n],刪除則是ip neigh del IP地址 dev 設備名
socket
若是你有一個大規模的路由器,須要同時知足不一樣用戶對於路由的不通需求,路由策略數據庫能夠幫你經過多路由表技術來實現。當內核須要作出路由選擇時,它會找出應該參考哪一張路由表。除了ip
外,route
也能夠修改main和local表。工具
# ip rule 0: from all lookup local 32766: from all lookup main 32767: from all lookup default
上面列出了規則的優先順序。ip route
命令默認顯示的就是main表。ip route show table all
顯示全部規則中的表oop
# ip route list table local broadcast 192.168.0.255 dev eth0 proto kernel scope link src 192.168.0.10 broadcast 10.2.0.0 dev eth1 proto kernel scope link src 10.2.0.217 broadcast 127.255.255.255 dev lo proto kernel scope link src 127.0.0.1 ... ...
default表爲空code
讓咱們再來一個真實的例子。我有兩個Cable Modem,鏈接到了一個 Linux的NAT (「假裝」) 路由器上。這裏的室友們向我付費使用Internet。假如我其中的一個室友由於只想訪問 hotmail 而但願少付一些錢。對我來講這沒有問題,他們確定只能使用那個比較次的Cable Modem。對象
那個比較快的cable modem 的IP地址是 212.64.94.251,PPP 鏈路,對端IP是212.64.94.1。而那個比較慢的cable modem的IP地址是212.64.78.148,對端是195.96.98.253。
local 表:
[ahu@home ahu]$ ip route list table local broadcast 127.255.255.255 dev lo proto kernel scope link src 127.0.0.1 local 10.0.0.1 dev eth0 proto kernel scope host src 10.0.0.1 broadcast 10.0.0.0 dev eth0 proto kernel scope link src 10.0.0.1 local 212.64.94.251 dev ppp0 proto kernel scope host src 212.64.94.251 broadcast 10.255.255.255 dev eth0 proto kernel scope link src 10.0.0.1 broadcast 127.0.0.0 dev lo proto kernel scope link src 127.0.0.1 local 212.64.78.148 dev ppp2 proto kernel scope host src 212.64.78.148 local 127.0.0.1 dev lo proto kernel scope host src 127.0.0.1 local 127.0.0.0/8 dev lo proto kernel scope host src 127.0.0.1
讓咱們看看「main」路由表:
[ahu@home ahu]$ ip route list table main 195.96.98.253 dev ppp2 proto kernel scope link src 212.64.78.148 212.64.94.1 dev ppp0 proto kernel scope link src 212.64.94.251 10.0.0.0/8 dev eth0 proto kernel scope link src 10.0.0.1 127.0.0.0/8 dev lo scope link default via 212.64.94.1 dev ppp0
咱們如今爲咱們的朋友建立了一個叫作「John」的規則。其實咱們徹底可使用純數字表示規則,可是不方便。咱們能夠向/etc/iproute2/rt_tables文件中添加數字與名字的關聯:
# echo 200 John >> /etc/iproute2/rt_tables # ip rule add from 10.0.0.10 table John # ip rule 0: from all lookup local 32765: from 10.0.0.10 lookup John 32766: from all lookup main 32767: from all lookup default
如今,剩下的事情就是爲 John 的路由表建立路由項了。別忘了刷新路由緩存:
# ip route add default via 195.96.98.253 dev ppp2 table John # ip route flush cache
總結主要是如下幾步:
echo 200 John >> /etc/iproute2/rt_tables #方便表示,把規則名字和數字對應加入到/etc/iproute2/rt_tables文件 ip rule add from 10.0.0.10 table John #新增規則 ip route add default via 195.96.98.253 dev ppp2 table John #規則中添加路由表 ip route flush cache #刷新路由表