1. 程式人生 > >rhel7如何添加永久靜態路由

rhel7如何添加永久靜態路由

靜態路由 rhel7

一:使用route 命令添加

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

例如:

//添加到主機的路由

# route add –host 11.11.1.11 dev eth0

# route add –host 11.11.1.11 gw 11.11.1.1

//添加到網絡的路由

# route add –net 11.11.1.11 netmask 255.255.255.0 dev eth0

# route add –net 11.11.1.11 netmask 255.255.255.0 gw 11.11.1.1

# route add –net 11.11.1.0/24 dev eth1

//添加默認網關

# route add default gw 11.11.2.1

//刪除路由

# route del –host 11.11.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

格式如下:

ip route

(default via gateway dev interface)

(ip/netmask via gateway dev interface)

二:在linux下設置永久路由的方法:

1.在/etc/rc.local裏添加

方法:

route add -net 192.168.3.0/24 dev eth0

route add -net 192.168.2.0/24 gw 192.168.2.254

註:如果在rc.local中添加路由會造成NFS無法自動掛載問題,可以使用/etc/sysconfig/static-routes的方法。無論重啟系統和service network restart 都會生效。

NFS問題的描述:

按照linux啟動的順序,rc.local裏面的內容是在linux所有服務都啟動完畢,最後才被執行的,也就是說,這裏面的內容是在NFS之後才被執行的,那也就是說在NFS啟動的時候,服務器上的靜態路由是沒有被添加的,所以NFS掛載不能成功。

2.在/etc/sysconfig/network裏添加到末尾,是全局生效的。(有效的方法

方法:

GATEWAY=gw-ip

或者

GATEWAY=gw-dev

3./etc/sysconfig/static-routes :

any net 192.168.3.0/24 gw 192.168.3.254

any net 10.30.27.128 netmask 255.255.255.192 gw 10.30.27.129


4、在/etc/sysconfig/network-script/route-interface下添加路由(每個接口一個文件,如果沒有就創建一個,只能添加針對該接口的路由)

格式如下:

network/prefix via gateway dev intf

例如給eth0添加一個默認網關:

vim /etc/sysconfig/network-scripts/route-eth0

#添加如下內容(可以省略dev eth0)

0.0.0.0/0 via 11.11.10.2 dev eth0

ps:註意這裏的掩碼是0而不是32,因為這裏是網段而不是路由。

保存退出後,service network restart或systemctl restart network。

使用route -n或netstat -r查看路由表。

[[email protected] ~]# route -n
Kernel IP routing table
Destination Gateway Genmask Flags Metric Ref Use Iface
11.11.10.0 0.0.0.0 255.255.255.0 U 0 0 0 eth0
192.168.122.0 0.0.0.0 255.255.255.0 U 0 0 0 virbr0
169.254.0.0 0.0.0.0 255.255.0.0 U 1002 0 0 eth0
0.0.0.0 11.11.10.2 0.0.0.0 UG 0 0 0 eth0

默認路由已經被添加到路由表裏面了。

註意如果有兩塊網卡,需要設置默認路由才能訪問internet。

所有添加靜態路由的方法在rhel7上面均經過驗證,正確無誤。


本文出自 “滴水穿石孫傑” 博客,請務必保留此出處http://xjsunjie.blog.51cto.com/999372/1928908

rhel7如何添加永久靜態路由