[LINUX] 如何像 Windows 上的 "route add -p" command 來建立 persistent static routes?

今天同事問了我個問題,在 LINUX 上要如何像 Windows 上的 "route add -p" command 來建立 persistent static routes?這個部分簡單說有兩種方式可以達成:

首先,今天的測試 Server 一樣為 HP DL-380 G4 上,安裝 RHEL ES/AS 3 的版本來做示範,目前的環境為:

RHAS3 # ifconfig -a
eth0 Link encap:Ethernet HWaddr 00:0E:7F:F2:2B:B1
inet addr:202.39.120.51 Bcast:202.39.120.255 Mask:255.255.255.0
UP BROADCAST RUNNING MULTICAST MTU:1500 Metric:1
RX packets:6707518 errors:0 dropped:0 overruns:0 frame:0
TX packets:172226 errors:0 dropped:0 overruns:0 carrier:0
collisions:0 txqueuelen:1000
RX bytes:844853502 (805.7 Mb) TX bytes:12185954 (11.6 Mb)
Interrupt:25

eth1 Link encap:Ethernet HWaddr 00:0E:7F:F2:2B:B0
inet addr:172.17.65.3 Bcast:172.17.65.255 Mask:255.255.255.0
UP BROADCAST MULTICAST MTU:1500 Metric:1
RX packets:0 errors:0 dropped:0 overruns:0 frame:0
TX packets:0 errors:0 dropped:0 overruns:0 carrier:0
collisions:0 txqueuelen:1000
RX bytes:0 (0.0 b) TX bytes:0 (0.0 b)
Interrupt:26

eth1:0 Link encap:Ethernet HWaddr 00:0E:7F:F2:2B:B0
inet addr:192.168.10.1 Bcast:192.168.10.255 Mask:255.255.255.0
UP BROADCAST MULTICAST MTU:1500 Metric:1
RX packets:0 errors:0 dropped:0 overruns:0 frame:0
TX packets:0 errors:0 dropped:0 overruns:0 carrier:0
collisions:0 txqueuelen:1000
RX bytes:0 (0.0 b) TX bytes:0 (0.0 b)
Interrupt:26

lo Link encap:Local Loopback
inet addr:127.0.0.1 Mask:255.0.0.0
UP LOOPBACK RUNNING MTU:16436 Metric:1
RX packets:772080 errors:0 dropped:0 overruns:0 frame:0
TX packets:772080 errors:0 dropped:0 overruns:0 carrier:0
collisions:0 txqueuelen:0
RX bytes:60976074 (58.1 Mb) TX bytes:60976074 (58.1 Mb)
RHAS3 # netstat -nr
Kernel IP routing table
Destination Gateway Genmask Flags MSS Window irtt Iface
202.39.120.0 0.0.0.0 255.255.255.0 U 0 0 0 eth0
192.168.10.0 0.0.0.0 255.255.255.0 U 0 0 0 eth1
172.17.65.0 0.0.0.0 255.255.255.0 U 0 0 0 eth1
169.254.0.0 0.0.0.0 255.255.0.0 U 0 0 0 eth1
127.0.0.0 0.0.0.0 255.0.0.0 U 0 0 0 lo
0.0.0.0 202.39.120.1 0.0.0.0 UG 0 0 0 eth0

第一種方式:用 route add -net 把要加的 routing 加在 /etc/rc.d/rc.local 裡面,Server 開機帶起所有的網卡之後,便會去把 /etc/rc.d/rc.local 所列的 routing 都加進去...
舉例來說,我想加上一個 172.17.64.0 的網段 route 經 eth1 出去,我可以在 /etc/rc.d/rc.local 加入下面這一行:
route add -net 172.17.64.0 netmask 255.255.255.0 gw 172.17.64.1 eth1
這樣 Server 開起來之後就 ok 了。
不過這種方式有個缺點,那就是網路如果 Restart 過的話,所有開機加進去的 Static routes 都會不見了,因此在這裡建議用下面的方式:

第二種方式:建立 /etc/sysconfig/network-scripts/route-ethx (這個 x 代表你想加 Static route 的 device,在這裡我用 eth1 來做示範...)
在 /etc/sysconfig/network-scripts/route-eth1 裡面加入下面這一行:
172.17.64.0/24 via 172.17.65.1 dev eth1
就是去指定一個 172.17.64.0 C-Class 的 Subnet 經由 eth1 網卡, Gateway 為 172.17.65.1 。
然後用 "service network restart" 把網路重起後,再用 netstat -nr 應該就會看到如下面的 Routing table:

RHAS3 # service network restart
Shutting down interface eth0: [ OK ]
Shutting down interface eth1: [ OK ]
Shutting down loopback interface: [ OK ]
Setting network parameters: [ OK ]
Bringing up loopback interface: [ OK ]
Bringing up interface eth0: [ OK ]
Bringing up interface eth1: [ OK ]
RHAS3 # netstat -nr
Kernel IP routing table
Destination Gateway Genmask Flags MSS Window irtt Iface
202.39.120.0 0.0.0.0 255.255.255.0 U 0 0 0 eth0
172.17.64.0 172.17.65.1 255.255.255.0 UG 0 0 0 eth1
192.168.10.0 0.0.0.0 255.255.255.0 U 0 0 0 eth1
172.17.65.0 0.0.0.0 255.255.255.0 U 0 0 0 eth1
169.254.0.0 0.0.0.0 255.255.0.0 U 0 0 0 eth1
127.0.0.0 0.0.0.0 255.0.0.0 U 0 0 0 lo
0.0.0.0 202.39.120.1 0.0.0.0 UG 0 0 0 eth0

Reference URL :
How do I create persistent static routes in Red Hat Enterprise Linux 4?

好了,報告完畢。
0 Responses