CentOSなどRedhat系linuxでのルーティングテーブルの設定方法
色説明 |
コメント |
設定/書き換え点 |
ターミナル入力 |
○設定の確認
# route -n
Kernel IP routing table
Destination Gateway Genmask Flags Metric Ref Use Iface
192.168.1.0 0.0.0.0 255.255.255.0 U 0 0 0 eth0
169.254.0.0 0.0.0.0 255.255.0.0 U 0 0 0 eth0
0.0.0.0 192.168.1.1 0.0.0.0 UG 0 0 0 eth0
routeだけでも表示されますが、-nを付けると名前を引かないです。○設定を追加する
※追加例は192.168.2.0/24(192.168.2.0/255.255.255.0)のgatewayを192.168.1.254に設定するものです。
# route add -net 192.168.2.0 netmask 255.255.255.0 gw 192.168.1.254 または、 # route add -net 192.168.2.0/24 gw 192.168.1.254※2種類書きましたが、下はsubnetmaskをCIDR表記しただけですね。
# route -n Kernel IP routing table Destination Gateway Genmask Flags Metric Ref Use Iface 192.168.2.0 192.168.1.254 255.255.255.0 UG 0 0 0 eth0 ←設定が追加された 192.168.1.0 0.0.0.0 255.255.255.0 U 0 0 0 eth0 169.254.0.0 0.0.0.0 255.255.0.0 U 0 0 0 eth0 0.0.0.0 192.168.1.1 0.0.0.0 UG 0 0 0 eth0
宛先 via ゲートウェイ
では実際に作成 # vi /etc/sysconfig/network-scripts/route-eth0
192.168.2.0/24 via 192.168.1.254
では反映(ネットワーク再起動)&確認
# /etc/rc.d/init.d/network restart インターフェース eth0 を終了中: [ OK ] ループバックインターフェースを終了中 [ OK ] ループバックインターフェイスを呼び込み中 [ OK ] インターフェース eth0 を活性化中: [ OK ] # route -n Kernel IP routing table Destination Gateway Genmask Flags Metric Ref Use Iface 192.168.2.0 192.168.1.254 255.255.255.0 UG 0 0 0 eth0 192.168.1.0 0.0.0.0 255.255.255.0 U 0 0 0 eth0 169.254.0.0 0.0.0.0 255.255.0.0 U 0 0 0 eth0 0.0.0.0 192.168.1.1 0.0.0.0 UG 0 0 0 eth0※eth0の例ですが、bondingドライバを使っている場合は以下のファイルになります
# vi /etc/rc.local
route add -net 192.168.2.0 netmask 255.255.255.0 gw 192.168.1.254
確かにOS起動時に追加されますが、ネットワークをリスタートすると消えてしまいます
# route -n Kernel IP routing table Destination Gateway Genmask Flags Metric Ref Use Iface 192.168.2.0 192.168.1.254 255.255.255.0 UG 0 0 0 eth0 192.168.1.0 0.0.0.0 255.255.255.0 U 0 0 0 eth0 169.254.0.0 0.0.0.0 255.255.0.0 U 0 0 0 eth0 0.0.0.0 192.168.1.1 0.0.0.0 UG 0 0 0 eth0 # /etc/rc.d/init.d/network restart インターフェース eth0 を終了中: [ OK ] ループバックインターフェースを終了中 [ OK ] ループバックインターフェイスを呼び込み中 [ OK ] インターフェース eth0 を活性化中: [ OK ] # route -n Kernel IP routing table Destination Gateway Genmask Flags Metric Ref Use Iface 192.168.1.0 0.0.0.0 255.255.255.0 U 0 0 0 eth0 169.254.0.0 0.0.0.0 255.255.0.0 U 0 0 0 eth0 0.0.0.0 192.168.1.1 0.0.0.0 UG 0 0 0 eth0※起動のみ適応というのも意味を持たないと思うのでお勧めしません。
○設定を削除する
# route del -net 192.168.2.0/24
以下、若干脱線気味のところ
# cat /etc/sysconfig/network-scripts/ifup-routes#! /bin/bash
#
# adds static routes which go through device $1
if [ -z "$1" ]; then
echo $"usage: ifup-routes <net-device> [<nickname>]"
exit 1
fi
handle_file () {
. $1
routenum=0
while [ "x$(eval echo '$'ADDRESS$routenum)x" != "xx" ]; do
eval `ipcalc -p $(eval echo '$'ADDRESS$routenum) $(eval echo '$'NETMASK$routenum)`
line="$(eval echo '$'ADDRESS$routenum)/$PREFIX"
if [ "x$(eval echo '$'GATEWAY$routenum)x" != "xx" ]; then
line="$line via $(eval echo '$'GATEWAY$routenum)"
fi
line="$line dev $2"
/sbin/ip route add $line
routenum=$(($routenum+1))
done
}
FILES="/etc/sysconfig/network-scripts/route-$1"
if [ -n "$2" -a "$2" != "$1" ]; then
FILES="$FILES /etc/sysconfig/network-scripts/route-$2"
fi
for file in $FILES; do
if [ -f "$file" ]; then
if egrep -q '^[[:space:]]*ADDRESS[0-9]+=' $file ; then
# new format
handle_file $file ${1%:*}
else
# older format
{ cat "$file" ; echo ; } | while read line; do
if [[ ! "$line" =~ '^[[:space:]]*(\#.*)?$' ]]; then
/sbin/ip route add $line
fi
done
fi
fi
done
# Red Hat network configuration format
NICK=${2:-$1}
CONFIG="/etc/sysconfig/network-scripts/$NICK.route"
[ -f $CONFIG ] && handle_file $CONFIG $1
# Routing rules
FILES="/etc/sysconfig/network-scripts/rule-$1"
if [ -n "$2" -a "$2" != "$1" ]; then
FILES="$FILES /etc/sysconfig/network-scripts/rule-$2"
fi
for file in $FILES; do
if [ -f "$file" ]; then
{ cat "$file" ; echo ; } | while read line; do
if [[ ! "$line" =~ '^[[:space:]]*(\#.*)?$' ]]; then
/sbin/ip rule add $line
fi
done
fi
done
あまりにも長いので必要な部分抜粋
FILES="/etc/sysconfig/network-scripts/route-$1"と、言うことは
/etc/sysconfig/network-scripts/route-eth0等でいけるらしい。