Linux教程網
Linux教程網
Linux教程網
您现在的位置: Linux教程網 >> UnixLinux >  >> Linux綜合 >> Linux資訊 >> Linux文化

用 iproute2 設定路由後,該如何讓設定永久保存?

很多管理員用 ip route add 這類 iproute2 指令新增路由後,會發現一重開機設定就消失了。原因很簡單:這些指令只作用於目前執行中的核心路由表,並不會寫入任何設定檔。

通用做法:寫成開機腳本

一種跨發行版都適用的思路,是在 /etc/init.d/ 底下建立一支腳本,例如名為 static.route:

#!/bin/sh
IP=/sbin/ip
$IP route add ip1 via gw
$IP route add ip2 via gw

接著賦予執行權限 chmod +x static.route,再到對應的執行等級目錄(如 /etc/rc?.d)建立啟動連結:

ln -s /etc/init.d/static.route /etc/rc5.d/S99static.route

如此一來,開機時就會自動套用這些靜態路由。要注意的是,有些發行版並沒有 /etc/init.d,而是放在 /etc/rc.d/init.d,因此不能死搬硬套。

各發行版的原生方式

RedHat 系列可在 /etc/sysconfig/network-scripts/ 下,用 route-eth0、route-eth1 等檔案為每張網卡分別設定靜態路由;Debian 系列則可直接在 /etc/network/interfaces 中宣告。相較於通用腳本,這些原生方式更貼合各自的系統管理邏輯,也更值得優先採用。

Copyright © Linux教程網 All Rights Reserved