[原创] 在高通MDM9607设备上配置iptables服务

2017-12-08  本文已影响0人  赵国开

1 Iptables简介


2 内核配置


$ bitbake -c menuconfig linux-quic
→ Networking support → Networking options → Network packet filtering framework (Netfilter) 

Netfilter Xtables support (required for ip_tables)
选上(默认已选上),有了它,你才能使用过滤、伪装、NAT。它 为内核加入了iptables标识框架。没有它,iptables毫无作用。

3 在设备上配置Iptables服务


Iptables的最新源码可以去www.netfilter.org 官网下载。不过设备上也有安装iptables的工具,经过测试是可以使用的,由于时间关系没有下载最新的iptables源码进行编译测试。

# which iptables
/usr/sbin/iptables

1)把启动脚本和服务脚本放到/etc/init.d目录

$adb push iptables-start.sh /etc/init.d/.
$adb push iptables.sh /etc/init.d/.

2)配置启动脚本
查看运行等级

# runlevel
N 5

说明启动时会从 rc5.d目录下按顺序去启动脚本
在rc5.d目录建立启动链接

#cd cd /etc/rc5.d
#ln -s ../init.d/iptables-start.sh S99start_iptables

备注:下面是我写好的iptables的启动脚本和服务脚本

#!/bin/sh
# copyright (c) 2017-2027, xiamen yaxon. All rights reserved.  
# iptables      Start iptables firewall     
#     
# runlevel 5 in  S99     
# description:  starts

/etc/init.d/iptables.sh start
#!/bin/sh
# copyright (c) 2017-2027, xiamen yaxon. All rights reserved.  
# iptables      {start|stop|restart|save}     
#         
# description:  starts, stops and restart  

IPTABLES_DIR='/etc/sysconfig'
IPTABLES_RULE='/etc/sysconfig/iptables'
IPTABLES_ERROR_CODE=3


start() {     
    #如果/etc/sysconfig/iptables不存在,返回值出错码   
    [ ! -f "$IPTABLES_RULE" ] && return $IPTABLES_ERROR_CODE          

    echo "get firewall rules"  
    
    #清除之前的规则
    iptables -F
    iptables -X
    iptables -Z

    #恢复之前配置的防火墙规则  
    iptables-restore < ${IPTABLES_RULE}
    if [ $? -eq 0 ]; then     
        echo "restore firewall rules success"     
        else  
        echo "restore firewall rules failure"; return $IPTABLES_ERROR_CODE     
    fi  
}   

save() {        

    if [ ! -d "$IPTABLES_DIR" ]; then     
        mkdir -p $IPTABLES_DIR    
    fi  
 
    iptables-save > ${IPTABLES_RULE}
    if [ $? -eq 0 ]; then     
        echo "save firewall rules success"     
        else  
        echo "save firewall rules failure"; return $IPTABLES_ERROR_CODE     
    fi  
} 

stop() { 
    #暂时不处理
    echo "stop firewall success"  
}

restart() {       
    stop; 
    start;   
}  

case "$1" in  
    start)        
        start     
        RETVAL=$?     
        ;;  
    save)        
        save     
        RETVAL=$?     
        ;;    
    stop)   
        stop     
        RETVAL=$?     
        ;; 
    restart)     
        restart     
        RETVAL=$?     
        ;;          
    *)     
        echo $"Usage: $0 {start|stop|restart|save}"  
        RETVAL=$?      
        ;;     
esac     

exit $RETVAL 

3)iptables的规则配置
可以使用iptables工具进行配置,具体配置规则可以参考https://www.frozentux.net/iptables-tutorial/cn/iptables-tutorial-cn-1.1.19.html#INCLUDE.RCFIREWALL
可以根据实际需要进行选择配置,这里不再介绍。
4)iptables.sh脚本的使用
启动,iptables的规则不会自动保存,需要每次系统运行时重新加载这些规则,下面的命令可以重新去加载之前配置的规则

#/etc/init.d/iptables.sh start

重启,如果想放弃正在配置的规则可以用下面的命令

#/etc/init.d/iptables.sh restart

存储,在使用iptables进行配置完规则,可以用下面的指令存储规则,因为系统不会自动保存配置规则,重启之后配置的规则会丢掉,所以配置完需要存储一下规则才会保存下来

#/etc/init.d/iptables.sh save

参考文献


[1] https://www.frozentux.net/iptables-tutorial/cn/iptables-tutorial-cn-1.1.19.html#INCLUDE.RCFIREWALL
[2] http://www.netfilter.org/projects/conntrack-tools/index.html
[3] http://www.faqs.org/docs/iptables/
[4] 鸟哥的linux私房菜-服务器(第三版/第九章)

上一篇 下一篇

猜你喜欢

热点阅读