【树莓派】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
【树莓派】iptables相关配置的更多相关文章
- 【树莓派】服务配置相关3:基于Ubuntu Server的服务配置
该文接续之前写过的两篇: [树莓派]服务配置相关 [树莓派]服务配置相关2:基于RPi Desktop的服务配置 这是我个人用来进行树莓派盒子安装配置的脚本,对于外部其他博友,可以部分参考,但不需要逐 ...
- 【树莓派】服务配置相关2:基于RPi Desktop的服务配置
该文接续之前写过的一篇:[树莓派]服务配置相关. 这是我个人用来进行树莓派盒子安装配置的脚本,对于外部其他博友,可以部分参考,但不需要逐个引用. 现在有一定更新,部分按如下脚本来操作: step1: ...
- CentOS切换为iptables防火墙并进行相关配置
CentOS切换为iptables防火墙 切换到iptables首先应该关掉默认的firewalld,然后安装iptables服务. 1.关闭firewall: service firewalld s ...
- iptables基础配置
启动指令:service iptables start 重启指令:service iptables restart 关闭指令:service iptables stop 规则相关配置:/e ...
- dbcp相关配置
最近在看一些dbcp的相关内容,顺便做一下记录,免得自己给忘记了. 1. 引入dbcp (选择1.4) <dependency> <groupId>com.alibaba. ...
- zookeeper集群的搭建以及hadoop ha的相关配置
1.环境 centos7 hadoop2.6.5 zookeeper3.4.9 jdk1.8 master作为active主机,data1作为standby备用机,三台机器均作为数据节点,yarn资源 ...
- Linux网络相关配置
一.修改网卡相关配置 Linux网络参数是在/etc/sysconfig/network-scripts/ifcfg-eth0中设置,其中ifcfg-eth0表示是第一个网卡,如果还有另外一块网卡,则 ...
- ios开发之Info.plist文件相关配置
前言:在iOS开发中有些情况下需要对Info.plist文件进行配置,以下介绍几种相关配置.以后遇到需要配置的再更新... 开发环境:swift3.0.1,Xcode8.1 一,项目中需要使用第三方字 ...
- SharePoint 2013 托管导航及相关配置 <二>
本文的思路是使用JQuery重写SharePoint自带托管导航的样式,其实思路和脚本都非常简单,引用一下JQuery脚本,然后重写导航的样式,把脚本放到母版页中,即可.当然,使用JQuery可以做很 ...
随机推荐
- 启动activity与使用Intent通信机制解析
我们都知道,一个activity启动另一个activity最简单的方式就是使用startActivity方法: public void startActivity (Intent intent) 但是 ...
- canvas绘图详解-06-绘制一个五角星-常用绘图原理
先将如何画一个正规的五角星 在五角星的内外画两个圆,五角星有五个角,360/5=72度 所以得出这两个角的度数 然后算出这两个点坐标 角度转弧度 角度/180*Math.PI 所以外顶点坐标 x: ...
- EntityFramework Core并发导致显示插入主键问题
前言 之前讨论过EntityFramework Core中并发问题,按照官网所给并发冲突解决方案以为没有什么问题,但是在做单元测试时发现too young,too siimple,下面我们一起来看看. ...
- swift注意
赋值的时候要想为空 可以用 ? 例如 var age1:Int? // ?表示age1的类型为可选类型,其值可以为空print(age1) 判断一个字符串为空字符串if str_empty.isE ...
- java 重写的学习
本文全文转自:http://www.cnblogs.com/happyframework/p/3332243.html,非常感谢 Java中的重写规则比较灵活,具体如下: 除了 private 修饰之 ...
- Java 练习(多态,instanceof)
题目:*(封装.继承)设计如下的继承树: Accout 表示银行账户,id 属性表示账户id,balance 表示账户余额,password 表示账户密码: SavingAccount 表示储蓄账户, ...
- barmanager工具栏及gridcontrol部分属性设置
1.工具栏部分属性设置,如:右键菜单,禁止移动等 2.gridControl属性设置,如选中行颜色,禁止移动隐藏等
- linux下载时提示请尝试移除磁盘中不需要的文件并重试,或者保存到其他位置
因为我是用虚拟机装的linux,所以当时就分配了20G硬盘,下载了几个应用后再下载就提示我这个了.一开始我还以为是因为下载链接的问题,后来才知道原来是因为/tmp的满了. 然后我输入以下连个命令就能正 ...
- unity3d 脚本学习系列
最近使用unity3d,对其中的脚本部分进一系列总结,算是这一段时间的收获吧.
- Mysql动态sql语句,用当前时间做表名
在mysql备份操作中, 我们可能要使用表名和当前时间来做为备份表的名称,但是MySQL在存储过程中不支持使用变量名来做表名或者列名. 例如:有一个表"user",我需要备份一份, ...