微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

Linux上的持久性ip规则(Redhat)

如何在 Linux上配置持久性ip规则(特别是基于Redhat的发行版)?有内置方法吗?我唯一的选择是添加到/etc/rc.d/rc.local还是创建自己的rc.d脚本?

编辑:为了澄清我不是指iptables而是ip工具(我认为很多人都不熟悉).在任何情况下,我试图保持的规则添加以下命令:

# ip rule add fwmark 1 lookup 100
# ip rule
...
32765: from all fwmark 0x1 lookup 100
...

我发现这样做的唯一参考来自Novell:http://www.novell.com/support/viewContent.do?externalId=7008874&sliceId=1,它建议创建一个rc.d脚本

解决方法

按照惯例,我在询问后不久就偶然发现了自己问题的答案:)在 http://grokbase.com/t/centos/centos/099bmc07mq/persisting-iproute2-routes-and-rules找到答案

在Redhat 5上,/ etc / sysconfig / network-scripts / ifup-routes脚本处理rule- *文件.相关代码如下:

# 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" =~ $MATCH ]]; then
           /sbin/ip rule add $line
       fi
       done
   fi
done

RHEL 6.5的脚本(可能是较旧的6):

# Routing rules
FILES="/etc/sysconfig/network-scripts/rule-$1 /etc/sysconfig/network-scripts/rule6-$1"
if [ -n "$2" -a "$2" != "$1" ]; then
FILES="$FILES /etc/sysconfig/network-scripts/rule-$2 /etc/sysconfig/network-scripts/rule6-$2"
fi

for file in $FILES; do
   if [ -f "$file" ]; then
       handle_ip_file $file
   fi
done

handle_ip_file() {
    local f t type= file=$1 proto="-4"
    f=${file##*/}
    t=${f%%-*}
    type=${t%%6}
    if [ "$type" != "$t" ]; then
        proto="-6"
    fi
    { cat "$file" ; echo ; } | while read line; do
        if [[ ! "$line" =~ $MATCH ]]; then
            /sbin/ip $proto $type add $line
        fi
    done
}

原文地址:https://www.jb51.cc/linux/400901.html

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。

相关推荐