zoukankan      html  css  js  c++  java
  • Ubuntu 14.04 配置iptables防火墙

    Ubuntu 14.04 配置iptables防火墙

    ----------------------- 配置防火墙,开启80端口、3306端口 ---- start -----------------------
    # 说明:Ubuntu默认安装是没有开启任何防火墙的,为了服务器的安全,建议大家安装启用防火墙设置,这里推荐使用iptables防火墙.如果mysql本地使用,可以不用打开3306端口.

    whereis iptables #查看系统是否安装防火墙
    # 显示:iptables: /sbin/iptables /usr/share/iptables /usr/share/man/man8/iptables.8.gz
    # 表示已经安装 iptables
    # apt-get install iptables #如果默认没有安装,请运行此命令安装防火墙

    iptables -L #查看防火墙配置信息,显示如下:
    ######################################
    Chain INPUT (policy ACCEPT)
    target prot opt source destination

    Chain FORWARD (policy ACCEPT)
    target prot opt source destination

    Chain OUTPUT (policy ACCEPT)
    target prot opt source destination
    ######################################
    可以看到Linux中都有的3个常用默认链(INPUT、OUTPUT和FORWARD),同时也可以看到每个链的缺省策略(每个链对默认策略都是接受),在此我们可以看到Ubuntu中并没有添加任何默认规则集。

    # 1. 一键批处理设置
    vi lanmps_iptables.sh 保存一下内容(如果还有其他端口规则请一起在上面配置,执行时清空规则):
    ##################################################################################################################
    #!/bin/bash
    PATH=/bin:/sbin:/usr/bin:/usr/sbin:/usr/local/bin:/usr/local/sbin:/root/bin:~/bin
    export PATH
    # Check if user is root
    if [ $UID != 0 ]; then echo "Error: You must be root to run the install script, please use root to install lanmps";exit;fi

    iptables-save >> _.iptables.up.rules #保存防火墙设置,以便没保存时使用
    iptables -L -n 2>&1 | tee -a "_.iptables.log"
    iptables -F #清除预设表filter中的所有规则链的规则
    iptables -X #清除预设表filter中使用者自定链中的规则
    iptables -Z #计数器清零

    iptables -P INPUT DROP
    iptables -P OUTPUT ACCEPT
    iptables -P FORWARD ACCEPT

    #双向
    iptables -A INPUT -m state --state ESTABLISHED,RELATED -j ACCEPT
    #允许本机
    iptables -A INPUT -i lo -j ACCEPT
    #FTP
    iptables -A INPUT -p tcp --dport 21 -j ACCEPT
    #SSH
    iptables -A INPUT -p tcp --dport 22 -j ACCEPT
    #www 80
    iptables -A INPUT -p tcp --dport 80 -j ACCEPT

    #13306 映射转发到 mysql数据库 3306
    iptables -A PREROUTING -p tcp --dport 13306 -j REDIRECT --to-ports 3306 -t nat
    #3306 mysql数据库
    #iptables -A INPUT -p tcp --dport 3306 -j ACCEPT
    #memache
    #iptables -A INPUT -p tcp --dport 11211 -j ACCEPT

    #对于OUTPUT规则,因为预设的是ACCEPT,所以要添加DROP规则,减少不安全的端口链接。
    iptables -A OUTPUT -p tcp --sport 31337 -j DROP
    iptables -A OUTPUT -p tcp --dport 31337 -j DROP

    #丢弃坏的TCP包
    iptables -A FORWARD -p tcp ! --syn -m state --state NEW -j DROP
    #处理IP碎片数量,防止攻击,允许每秒100个
    #iptables -A FORWARD -f -m limit --limit 100/s --limit-burst 100 -j ACCEPT

    #设置ICMP包过滤,允许每秒1个包,限制触发条件是10个包
    #iptables -A FORWARD -p icmp -m limit --limit 1/s --limit-burst 10 -j ACCEPT

    #防止外部的ping和SYN洪水攻击
    iptables -A INPUT -p tcp --syn -m limit --limit 100/s --limit-burst 100 -j ACCEPT
    #ping洪水攻击,限制每秒的ping包不超过10个
    iptables -A INPUT -p icmp --icmp-type echo-request -m limit --limit 1/s –limit-burst 10 -j ACCEPT
    #防止各种端口扫描,将SYN及ACK SYN限制为每秒钟不超过200个
    iptables -A INPUT -p tcp -m tcp --tcp-flags SYN,RST,ACK SYN -m limit --limit 20/sec --limit-burst 200 -j ACCEPT

    #最后规则拒绝所有不符合以上所有的
    iptables -A INPUT -j DROP

    if [ -z "`grep "iptables-save" /etc/network/interfaces`" ]
    then
    echo "#以下有防火墙需要的可以使用
    pre-up iptables-restore < /etc/iptables.up.rules #启动时应用防火墙
    post-down iptables-save > /etc/iptables.up.rules #关闭时保存防火墙设置,以便下次启动时使用 " >> /etc/network/interfaces

    else
    echo "iptables-save find "
    fi

    clear
    echo "iptables ok ";
    echo ""

    iptables -L -n
    cat /etc/network/interfaces
    ##################################################################################################################


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

    # 2. ubuntu iptables 防火墙 启动
    modprobe ip_tables

    # 3. ubuntu iptables 防火墙 关闭
    # ubuntu 并没有关闭命令,所以要通过变通方法解决防火墙
    iptables -F
    iptables -X  
    iptables -Z  
    iptables -P INPUT ACCEPT  
    iptables -P OUTPUT ACCEPT  
    iptables -P FORWARD ACCEPT  
    modprobe -r ip_tables  
    依次执行以上命令即可关闭iptables,否则在执行modproble -r ip_tables时将会提示  FATAL: Module ip_tables is in use.

    # 4. Iptables的保存和调用
    防止每次开机或重启后都需要去调用一次,把它设置自动执行
    第一步 更改网卡配置文件
    sudo vi /etc/network/interfaces

    第二部 在最后增加配置
    #以下有防火墙需要的可以使用
    pre-up iptables-restore < /etc/iptables.up.rules #启动时应用防火墙
    post-down iptables-save > /etc/iptables.up.rules #关闭时保存防火墙设置,以便下次启动时使用

    ----------------------- 配置防火墙,开启80端口、3306端口 ---- end -----------------------

  • 相关阅读:
    java多线程执行时主线程的等待
    数据库锁机制
    数据库事务学习
    EF查询百万级数据的性能测试--多表连接复杂查询
    EF查询百万级数据的性能测试--单表查询
    一文看懂-Docker容器化
    一文看懂-Kafka消息队列
    一文看懂-ElasticSearch全文搜索引擎
    Linux系统(ubuntu)部署Asp.Net Core网站
    Linux系统学习(一)一Linux介绍
  • 原文地址:https://www.cnblogs.com/yhdsir/p/4920138.html
Copyright © 2011-2022 走看看