1. net-tools
net-tools一般包括ifconfig、route、arp和netstat等工具,一般用來配置網絡功能,解決網絡故障。net-tools起源於BSD的TCP/IP工具箱,後來成爲老版本Linux內核中配置網絡功能的工具。但自2001年起,Linux社區已經對其停止維護。同時,一些Linux發行版比如Ubuntu 18,Arch Linux和CentOS/RHEL 7則已經完全拋棄了net-tools,只支持iproute2。
2. iproute2
作爲網絡配置工具的一份子,iproute2是linux下管理控制TCP/IP網絡和流量控制的新一代工具包,旨在替代老派的工具鏈net-tools。net-tools通過procfs(/proc)和ioctl系統調用去訪問和改變內核網絡配置,而iproute2則通過netlink套接字接口與內核通訊。net-tools的用戶體驗差,相對混亂。而iproute2的用戶接口相對net-tools來說相對來說,更加直觀。比如,各種網絡資源(如link、IP地址、路由和隧道等)均使用合適的對象抽象去定義,使得用戶可使用一致的語法去管理不同的對象。。更重要的是,到目前爲止,iproute2仍處在持續開發中。
3. iproute2概覽
輸入命令後如下:
# ip help
Usage: ip [ OPTIONS ] OBJECT { COMMAND | help }
ip [ -force ] -batch filename
where OBJECT := { link | addr | addrlabel | route | rule | neigh | ntable |
tunnel | tuntap | maddr | mroute | mrule | monitor | xfrm |
netns | l2tp | tcp_metrics | token }
OPTIONS := { -V[ersion] | -s[tatistics] | -d[etails] | -r[esolve] |
-h[uman-readable] | -iec |
-f[amily] { inet | inet6 | ipx | dnet | bridge | link } |
-4 | -6 | -I | -D | -B | -0 |
-l[oops] { maximum-addr-flush-attempts } |
-o[neline] | -t[imestamp] | -b[atch] [filename] |
-rc[vbuf] [size] | -n[etns] name | -a[ll] }
4. ip route基本用法
1.顯示ip地址
ip a
ip address show
ip addr show dev eth0
ip a sh eth0
2.列出路由
ip route list
ip route show
ip route
3.查看指定網段的路由
ip route list 192.168.2.0/24
4.添加路由
ip route add 192.168.2.0/24 via 192.168.1.1
5.追加路由
ip route append 192.168.2.0/24 via 192.168.1.12
#追加一個指定網絡的路由,爲了平滑切換網關使用
6.修改路由
ip route change 192.168.2.0/24 via 192.168.1.11
ip route replace 192.168.2.0/24 via 192.168.1.111
7.刪除路由
ip route del 192.168.2.0/24 via 192.168.1.1
8.清空指定網絡的路由
ip route flush 192.168.2.0/24
#這個是清理所有192.168.2.0/24相關的所有路由,
#有時候設置錯網關存在多條記錄,就需要一次性清空相關路由再進行添加
9.添加默認路由
ip route add default via 192.168.1.1
10.指定路由metirc
ip route add 192.168.2.0/24 via 192.168.1.15 metric 10
11.增加刪除GRE隧道
ip tunnel add gre01 mode gre local 10.1.1.1 remote 20.2.2.1 ttl 255
ip tunnel del gre01