• ubuntu 14.04/14.10 iptables 防火墙设置


    1. 一键批处理设置

        
    1. #!/bin/bash  
    2. PATH=/bin:/sbin:/usr/bin:/usr/sbin:/usr/local/bin:/usr/local/sbin:/root/bin:~/bin  
    3. export PATH  
    4. # Check if user is root  
    5. if [ $UID != 0 ]; then echo "Error: You must be root to run the install script, please use root to install lanmps";exit;fi  
    6.   
    7. iptables-save >> _.iptables.up.rules #保存防火墙设置,以便没保存时使用  
    8. iptables -L -n 2>&1 | tee -a "_.iptables.log"  
    9. iptables -F        #清除预设表filter中的所有规则链的规则  
    10. iptables -X        #清除预设表filter中使用者自定链中的规则  
    11. iptables -Z        #计数器清零  
    12.   
    13. iptables -P INPUT DROP  
    14. iptables -P OUTPUT ACCEPT  
    15. iptables -P FORWARD ACCEPT  
    16.   
    17. #双向  
    18. iptables -A INPUT -m state --state ESTABLISHED,RELATED -j ACCEPT  
    19. #允许本机  
    20. iptables -A INPUT -i lo -j ACCEPT  
    21. #FTP  
    22. iptables -A INPUT -p tcp --dport 21 -j ACCEPT  
    23. #SSH  
    24. iptables -A INPUT -p tcp --dport 22 -j ACCEPT  
    25. #www 80  
    26. iptables -A INPUT -p tcp --dport 80 -j ACCEPT  
    27.   
    28. #13306 映射转发到  mysql数据库 3306  
    29. iptables -A PREROUTING -p tcp --dport 13306 -j REDIRECT --to-ports 3306 -t nat  
    30. #3306 mysql数据库  
    31. #iptables -A INPUT -p tcp --dport 3306 -j ACCEPT  
    32. #memache  
    33. #iptables -A INPUT -p tcp --dport 11211 -j ACCEPT  
    34.   
    35. #对于OUTPUT规则,因为预设的是ACCEPT,所以要添加DROP规则,减少不安全的端口链接。  
    36. iptables -A OUTPUT -p tcp --sport 31337 -j DROP  
    37. iptables -A OUTPUT -p tcp --dport 31337 -j DROP  
    38.   
    39. #丢弃坏的TCP包  
    40. iptables -A FORWARD -p tcp ! --syn -m state --state NEW -j DROP  
    41. #处理IP碎片数量,防止攻击,允许每秒100个  
    42. #iptables -A FORWARD -f -m limit --limit 100/s --limit-burst 100 -j ACCEPT  
    43.   
    44. #设置ICMP包过滤,允许每秒1个包,限制触发条件是10个包  
    45. #iptables -A FORWARD -p icmp -m limit --limit 1/s --limit-burst 10 -j ACCEPT  
    46.   
    47. #防止外部的ping和SYN洪水攻击  
    48. iptables -A INPUT -p tcp --syn -m limit --limit 100/s --limit-burst 100 -j  ACCEPT  
    49. #ping洪水攻击,限制每秒的ping包不超过10个  
    50. iptables -A INPUT -p icmp --icmp-type echo-request -m limit --limit 1/s –limit-burst 10 -j ACCEPT  
    51. #防止各种端口扫描,将SYN及ACK SYN限制为每秒钟不超过200个  
    52. iptables -A INPUT -p tcp -m tcp --tcp-flags SYN,RST,ACK SYN -m limit --limit 20/sec --limit-burst 200 -j ACCEPT  
    53.   
    54. #最后规则拒绝所有不符合以上所有的  
    55. iptables -A INPUT -j DROP  
    56.   
    57. if [ -z "`grep "iptables-save" /etc/network/interfaces`" ]  
    58. then  
    59.     echo "#以下有防火墙需要的可以使用    
    60. pre-up iptables-restore < /etc/iptables.up.rules #启动时应用防火墙    
    61. post-down iptables-save > /etc/iptables.up.rules #关闭时保存防火墙设置,以便下次启动时使用  " >> /etc/network/interfaces  
    62.       
    63. else  
    64.      echo "iptables-save find "  
    65. fi  
    66.   
    67. clear  
    68. echo "iptables ok ";  
    69. echo ""  
    70.   
    71. iptables -L -n  
    72. cat /etc/network/interfaces  


    把上面的保存为 (如果还有其他端口规则请一起在上面配置,执行时清空规则):
    1. lanmps_iptables.sh  

    上传到服务器然后设置权限,并执行

    1. chmod 777 lanmps_iptables.sh   
    2. ./lanmps_iptables.sh  
    那么  防火墙就设置完成了

    2. ubuntu iptables 防火墙 启动

    1. modprobe ip_tables  

    3. ubuntu iptables 防火墙 关闭

    ubuntu 并没有关闭命令,所以要通过变通方法解决防火墙

    1. iptables -F  
    2. iptables -X    
    3. iptables -Z    
    4. iptables -P INPUT ACCEPT    
    5. iptables -P OUTPUT ACCEPT    
    6. iptables -P FORWARD ACCEPT    
    7. modprobe -r ip_tables    
    8. 依次执行以上命令即可关闭iptables,否则在执行modproble -r ip_tables时将会提示  FATAL: Module ip_tables is in use.  

    4. Iptables的保存和调用

    防止每次开机或重启后都需要去调用一次,把它设置自动执行

    第一步 更改网卡配置文件

    1. sudo vi /etc/network/interfaces   
    第二部 在最后增加配置

    1. #以下有防火墙需要的可以使用    
    2. pre-up iptables-restore < /etc/iptables.up.rules #启动时应用防火墙    
    3. post-down iptables-save > /etc/iptables.up.rules #关闭时保存防火墙设置,以便下次启动时使用    
    如果不明白的看 http://blog.csdn.net/fenglailea/article/details/12191607#t1

    感谢:http://hongwei.im/iptables-setting-for-ubuntu-1304



来自为知笔记(Wiz)


  • 相关阅读:
    备胎的养成记KeepAlived实现热备负载
    入坑系列之HAProxy负载均衡
    将Error异常日志从普通日志中剥离
    一步一步在Windows中使用MyCat负载均衡 下篇
    年终的第一篇总结 结束南漂 写在2017
    Android实现TCP断点上传,后台C#服务实现接收
    为什么我会反对大家写工作日报
    ANSI C、ISO C、Standard C联系与区别
    c、c++ char*和wchar*互相转换
    宽字符与Unicode (c语言 汉语字符串长度)
  • 原文地址:https://www.cnblogs.com/jins-note/p/9513101.html
  • Copyright © 2020-2023  润新知