靜態路由由管理員手動定義,可強制流量走特定閘道,增強網路安全與可控性。本文說明 route add 臨時新增、寫入 /etc/rc.local 開機自動載入、以及建立 /etc/sysconfig/static-routes 永久生效的完整方法。
route add default gw 10.0.0.1
開機自動載入:
echo "route add default gw 10.0.0.1" >> /etc/rc.local
route add -net 192.168.2.0/24 gw 192.168.2.254
永久生效(需手動建立檔案):
echo "any net 192.168.2.0/24 gw 192.168.2.254" >> /etc/sysconfig/static-routes
route add -host 192.168.2.2 gw 192.168.2.254
永久生效:
echo "any host 192.168.2.2 gw 192.168.2.254" >> /etc/sysconfig/static-routes
/etc/sysconfig/static-routes 預設不存在,需手動建立。視需求選擇臨時或永久方式,生產環境建議寫入 static-routes 確保重開機後路由依然生效。