Linux Iptables 相关设置

首先在使用iptables之前敲入一下两条命令
> iptables -F   #这句话的意思是清空所有的链
> iptables -X  #这句话的意思是清空所有自定义的链
以上两条的含义你可以简单的认为是iptables的初始化命令,无需深入。
下面我们将要开始建立一个iptables防火墙了。我们的做法是,默认所有的数据都丢弃,除非我认为满足条件的我才接受,有针对的打开我们需要的端口,无疑是很安全的一种做法。下面两句话可以定义默认全部丢弃数据包:
> iptables -P INPUT DROP
> iptables -P OUTPUT DROP
-P参数的意思是policy,翻译成策略~那么这两句话就好理解了。
第一句的意思是:
输入(INPUT)的数据包默认的策略(-P)是丢弃(DROP)的
第二句的意思是:
输出(OUTPUT)的数据包默认的策略(-P)是丢弃(DROP)的
其实到这里已经是一个有用的防火墙了,只不过,没有什么意义,和拔掉网线的概念没有什么不同。
首先写下这6句话:
iptables -A INPUT -p icmp --icmp-type any -j ACCEPT
允许icmp包进入
iptables -A INPUT -s localhost -d localhost -j ACCEPT
允许本地的数据包
iptables -A INPUT -m state --state ESTABLISHED,RELATED -j ACCEPT
允许已经建立和相关的数据包进入
iptables -A OUTPUT -p icmp --icmp any -j ACCEPT
允许icmp包出去
iptables -A OUTPUT -s localhost -d localhost -j ACCEPT
允许本地数据包
iptables -A OUTPUT -m state --state ESTABLISHED,RELATED -j ACCEPT
允许已经建立和相关的数据包出去
说明一下,这6句基本上都是要的。
 
如果我的电脑是一台web服务器的话,别人也没有办法访问,怎样才能让别人能访问我的web呢?很简单,打开80端口。
> iptables -A INPUT -p tcp --dport 80 -j ACCEPT
但是这样的话,别人还是没有办法访问我,问什么呢?因为OUTPUT是关闭的,没有数据包能出去,那么就需要下面的一句话:
> iptables -A OUTPUT -m state --state ESTABLISHED,RELATED -j ACCEPT
这样别人就能访问你的web了。
 
但是如果你想访问别人的web怎么办呢?打开出去的80端口吧!
> iptables -A OUTPUT -p tcp -m state --state NEW --dport 80  -j ACCEPT
同样的这样的一句话还是不能起作用,我们需要打开别人进来的数据包
> iptables -A INPUT -m state --state ESTABLISHED,RELATED -j ACCEPT
可以了么?试试看~还是不可以???为什么呢???对了,你可能想到了DNS端口没有打开怎么访问域名服务器呢?下面我们打开DNS端口吧!
> iptables -A OUTPUT -p udp --dport 53 -j ACCEPT
> iptables -A INPUT -p udp --dport 53 -j ACCEPT
OK,这样就能访问别人的web站点了,不过如果你要访问https的站点,打开443端口吧
 
为了方便管理,我们可能还要经常ssh到这台服务器上去,那么打开22号端口吧!
> iptables -A IPUT -p tcp -dport 22 -j ACCEPT
或者我们还可能需要用这台电脑ssh到别的电脑上去
> iptables -A OUTPUT -p tcp --dport 22 -j ACCEPT
 
但是我只允许一个固定的ip能ssh到我的服务器上来怎么办呢?上句改成:
> iptables -A INPUT -p tcp --dport 22 -s 192.168.1.10 -j ACCEPT
 
上句话的意思是,只允许192.168.1.1的用户通过ssh进到服务器。不过这样还是不安全,我们可以同时绑定访问者的mac,这样就安全多了!
> iptables -A INPUT -p tcp --dport 22 -m mac --mac 00:18:de:a5:83:c7 -s 192.168.1.10 -j ACCEPT
 
最后脚本话一下:
#!/bin/bash
#DEFINE VARIABLES
HTTP_PORT=80
SECURE_HTTP_PORT=443
ALLOWED_MAC=00:18:de:a5:83:c7
SSH_PORT=22
DNS_PORT=53
ALLOWED_IP=192.168.1.10
#FLUSH IPTABLES
iptables -F
iptables -X
#DEFINE DEFAULT ACTION
iptables -P INPUT DROP
iptables -P OUTPUT DROP
#DEFINE INPUT CHAINS
iptables -A INPUT -p icmp --icmp-type any -j ACCEPT
iptables -A INPUT -s localhost -d localhost -j ACCEPT
iptables -A INPUT -m state --state ESTABLISHED,RELATED -j ACCEPT
 
iptables -A INPUT -p tcp --dport $SSH_PORT -j ACCEPT
diptables -A INPUT -p tcp --dport 22 -m mac --mac $ALLOWED_MAC -s $ALLOWED_IP -j ACCEPT

#DEFINE OUTPUT CHAINS
iptables -A OUTPUT -p icmp --icmp any -j ACCEPT
iptables -A OUTPUT -s localhost -d localhost -j ACCEPT
iptables -A OUTPUT -m state --state ESTABLISHED,RELATED -j ACCEPT
 

iptables -A OUTPUT -p tcp -m state --state NEW --dport $HTTP_PORT  -j ACCEPT
iptables -A OUTPUT -p tcp --dport $SECURE_HTTP_PORT -j ACCEPT
iptables -A OUTPUT -p udp --dport $DNS_PORT -j ACCEPT
iptables -A OUTPUT -p tcp --dport $SSH_PORT -j ACCEPT

posted @ 2016-06-13 17:36  林锅  阅读(180)  评论(0编辑  收藏  举报