CentOS服務(wù)器如何添加永久靜態(tài)路由

admin Linux運維評論669字數(shù) 1472閱讀模式

一、Linux添加永久靜態(tài)路由

方法一:修改 rc.local 配置文件文章源自網(wǎng)吧系統(tǒng)維護-http://www.strong-digital.cn/7796.html

范圍:測試 Ubuntu 14.04、centos 6.4、debian 8.1、redhat 6.4 系統(tǒng)可用文章源自網(wǎng)吧系統(tǒng)維護-http://www.strong-digital.cn/7796.html

使用方法:文章源自網(wǎng)吧系統(tǒng)維護-http://www.strong-digital.cn/7796.html

1.向 linux 系統(tǒng)的 /etc/rc.local文件中添加命令route add -net 192.168.2.0/24 gw 192.168.2.254(服務(wù)器的默認路由為192.168.2.254),或者添加命令route add -net 192.168.3.0/24 dev eth0,前者是依賴默認路由進行數(shù)據(jù)的轉(zhuǎn)發(fā),后者是依賴網(wǎng)卡接口進行數(shù)據(jù)的轉(zhuǎn)發(fā)文章源自網(wǎng)吧系統(tǒng)維護-http://www.strong-digital.cn/7796.html

2.reboot重啟系統(tǒng)命令生效文章源自網(wǎng)吧系統(tǒng)維護-http://www.strong-digital.cn/7796.html

3.用 route -n 命令查看命令是否已經(jīng)生效文章源自網(wǎng)吧系統(tǒng)維護-http://www.strong-digital.cn/7796.html

方法二:修改 static-routes 配置文件文章源自網(wǎng)吧系統(tǒng)維護-http://www.strong-digital.cn/7796.html

范圍:測試 centos 6.4、redhat 6.4 系統(tǒng)可用文章源自網(wǎng)吧系統(tǒng)維護-http://www.strong-digital.cn/7796.html

使用方法(此方法比 rc.local 方法實用,能保證網(wǎng)絡(luò)鏈路的通暢):文章源自網(wǎng)吧系統(tǒng)維護-http://www.strong-digital.cn/7796.html

1.向系統(tǒng)的 /etc/sysconfig/static-routes 文件(如果系統(tǒng)沒有此文件,可以新建此文件)中添加命令any net 192.168.2.0 netmask 255.255.255.0 gw 192.168.2.254(服務(wù)器的默認路由為192.168.2.254),或者添加命令any net 192.168.2.0 netmask 255.255.255.0 dev eth0, 前者是依賴默認路由進行數(shù)據(jù)的轉(zhuǎn)發(fā),后者是依賴網(wǎng)卡接口進行數(shù)據(jù)的轉(zhuǎn)發(fā)文章源自網(wǎng)吧系統(tǒng)維護-http://www.strong-digital.cn/7796.html

2.service network restart 命令重啟網(wǎng)絡(luò)服務(wù)文章源自網(wǎng)吧系統(tǒng)維護-http://www.strong-digital.cn/7796.html

3.用 route -n 命令查看命令是否已經(jīng)生效文章源自網(wǎng)吧系統(tǒng)維護-http://www.strong-digital.cn/7796.html

方法三:修改 interface 配置文件文章源自網(wǎng)吧系統(tǒng)維護-http://www.strong-digital.cn/7796.html

范圍:測試 debian 8.1、Ubuntu 14.04 系統(tǒng)可用文章源自網(wǎng)吧系統(tǒng)維護-http://www.strong-digital.cn/7796.html

使用方法:文章源自網(wǎng)吧系統(tǒng)維護-http://www.strong-digital.cn/7796.html

1.向系統(tǒng)的 /etc/network/interface 文件中添加命令 up route add -net 192.168.2.0/24 gw 192.168.2.254(服務(wù)器的默認路由為192.168.2.254),或者添加命令 up route add -net 192.168.3.0/24 dev eth0,前者是依賴默認路由進行數(shù)據(jù)的轉(zhuǎn)發(fā),后者是依賴網(wǎng)卡接口進行數(shù)據(jù)的轉(zhuǎn)發(fā)文章源自網(wǎng)吧系統(tǒng)維護-http://www.strong-digital.cn/7796.html

2.reboot 重啟系統(tǒng)命令生效文章源自網(wǎng)吧系統(tǒng)維護-http://www.strong-digital.cn/7796.html

3.用 route -n 命令查看命令是否已經(jīng)生效文章源自網(wǎng)吧系統(tǒng)維護-http://www.strong-digital.cn/7796.html

* 注 1:(引用網(wǎng)上內(nèi)容)如果在 rc.local中添加路由會造成NFS無法自動掛載問題,所以使用 static-routes 的方法是最好的。無論重啟系統(tǒng)和service network restart 都會生效,按照linux啟動的順序,rc.local 里面的內(nèi)容是在linux所有服務(wù)都啟動完畢,最后才被執(zhí)行的,也就是說,這里面的內(nèi)容是在netfs之后才被執(zhí)行的,那也就是說在netfs啟動的時候,服務(wù)器上的靜態(tài)路由是沒有被添加的,所以netfs掛載不能成功。文章源自網(wǎng)吧系統(tǒng)維護-http://www.strong-digital.cn/7796.html

* 注 2:以上添加命令可以通過 linux 命令 sed -i '2i route add -net 192.168.2.0/24 gw 192.168.2.254' + 文件路徑 命令向相應(yīng)的文件的第二行添加命令或者用 echo route add -net 192.168.2.0/24 gw 192.168.2.254 >> 文件路徑向文件的末尾添加命令文章源自網(wǎng)吧系統(tǒng)維護-http://www.strong-digital.cn/7796.html

二、windows 添加永久靜態(tài)路由

方法一:

范圍: 測試 windows2008 系統(tǒng)可用

使用方法:

1.以管理員身份 打開運行 cmd.exe

2.使用命令:route -p add 192.168.2.0 mask 255.255.255.0 192.168.2.1 (服務(wù)器的默認路由為192.168.2.1)

3.使用 route print 查看命令是否生效

版權(quán)聲明:文章圖片資源來源于網(wǎng)絡(luò),如有侵權(quán),請留言刪除!!!
廣告也精彩
admin
  • 本文由 發(fā)表于 2021年9月29日 09:44:15
  • 轉(zhuǎn)載請務(wù)必保留本文鏈接:http://www.strong-digital.cn/7796.html
匿名

發(fā)表評論

匿名網(wǎng)友 填寫信息

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen: