zoukankan      html  css  js  c++  java
  • 【树莓派】iptables相关配置

    关于iptables的配置,参见官方资料:http://wiki.ubuntu.org.cn/IptablesHowTo 最好。

    进入iptables

    # sudo iptables -L
    

    列出目前的ip策略. 如果您刚刚配置好服务器,您是没有设置ip规则的,您要自己设置。

    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
    

    使用命令

    # sudo iptables -L
    

    查看现有的iptables防火墙规则。如果您刚架设好服务器,那么规则表应该是空的,您将看到如下内容

    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
    

    Allowing Established Sessions 允许已建立的连接接收数据

    We can allow established sessions to receive traffic:

    可以使用下面的命令,允许已建立的连接接收数据:

    # iptables -A INPUT -m state --state ESTABLISHED,RELATED -j ACCEPT
    
    


    lll

    Allowing Incoming Traffic on Specific Ports 开放指定的端口

    You could start by blocking traffic, but you might be working over SSH, where you would need to allow SSH before blocking everything else.

    To allow incoming traffic on port 22 (traditionally used by SSH), you could tell iptables to allow all TCP traffic on port 22 of your network adapter.

    刚开始时您不妨阻断所有通信,但考虑到您将来可能要使用SSH,那么您要让iptables在使用默认规则丢弃报文之前,允许SSH报文通过。

    要开放端口22(SSH的默认端口),您要告诉iptables允许接受到的所有目标端口为22的TCP报文通过。

    # iptables -A INPUT -p tcp -i eth0 --dport ssh -j ACCEPT
    

    Specifically, this appends (-A) to the table INPUT the rule that any traffic to the interface (-i) eth0 on the destination port for ssh that iptables should jump (-j), or perform the action, ACCEPT.

    执行上面的命令,一条规则会被追加到INPUT规则表的末尾(-A表示追加)。根据这条规则,对所有从接口eth0(-i指出对通过哪个接口的报文运用此规则)接收到的目标端口为22的报文,iptables要执行ACCEPT行动(-j指明当报文与规则相匹配时应采取的行动)。

    Lets check the rules: (only the first few lines shown, you will see more)

    我们来看看规则表中的情况:(这里只列出了开始的几行,您应该会看到更多内容)

    # iptables -L
    Chain INPUT (policy ACCEPT)
    target     prot opt source               destination
    ACCEPT     all  --  anywhere             anywhere            state RELATED,ESTABLISHED
    ACCEPT     tcp  --  anywhere             anywhere            tcp dpt:ssh
    

    Now, let's allow all web traffic

    现在我们开放端口80:

    # iptables -A INPUT -p tcp -i eth0 --dport 80 -j ACCEPT
    

    Checking our rules, we have

    此时的规则表中的内容如下:

    # iptables -L
    Chain INPUT (policy ACCEPT)
    target     prot opt source               destination
    ACCEPT     all  --  anywhere             anywhere            state RELATED,ESTABLISHED
    ACCEPT     tcp  --  anywhere             anywhere            tcp dpt:ssh
    ACCEPT     tcp  --  anywhere             anywhere            tcp dpt:www
    

    We have specifically allowed tcp traffic to the ssh and web ports, but as we have not blocked anything, all traffic can still come in.

    通过上述命令,我们已经代开了SSH和web服务的相应的端口,但由于没有阻断任何通信,因此所有的报文都能通过。

    ubuntu iptables 配置脚本

    #!/bin/bash
    
    case "$1" in
    
    start)
            echo -n "Staring to write your Iptbales:..."
    
            /sbin/iptables -F
            /sbin/iptables -X
            /sbin/iptables -Z
            /sbin/iptables -A INPUT -i lo -j ACCEPT
            /sbin/iptables -A INPUT -m state --state ESTABLISHED,RELATED -j ACCEPT
            /sbin/iptables -A INPUT -p icmp -m icmp --icmp-type 8 -j DROP
            /sbin/iptables -A INPUT -p tcp -m tcp --dport 22 -j ACCEPT
            /sbin/iptables -A INPUT -p tcp -m tcp --dport 80 -j ACCEPT
            /sbin/iptables -A INPUT -p tcp -m tcp --dport 3306 -j ACCEPT
            /sbin/iptables -P INPUT DROP
            echo "OK"
    ;;
    
    stop)
            echo -n "Stop iptables...."
    
            /sbin/iptables -P INPUT ACCEPT
            /sbin/iptables -F
            /sbin/iptables -X
            /sbin/iptables -Z
            echo "OK"
    ;;
    
    *)
            echo "Usage: $0  {start|stop}"
    ;;
    
    esac 

    参见:http://www.netingcn.com/ubuntu-iptables-config.html

    阿里云服务器上使用iptables设置安全策略

    公司的产品一直运行在云服务器上,从而有幸接触过aws的ec2,盛大的云服务器,最近准备有使用阿里云的弹性计算(云服务器)。前两种云服务器在安全策略这块做的比较好,提供简单明了的配置界面,而且给了默认的安全策略,反观阿里云服务器,安全策略需要自己去配置,甚至centos机器上都没有预装iptables(起码我们申请两台上都没有),算好可以使用yum来安装,安装命令如下:

    yum install -y iptables

    iptables安装好后就可以来配置规则了。由于作为web服务器来使用,所以对外要开放 80 端口,另外肯定要通过ssh进行服务器管理,22 端口也要对外开放,当然最好是把ssh服务的默认端口改掉,在公网上会有很多人试图破解密码的,如果修改端口,记得要把该端口对外开发,否则连不上就悲剧了。下面提供配置规则的详细说明:

    第一步:清空所有规则
    
    当Chain INPUT (policy DROP)时执行/sbin/iptables -F后,你将和服务器断开连接
    所有在清空所有规则前把policy DROP该为INPUT,防止悲剧发生,小心小心再小心
    /sbin/iptables -P INPUT ACCEPT
    清空所有规则
    /sbin/iptables -F
    /sbin/iptables -X
    计数器置0
    /sbin/iptables -Z
    
    第二步:设置规则
    
    允许来自于lo接口的数据包,如果没有此规则,你将不能通过127.0.0.1访问本地服务,例如ping 127.0.0.1
    /sbin/iptables -A INPUT -i lo -j ACCEPT 
    
    开放TCP协议22端口,以便能ssh,如果你是在有固定ip的场所,可以使用 -s 来限定客户端的ip
    /sbin/iptables -A INPUT -p tcp --dport 22 -j ACCEPT
    
    开放TCP协议80端口供web服务
    /sbin/iptables -A INPUT -p tcp --dport 80 -j ACCEPT
    
    10.241.121.15是另外一台服务器的内网ip,由于之间有通信,接受所有来自10.241.121.15的TCP请求
    /sbin/iptables -A INPUT -p tcp -s 10.241.121.15 -j ACCEPT
    
    接受ping
    /sbin/iptables -A INPUT -p icmp -m icmp --icmp-type 8 -j ACCEPT
    
    这条规则参看:http://www.netingcn.com/iptables-localhost-not-access-internet.html
    /sbin/iptables -A INPUT -m state --state ESTABLISHED -j ACCEPT
    
    屏蔽上述规则以为的所有请求,不可缺少,否则防火墙没有任何过滤的功能
    /sbin/iptables -P INPUT DROP
    
    可以使用 iptables -L -n 查看规则是否生效

    至此防火墙就算配置好,但是这是临时的,当重启iptables或重启机器,上述配置就会被清空,要想永久生效,还需要如下操作:

    /etc/init.d/iptables save   
    或
    service iptables save
    
    执行上述命令可以在文件 /etc/sysconfig/iptables 中看到配置

    以下提供一个干净的配置脚本:

    /sbin/iptables -P INPUT ACCEPT
    /sbin/iptables -F
    /sbin/iptables -X
    /sbin/iptables -Z
    
    /sbin/iptables -A INPUT -i lo -j ACCEPT 
    /sbin/iptables -A INPUT -p tcp --dport 22 -j ACCEPT
    /sbin/iptables -A INPUT -p tcp --dport 80 -j ACCEPT
    /sbin/iptables -A INPUT -p tcp -s 10.241.121.15 -j ACCEPT
    /sbin/iptables -A INPUT -p icmp -m icmp --icmp-type 8 -j ACCEPT
    /sbin/iptables -A INPUT -m state --state ESTABLISHED -j ACCEPT
    /sbin/iptables -P INPUT DROP
    

    最后执行 service iptables save ,先确保ssh连接没有问题,防止规则错误,导致无法连上服务器,因为没有save,重启服务器规则都失效,否则就只有去机房才能修改规则了。也可以参考:ubuntu iptables 配置脚本来写一个脚本。

    最后再次提醒,在清空规则之前一定要小心,确保Chain INPUT (policy ACCEPT)。

    参考:http://www.netingcn.com/aliyun-iptables.html

  • 相关阅读:
    死锁程序示例
    用Intellij打可执行jar包
    Semaphore tryAcquire release 正确的使用方法
    计算对象占用空间工具类
    mysql高效分页方案及原理
    乐视秒杀:每秒十万笔交易的数据架构解读
    mysql 联合索引(转)
    mysql中in和exists二者的区别和性能影响
    怎样避免 i f 判断过多,全复杂度较高,代码不美观的问题?
    Java中Enum类型的序列化(转)
  • 原文地址:https://www.cnblogs.com/haochuang/p/6398363.html
Copyright © 2011-2022 走看看