ルーティングテーブルの設定
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
- 恒久的に経路を追加する
※OS起動時に追加する
記述方法は以下の通り
宛先 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ドライバを使っている場合は以下のファイルになります
/etc/sysconfig/network-scripts/route-bond0
まぁ、bondingドライバ使われてる方はそれぐらい見ればわかりますよねw
eth1なら、もちろん次になります。
/etc/sysconfig/network-scripts/route-eth1 - 恒久的に経路を追加する。その2
※OS起動時から適応する(手抜き)# 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
以下、若干脱線気味のところ
- route周りのスクリプトファイルの中身を見てみる。
# 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等でいけるらしい。
パッケージの管理 |
CentOS |
swap領域拡張 |