route顯示并設置Linux中靜態路由表
說明:
route命令用來顯示并設置Linux內核中的網絡路由表 , route命令設置的路由主要是靜態路由 。實現兩個不同子網之間的通信 , 需要一臺連接兩個網絡的路由器 , 或者同事位于兩個網絡的網關來實現 。
在Linux系統中設置路由通常是為解決一下問題:
1) 該Linux系統在一個局域網中,局域網有一個網關,能夠讓機器訪問Internet , 那么就需要將這臺機器的IP地址設置為Linux機器的默認路由 。需要注意的是,直接在命令行下執行route命令來添加路由 , 只是臨時生效 , 當網卡或者機器重啟之后 , 該路由條目就失效了 。只有剛添加在/etc/rc.local中添加route命令來保證該路由設置永久有效 。
選項and參數:

文章插圖
查看路由表:
[root@zsf ~]# route
Kernel IP routing table
Destination Gateway Genmask Flags Metric Ref Use Iface
12.1.1.0 * 255.255.255.0 U 0 0 0 eth0
link-local * 255.255.0.0 U 1002 0 0 eth0
default 12.1.1.2 0.0.0.0 UG 0 0 0 eth0
[root@zsf ~]# route -n
Kernel IP routing table
Destination Gateway Genmask Flags Metric Ref Use Iface
12.1.1.0 0.0.0.0 255.255.255.0 U 0 0 0 eth0
169.254.0.0 0.0.0.0 255.255.0.0 U 1002 0 0 eth0
0.0.0.0 12.1.1.2 0.0.0.0 UG 0 0 0 eth0
[root@zsf ~]# route -e
Kernel IP routing table
Destination Gateway Genmask Flags MSS Window irtt Iface
12.1.1.0 * 255.255.255.0 U 0 0 0 eth0
link-local * 255.255.0.0 U 0 0 0 eth0
default 12.1.1.2 0.0.0.0 UG 0 0 0 eth0
說明:
其中Flags為路由標志,編輯當前網絡節點的狀態
·U up代表路由當前為啟動狀態
·H host表示此網關為一個主機
·G gateway此網關為一個路由器
·R reinstate route使用動態路由重新初始化的路由
·D dynamically,此路由是動態寫入的
·M modified是有路由守護程序或導向器修改
·! 此路由當前為關閉狀態
插入一條到13.1.1.0/24這個網段的路由從eth0出去
[root@zsf ~]# route add -net 13.1.1.0 netmask 255.255.255.0 dev eth0
[root@zsf ~]# route
Kernel IP routing table
Destination Gateway Genmask Flags Metric Ref Use Iface
12.1.1.0 * 255.255.255.0 U 0 0 0 eth0
13.1.1.0 * 255.255.255.0 U 0 0 0 eth0
link-local * 255.255.0.0 U 1002 0 0 eth0
default 12.1.1.2 0.0.0.0 UG 0 0 0 eth0
刪除一條到13.1.1.0/24這個網段的路由從eth0出去的路由
[root@zsf ~]# route del -net 13.1.1.0 netmask 255.255.255.0 dev eth0
[root@zsf ~]# route
Kernel IP routing table
Destination Gateway Genmask Flags Metric Ref Use Iface
12.1.1.0 * 255.255.255.0 U 0 0 0 eth0
link-local * 255.255.0.0 U 1002 0 0 eth0
default 12.1.1.2 0.0.0.0 UG 0 0 0 eth0
[root@zsf ~]# route add -net 13.1.1.0 netmask 255.255.255.0 dev eth0
SIOCADDRT: No route to host,因為設置的網關地址不可達所以報錯
[root@zsf ~]# route add default gw 12.1.1.13 #設置一個默認網關
[root@zsf ~]# route
Kernel IP routing table
Destination Gateway Genmask Flags Metric Ref Use Iface
12.1.1.0 * 255.255.255.0 U 0 0 0 eth0
13.1.1.0 – 255.255.255.0 ! 0 – 0 –
link-local * 255.255.0.0 U 1002 0 0 eth0
default 12.1.1.13 0.0.0.0 UG 0 0 0 eth0
default 12.1.1.2 0.0.0.0 UG 0 0 0 eth0
[root@zsf ~]# route add -net 0.0.0.0 netmask 0.0.0.0 gw 12.1.1.10 設置一個默認網關(刪除的時候必須把add換成del刪除)
[root@zsf ~]# route
Kernel IP routing table
Destination Gateway Genmask Flags Metric Ref Use Iface
12.1.1.0 * 255.255.255.0 U 0 0 0 eth0
link-local * 255.255.0.0 U 1002 0 0 eth0
default 12.1.1.10 0.0.0.0 UG 0 0 0 eth0
default 12.1.1.2 0.0.0.0 UG 0 0 0 eth0
[root@zsf ~]# route del default gw 12.1.1.13 #刪除一個默認網關
[root@zsf ~]# route
Kernel IP routing table
Destination Gateway Genmask Flags Metric Ref Use Iface
12.1.1.0 * 255.255.255.0 U 0 0 0 eth0
link-local * 255.255.0.0 U 1002 0 0 eth0
【linux路由添加命令講解 linux添加靜態路由配置文件】default 12.1.1.2 0.0.0.0 UG 0 0 0 eth0
#添加一條屏蔽的路由
[root@zsf ~]# route add -net 13.1.1.0 netmask 255.255.255.0 dev eth0 reject
[root@zsf ~]# route
Kernel IP routing table
Destination Gateway Genmask Flags Metric Ref Use Iface
12.1.1.0 * 255.255.255.0 U 0 0 0 eth0
13.1.1.0 – 255.255.255.0 ! 0 – 0 –
link-local * 255.255.0.0 U 1002 0 0 eth0
default 12.1.1.2 0.0.0.0 UG 0 0 0 eth0
#刪除一條屏蔽的路由
[root@zsf ~]# route del -net 13.1.1.0 netmask 255.255.255.0 dev eth0 reject
[root@zsf ~]# route
Kernel IP routing table
Destination Gateway Genmask Flags Metric Ref Use Iface
12.1.1.0 * 255.255.255.0 U 0 0 0 eth0
link-local * 255.255.0.0 U 1002 0 0 eth0
default 12.1.1.2 0.0.0.0 UG 0 0 0 eth0
以上方法都是臨時生效,想讓靜態路由永久生效我們把它寫入到/etc/rc.local開機的時候會自動執行這個文件內的指令 。
vim /etc/rc.local
#增加一條到13.1.1.0/24這個網段下一跳為eth0接口
route add -net 13.1.1.0 netmask 255.255.255.0 dev eth0
#增加一條到13.1.1.0/24這個網段下一跳為13.1.1.254
route add -net 13.1.1.0 netmask 255.255.255.0 gw 13.1.1.254
##增加一條到13.1.1.0/24這個網段下一跳為eth0的屏蔽路由
route add -net 13.1.1.0 netmask 255.255.255.0 dev eth0 reject
#增加一個默認網關
route add -net 0.0.0.0 netmask 0.0.0.0 gw 12.1.1.10
route add default gw 12.1.1.13
以上關于本文的內容,僅作參考!溫馨提示:如遇健康、疾病相關的問題,請您及時就醫或請專業人士給予相關指導!
「愛刨根生活網」www.malaban59.cn小編還為您精選了以下內容,希望對您有所幫助:- 如何批量在Excel中添加標題
- WPS表格自動添加邊框設置教程
- 如何給Word文檔添加頁碼
- 如何在路由器中設置P2P下載限制
- 如何正確重啟H3C ER3100路由器【詳細步驟】
- 如何正確綁定優酷路由寶賬號?
- 如何在PPT中添加藝術字的彎曲效果?
- Excel小技巧:給柱形圖添加誤差線
- 解決TP-LINK路由器顯示設備已離線的方法
- 如何使用軟件為圖片添加Spart濾鏡效果
