Linux上的持久ip规则(Redhat)

如何在Linux上configuration持久性ip rule (特别是基于Redhat的发行版)? 有没有内置的方法? 我唯一的select是添加到/etc/rc.d/rc.local还是创build我自己的rc.d脚本?

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

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

我发现这样做的唯一参考来自Novell: http : rc.d =1build议创build一个rc.d脚本

按照习惯,我偶然发现自己问题的答案:)在http://grokbase.com/t/centos/centos/099bmc07mq/persisting-iproute2-routes-and-rules上find答案

在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 ft 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 } 

以上是3/4的答案 – 缺less的是如何格式化/ etc / sysconf / network-scripts / rule-ethX文件。 您还需要将路由表添加到/ etc / iproute2 / rt_tables:

 # add a line with a table identifier and name: 100 ISPname 

并添加规则文件/ etc / sysconfig / network-scripts / rule-eth0:

 # rule-eth0 from 1.2.3.4/24 table {table name from /etc/iproute2/rt_tables} to 1.2.3.4/24 table {table name from /etc/iproute2/rt_tables} 

请注意,表名必须匹配,并区分大小写。

请注意,如果您在这些规则文件中使用任何规则的优先级,则必须使用所有规则的优先级。 否则,没有任何优先级的那些都被添加到优先级0链中。