centos - run script after interface up

最後更新: 2015-08-10

Making the route permanent

 

在 Centos 6 上設定 permanent route 及 行 script 的方法

 

network 與 route-ethX 的分別

 

# set default gateway IP address.

/etc/sysconfig/network

# set additional static gateway IP address.

/etc/sysconfig/network-scripts/route-ethX

"route-eth0" Example

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

188.165.229.254 dev eth0
default via 188.165.229.254 dev eth0

Service network restart

 

"service network restart" 是靠 "/sbin/ifdown" 及 "/sbin/ifup" 去實現 restart 的

它們會執行 "/etc/sysconfig/network-scripts/" 內有關的 scripts

部份有關的 scripts 如下

ifup-routes (file: route-ethX)

Adds static routes for a device as its interface is brought up.

ifup-post

它是在 interface "up" 後會行的 script

...................

if [ -x /sbin/ifup-local ]; then
    /sbin/ifup-local ${DEVICE}
fi

/sbin/ifup-local

自定某 interface "up" 後自己想行的 script

#!/bin/bash
if [[ "$1" == "eth0:1" ]]
then
  echo "this part will be executed right after eth0:1 is up."
  echo "so you can put any startup command for eth0 here"
else
  #DO_NOTHING
  echo -n ""
fi

 


Dual Gateway Example

 

/sbin/ifup-local

...................
ip route add 192.168.88.0/24 dev eth0:1 src 192.168.88.184 table rt2
ip route add default via 192.168.88.5 dev eth0:1 table rt2
ip rule add from 192.168.88.184/32 table rt2
ip rule add to 192.168.88.184/32 table rt2
...................

/etc/sysconfig/network-scripts/ifcfg-eth0:1

iface eth0:1 inet static
    address 10.10.0.10
    netmask 255.255.255.0
    post-up ip route add 192.168.88.0/24 dev eth0 src 192.168.88.184 table rt2
    post-up ip route add default via 192.168.88.5 dev eth0 table rt2
    post-up ip rule add from 192.168.88.184/32 table rt2
    post-up ip rule add to 192.168.88.184/32 table rt2

OR

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

192.168.88.0/24 dev eth0:1 src 192.168.88.184 table rt2
default via 192.168.88.5 dev eth0:1 table rt2
from 192.168.88.184/24 table rt2
to 192.168.88.184 table rt2

 

 

 

 

Creative Commons license icon Creative Commons license icon