【树莓派】iptables相关配置

本文涉及的产品
云服务器 ECS,每月免费额度280元 3个月
云服务器ECS,u1 2核4GB 1个月
简介:

进入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.cnblogs.com/haochuang/p/6398363.html,如需转载请自行联系原作者

相关实践学习
一小时快速掌握 SQL 语法
本实验带您学习SQL的基础语法,快速入门SQL。
7天玩转云服务器
云服务器ECS(Elastic Compute Service)是一种弹性可伸缩的计算服务,可降低 IT 成本,提升运维效率。本课程手把手带你了解ECS、掌握基本操作、动手实操快照管理、镜像管理等。了解产品详情: https://www.aliyun.com/product/ecs
相关文章
|
网络协议 Linux 网络安全
Linux学习笔记 28(Iptables防火墙设置)
1、 查询iptables服务器是否安装 2、 安装iptables 3、 查看iptables已有规则-I表示查看4、 清空iptables已有规则-F表示清空5、 设置iptables默认策略为拒绝-P表示默认6、 设置iptables默认策略为允许7、 设置对所有的ping操作的策略为允许Ping操作依赖ICMP-I表示添加规则到头部-p表示要应对的协议-j表示应当的策略行为8、 在OUTPUT规则链添加策略到末尾,允许其他没有被匹配的数据包-A表示添加规则到末尾没有指定要对应的数据包时哪一种具体的就省略9、 作为网关服务器1、 查询iptables服务器是否安装2、 安装i要禁止
Linux学习笔记 28(Iptables防火墙设置)
|
Linux 网络架构 网络协议
|
Linux 网络安全
Linux iptables 防火墙包过滤与端口转发
iptables 是与 Linux 内核集成的 IP 信息包过滤系统。如果 Linux 系统连接到因特网或 LAN、服务器或连接 LAN 和因特网的代理服务器, 则该系统有利于在 Linux 系统上更好地控制 IP 信息包过滤和防火墙配置。
1074 0
|
网络协议 Linux 网络安全
|
网络协议 Linux 网络安全