iptables

[ 2012-11-19 09:40:25 | 作者: admin ]
字号: | |
注:

                 每个举例都是分开的!

                 实际生产中如果全部为DROP策略还需要对lo放开权限;

                 一般使用INPUT DROP 、 FORWARD ACCEPT、 OUTPUT ACCEPT默认策略

 

 
  个人理解:
                 iptables由规则表组成:filter(常用)\nat\mangle\raw
                 规则表由规则链组成,如filter表:INPUT\FORWARD\OUTPUT
                 规则链中设置规则、并且有每个链有默认的规则ACCEPT或DROP
                 数据库自上而下比对规则一旦匹配,则不再理会以下的规则
                 
 
选项:
-t 指定表
-F [chain] 清空链中的规则,如果不指定链名会清空所有链的规则!!!
-N <chain> 创建自定义链
-X [chain] 自定义的链(删除前确保链中没有规则)
-P <chain> {DROP|ACCEPT}设置默认规则
-E <oldchain> <newchain>重命名链
-A 为指定的chain添加规则
-D 删除指定链中的规则:iptables -D INPUT 3
-I 插入规则
-R 替换规则
-p 指定使用的协议tcp\udp等
-s 指定源网段或主机
-d 指定目的网段或主机
--dport目的端口
--sport源端口
-j 跳转目标(指定处理的动作)
-i 进接口
-o 出接口
--ports 双向端口
--icmp-type <typename> 指定ICMP的类型
--mac-source <address> 指定MAC
-Z 流量统计清零
 


查看规则命令
         [root@localhost ~]# iptables -L -nv //显示内容更详细
         Chain INPUT (policy ACCEPT 244 packets, 39167 bytes)
           pkts bytes target prot opt in out source destination
          
         Chain FORWARD (policy ACCEPT 0 packets, 0 bytes)
           pkts bytes target prot opt in out source destination
          
         Chain OUTPUT (policy ACCEPT 125 packets, 13876 bytes)
           pkts bytes target prot opt in out source destination
          
          
         [root@localhost ~]# iptables -L -n
         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




attachments/201211/19_094413_162409669.jpg

attachments/201211/19_094424_162427860.jpg

attachments/201211/19_094434_162447280.jpg





1、设置链的默认规则为"拒绝" (如果不使用-t指定表,默认为filter表)
# iptables -P INPUT DROP
# iptables -P FORWARD DROP
# iptables -P OUTPUT DROP
 
 
iptables -L -n 查看是否设置好,这样的设置好了,只是临时的,使用以下命令保存。
# service iptables save 进行保存:保存在 /etc/sysconfig/iptables
 
 
 
2、只允许连接22端口
iptables -L -n 查看所有链的策略为DROP
# iptables -A INPUT -p tcp --dport 22 -j ACCEPT
# iptables -A OUTPUT -p tcp --sport 22 -j ACCEPT
# service iptables save
 
 
 
3、禁止192.168.2.26访问本机80端口
iptables -L -n查看所有链均为ACCEPT
# iptables -A INPUT -p tcp --dport 80 -s 192.168.2.26 -j DROP
 
附:
iptables -F [INPUT|FORWARD|OUTPUT] 清空指定链的所有规则
 
 
 
4、删除指定链中的规则,如:删除INPUT中对80端口的规则
[root@localhost ~]# iptables -L -n
Chain INPUT (policy ACCEPT)
target prot opt source destination
DROP tcp -- 192.168.2.26 0.0.0.0/0 tcp dpt:80
 
4.1、获取规则编号
[root@localhost ~]# iptables -L -n --line-number
Chain INPUT (policy ACCEPT)
num target prot opt source destination
1 DROP tcp -- 192.168.2.26 0.0.0.0/0 tcp dpt:80
 
4.2、删除指定规则
[root@localhost ~]# iptables -D INPUT 1
 
 
 
5、只允许指定IP或网段访问22端口
 
第一种
iptables -L -n查看所有链均为ACCEPT
# iptables -A INPUT -p tcp --dport 22 -s 192.168.2.254/32 -j ACCEPT
# iptables -A INPUT -p tcp --dport 22 -j DROP
第二种
iptables -L -n查看所有链均为DROP
# iptables -A INPUT -p tcp --dport 22 -s 192.168.2.254/32 -j ACCEPT
# iptables -A OUTPUT -p tcp --sport 22 -d 192.168.2.254/32 -j ACCEPT
 
 
 
6、使用指定的DNS服务器
6.1使用替换命令替换规则
iptables -R INPUT 1 -p udp --sport 53 -s 202.102.128.68 -j ACCEPT
iptables -R OUTPUT 1 -p udp --dport 53 -d 202.102.128.68 -j ACCEPT
6.2使用插入命令插入规则(插入后刚才的规则编号由1改为2)
iptables -I INPUT 1 -p udp --sport 53 -s 8.8.8.8 -j ACCEPT
iptables -I OUTPUT 1 -p udp --dport 53 -d 8.8.8.8 -j ACCEPT
注:在群里讨论说是有时还用到tcp53,这里由于可以正常解析,暂没有添加tcp规则。
 
 
 
 
7、对于被动工作模式FTP的规则设置
iptables -A INPUT -p tcp --dport 20 -j ACCEPT
iptables -A INPUT -p tcp --dport 21 -j ACCEPT
iptables -A OUTPUT -p tcp --sport 21 -j ACCEPT
iptables -A OUTPUT -p tcp --sport 20 -j ACCEPT
这样还不够,会在连接服务器数据端口的时候出问题。因为FTP服务器被动模式下的数据端口大于1023的随机端口。
 
解决:定义FTP服务器使用的数据端口
vim /etc/vsftpd/vsftpd.conf 添加以下两行参数
pasv_min_port=30001
pasv_max_port=31000
保存退出
重启FTP服务
 
添加规则
iptables -A INPUT -p tcp --dport 30001:31000 -j ACCEPT
iptables -A OUTPUT -p tcp --sport 30001:31000 -j ACCEPT
 
完成
 
 
 
8、清除本机防火墙所有规则
[root@localhost ~]# iptables -F
[root@localhost ~]# iptables -X
[root@localhost ~]# iptables -Z
 

 

本文出自 “notepad” 博客,请务必保留此出处http://sndapk.blog.51cto.com/5385144/1053885
[最后修改由 admin, 于 2012-11-19 09:45:40]
评论Feed 评论Feed: http://blog.xg98.com/feed.asp?q=comment&id=1907

这篇日志没有评论。

此日志不可发表评论。