1. 程式人生 > >Linux route指定靜態路由配置

Linux route指定靜態路由配置

linux 命令 路由

route

顯示並設置Linux中靜態路由表

說明:

route命令用來顯示並設置Linux內核中的網絡路由表,route命令設置的路由主要是靜態路由。實現兩個不同子網之間的通信,需要一臺連接兩個網絡的路由器,或者同事位於兩個網絡的網關來實現。

Linux系統中設置路由通常是為解決一下問題:

1) Linux系統在一個局域網中,局域網有一個網關,能夠讓機器訪問Internet,那麽就需要將這臺機器的IP地址設置為Linux機器的默認路由。需要註意的是,直接在命令行下執行route命令來添加路由,只是臨時生效,當網卡或者機器重啟之後,該路由條目就失效了。只有剛添加在

/etc/rc.local中添加route命令來保證該路由設置永久有效。

選項and參數:

選項

解釋英文

解釋中文

-A



-c

operate on the kernel’s routing cache.

打印將Linux核心的路由緩存

-n


不執行DNS反向查找,直接顯示數字形式的IP地址

-e


netstat格式顯示路由表

-net

the target is a network

到一個網絡的路由表

-host

the target is a host.

到一個主機的路由表

參數

解釋英文

解釋中文

add

add a new route.

增加指定的路由記錄

del

delete a route.

刪除指定的路由記錄

Target


母的網絡或目的主機

gw


設置網關,必須可達

dev


路由記錄所表示的網絡接口

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

[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

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


Linux route指定靜態路由配置