ip route
用於管理靜態路由表。
linux 系統中,可以自定義從 1-252個路由表。其中,linux系統維護了4個路由表:
- 0#表: 系統保留表
- 253#表: defulte table 沒特別指定的默認路由都放在改表
- 254#表: main table 沒指明路由表的所有路由放在該表
- 255#表: local table 保存本地接口地址,廣播地址、NAT地址 由系統維護,用戶不得更改
查找路由表可以通過ip route show table table_number[table name]命令,路由表和表明的對應關系記錄在/etc/iproute2/rt_tables中
ip route命令如果沒有明確指定table則使用main table,ip route命令是route命令的替換命令,route命令只能操作main路由表
ip route命令格式說明
ip route add
增加路由
- ip route add default via 192.168.1.1
- 增加默認網關(在main路由表中)
- ip route add 192.168.4.0/24 via 192.168.166.1 dev wlan0
- 設置192.168.4.0網段的網關為192.168.166.1,數據走wlan0接口
- ip route add 192.168.1.9 via 192.168.166.1 dev wlan0
- 增加目的地址192.168.1.9的網關為192.168.166.1,數據走wlan0接口
- ip route add default via 192.168.1.1 table 1
- 在1號路由表中增加默認網管
- ip route add 192.168.0.0/24 via 192.168.166.1 table 1
- 在1號路由表中增加192.168.0.0網段的網關為192.168.166.1
ip route show
- ip route 或:ip route show
- 顯示系統路由
- ip route show table local
- 查看本地路由表
ip route get
- ip route get 169.254.0.0/16
- 獲取到目標的單個路由,並按照內核所看到的方式打印其內容
ip route delete
- ip route del 192.168.4.0/24
- 刪除192.168.4.0網段的網關
- ip route del default
- 刪除默認網關
ip route flush
- ip route flush 10.38.0.0/16
- 刪除特定路由
- ip route flush table main
- 清空路由表
ip route是route命令的升級版本,但route命令仍在大量使用
