推薦閱讀:天天學一個 Linux 命令(62):pinglinux
route 命令用於顯示和設置linux系統的路由表(靜態路由表)。緩存
route 命令用來顯示並設置 Linux 內核中的網絡路由表,route 命令設置的路由主要是靜態路由。在 Linux、BSD 和其餘相似 Unix 的系統上,route 命令用於查看和更改內核路由表。在不一樣的系統上,命令語法不一樣。網絡
route [-CFvnee] route [-v] [-A family] add [-net|-host] target [netmask Nm] [gw Gw] [metric N] i [mss M] [window W] [irtt m] [reject] [mod] [dyn] [reinstate] [[dev] If] route [-v] [-A family] del [-net|-host] target [gw Gw] [netmask Nm] [metric N] [[dev] If] route [-V] [--version] [-h] [--help]
-A #指定地址類型 -C #打印將Linux核心的路由緩存 -v #輸出詳細信息 -n #不執行DNS反向查找,直接顯示數字形式的IP地址 -e #netstat格式顯示路由表 -net #到一個網絡的路由表 -host #到一個主機的路由表 Add #增長指定的路由記錄 Del #刪除指定的路由記錄 Target #目的網絡或目的主機 gw #設置默認網關 mss #設置TCP的最大區塊長度(MSS),單位MB window #指定經過路由表的TCP鏈接的TCP窗口大小 dev #路由記錄所表示的網絡接口
顯示當前系統的路由表3d
[root@CentOS7-1 ~]# route Kernel IP routing table Destination Gateway Genmask Flags Metric Ref Use Iface default gateway 0.0.0.0 UG 100 0 0 ens33 192.168.1.0 0.0.0.0 255.255.255.0 U 100 0 0 ens33 [root@CentOS7-1 ~]# route -n Kernel IP routing table Destination Gateway Genmask Flags Metric Ref Use Iface 0.0.0.0 192.168.1.1 0.0.0.0 UG 100 0 0 ens33 192.168.1.0 0.0.0.0 255.255.255.0 U 100 0 0 ens33
其中 Flags 爲路由標誌,標記當前網絡節點的狀態,Flags 標誌說明以下:code
U #Up 表示此路由當前爲啓動狀態 H #Host 表示此網關爲一主機 G #Gateway 表示此網關爲一路由器 R #Reinstate Route 使用動態路由從新初始化的路由 D #Dynamically 此路由是動態性地寫入 M #Modified 此路由是由路由守護程序或導向器動態修改 ! #表示此路由當前爲關閉狀態
添加網關/設置網關接口
[root@CentOS7-1 ~]# route add -net 192.168.2.0 netmask 255.255.255.0 dev ens33 [root@CentOS7-1 ~]# route -n Kernel IP routing table Destination Gateway Genmask Flags Metric Ref Use Iface 0.0.0.0 192.168.1.1 0.0.0.0 UG 100 0 0 ens33 192.168.1.0 0.0.0.0 255.255.255.0 U 100 0 0 ens33 192.168.2.0 0.0.0.0 255.255.255.0 U 0 0 0 ens33 #增長一條到達192.168.2.0子網的路由。
屏蔽一條路由路由
[root@CentOS7-1 ~]# route add -net 192.168.3.0 netmask 255.255.255.0 reject [root@CentOS7-1 ~]# route -n Kernel IP routing table Destination Gateway Genmask Flags Metric Ref Use Iface 0.0.0.0 192.168.1.1 0.0.0.0 UG 100 0 0 ens33 192.168.1.0 0.0.0.0 255.255.255.0 U 100 0 0 ens33 192.168.2.0 0.0.0.0 255.255.255.0 U 0 0 0 ens33 192.168.3.0 - 255.255.255.0 ! 0 - 0 - #增長一條屏蔽的路由,目的地址爲192.168.3.0子網將被拒絕。
刪除路由記錄get
[root@CentOS7-1 ~]# route del -net 192.168.2.0 netmask 255.255.255.0 [root@CentOS7-1 ~]# route -n Kernel IP routing table Destination Gateway Genmask Flags Metric Ref Use Iface 0.0.0.0 192.168.1.1 0.0.0.0 UG 100 0 0 ens33 192.168.1.0 0.0.0.0 255.255.255.0 U 100 0 0 ens33 192.168.3.0 - 255.255.255.0 ! 0 - 0 - [root@CentOS7-1 ~]# route del -net 192.168.3.0 netmask 255.255.255.0 reject [root@CentOS7-1 ~]# route -n Kernel IP routing table Destination Gateway Genmask Flags Metric Ref Use Iface 0.0.0.0 192.168.1.1 0.0.0.0 UG 100 0 0 ens33 192.168.1.0 0.0.0.0 255.255.255.0 U 100 0 0 ens33
刪除和添加設置默認網關io
[root@CentOS7-1 ~]# route add default gw 192.168.1.1 [root@CentOS7-1 ~]# route -n Kernel IP routing table Destination Gateway Genmask Flags Metric Ref Use Iface 0.0.0.0 192.168.1.1 0.0.0.0 UG 0 0 0 ens33 0.0.0.0 192.168.1.1 0.0.0.0 UG 100 0 0 ens33 192.168.1.0 0.0.0.0 255.255.255.0 U 100 0 0 ens33 [root@CentOS7-1 ~]# route del default gw 192.168.1.1 [root@CentOS7-1 ~]# route -n Kernel IP routing table Destination Gateway Genmask Flags Metric Ref Use Iface 0.0.0.0 192.168.1.1 0.0.0.0 UG 100 0 0 ens33 192.168.1.0 0.0.0.0 255.255.255.0 U 100 0 0 ens33