CentOS 6.9永久設置靜態路由表以及路由表常用設置


一、路由表常用設置:

1、使用route命令添加的路由,機器重啟或者網卡重啟后路由就失效了,方法:

//添加到主機的路由
# route add –host 192.168.1.11 dev eth0
# route add –host 192.168.1.12 gw 192.168.1.1

//添加到網絡的路由
# route add –net 192.168.1.11 netmask 255.255.255.0 dev eth0
# route add –net 192.168.1.11 netmask 255.255.255.0 gw 192.168.1.1
# route add –net 192.168.1.0/24 dev eth1

//添加默認網關
# route add default gw 192.168.2.1

//刪除路由
# route del –host 192.168.1.11 dev eth0

2、還可以使用ip命令來添加、刪除路由

//添加
ip route add default via 172.16.10.2 dev eth0 ip route add 172.16.1.0/24 via 172.16.10.2 dev eth0

3、查詢

# netstat -nr
# route -n
# ip route list
# ip route show

二、永久設置路由表:

方法一:在/etc/sysconfig/network配置文件中配置(我測試了好像不太可行):

default via 192.168.3.1 dev eth0 #192.168.3.1為eth0網卡的網關地址
10.211.6.0/24 via 192.168.3.1 dev eth0
10.0.0.0/8 via 10.212.52.1 dev eth1 #10.212.52.1為eth1網卡的網關地址

注:該種配置寫法同樣支持寫到/etc/sysconfig/network-scripts/route-interferface配置文件中。

具體可以參看redhat官方文檔

方法二:在/etc/sysconfig/network-scripts/route-{interferface}配置文件配置({interferface}為網卡接口,如eth0)

在這里支持兩種配置格式的寫法

A:方法1中提到的方法

# cat /etc/sysconfig/network-scripts/route-eth0
0.0.0.0/0 via 192.168.3.1 dev eth0
10.211.6.0/24 via 192.168.3.1 dev eth0
# cat /etc/sysconfig/network-scripts/route-eth1
10.0.0.0/8 via 10.212.52.1 dev eth1

B:網絡掩碼法(投機取巧,走漏洞)

# cat /etc/sysconfig/network-scripts/route-eth0
ADDRESS0=0.0.0.0
NETMASK0=0.0.0.0
GATEWAY0=192.168.3.1
ADDRESS1=10.211.6.0
NETMASK1=255.255.255.0
GATEWAY1=192.168.3.1

其中網段地址和掩碼全是0代表為所有網段,即默認路由。 

# cat /etc/sysconfig/network-scripts/route-eth1
ADDRESS0=10.0.0.0
NETMASK0=255.0.0.0
GATEWAY0=10.212.52.1

網絡掩碼法也可以參看redhat官方文檔

方法三:/etc/sysconfig/static-routes配置(推薦)

# cat /etc/sysconfig/static-route
any net any gw 192.168.3.1
any net 10.211.6.0/24 gw 192.168.3.1
any net 10.0.0.0 netmask 255.0.0.0 gw 10.212.52.1

注:默認情況下主機中並沒有該文件,需要手動創建。net是范圍,host可以單獨指定某一台機器。

之所以該方法也可以是因為/etc/init.d/network啟動腳本會調用該文件,具體調用部分代碼原理如下:

# Add non interface-specific static-routes.
if [ -f /etc/sysconfig/static-routes ]; then
  grep "^any" /etc/sysconfig/static-routes | while read ignore args ; do
    /sbin/route add -$args
  done
fi

方法四:寫在開機腳本/etc/rc.local

route add -net 10.8.0.0 255.255.255.0 gw 192.168.199.2

 

參考:

http://www.361way.com/linux-define-static-route/4053.html(以上內容轉自此篇文章)

http://dev.dafan.info/detail/221534

http://www.jb51.net/article/101683.htm

https://segmentfault.com/a/1190000004165066

http://network.chinabyte.com/194/11200694.shtml


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM