您的位置:首页 > 运维架构 > Linux

Linux之iptables使用

2015-11-24 00:18 363 查看
netfilter
 
3表
Filter表:(默认表)防火墙相关

NAT表:snat(源地址转换)、dnat(目标地址转换)、pnat(目标端口转换)

Mangle表:修改报文再整合

Raw
iptables

内置链

PREROUTING:数据包进入路由表之前
INPUT:通过路由表后目的地为本机(过滤进入本机的数据包,攻击)
FORWARDING:通过路由表后,目的地不为本地(经过而不入主机NAT相关)
OUTPUT:由本机产生,向外转发(处理本机发出的数据包,泄密)
POSTROUTING:发送到网卡接口之前
规则
iptables [-t 表名] -命令 -匹配 -j 动作/目标

-s:源IP段

-d:目标网段

-i:数据流入的网口eth0

-o:数据流出的网口eth0

-p:协议tcp udp

对于严谨的规则,一般默认规则都是拒绝未知,允许已知
iptables具备的3表5链
编号
命令
1
iptables -L
查看filter表
2
iptables -t nat -L
查看nat表
3
iptables -t mangle -L
查看mangle表
4
iptables -F

iptables -Z

清除所选链中的所有规则

计数器清零
4
iptables -A INPUT
为INPUT链追加
5
iptables -t filter -P INPUT -j DROP 丢弃

iptables -t filter -P INPUT -j REJECT 拒绝

iptables -t filter -P INPUT -j ACCEPT 接受

iptables -t filter -P INPUT -j RETURN 返回主链继续匹配

iptables -t filter -P INPUT -j REDIRECT 端口重定向

iptables -t filter -P INPUT -j MASQUERADE地址伪装

iptables -t filter -P INPUT -j DNAT|SNAT 地址转换

iptables -t filter -P INPUT -j MARK 打标签

iptables -t filter -P INPUT LOG

设置filter表INPUT链的默认Policy为丢弃

SNAT:代理内部客户端访问外部网络,在POSTROUTING或OUTPUT链上来做规则限制

DNAT:将内部服务器公开至外部网络,需在PREROUTING做限制
6
iptables -N filter_web

iptables -X

iptables -E

自定义链被调用才发挥作用

清除filter中使用者自定链中的规则(X Z同理),内置链不能删除

重命名自定义链
7
 
 
8
iptables -t nat -A

iptables -t nat -I3

iptables -t nat -D

iptables -t nat -R3

nat表末尾追加规则

nat插入为第3条规则

删除第几条规则

替换第几条规则
9
iptables -A INPUT -s 10.0.10.0/24 -d 10.0.10.0/24 -j ACCEPT

iptables -A INPUT -s 10.0.10.0/24 -d 10.0.10.0/24 -I eth0 -j ACCEPT

允许10.0.10.0/24网段的ip地址访问

只允许指定网段10.0.10.0/24内的IP可以访问到eth0网卡
10
iptables-P INPUT DORP

iptables-A INPUT -s 10.0.10.0/24 -d 10.0.10.0/24 -j ACCEPT

iptables-P OUTPUT DORP

iptables-A OUTPUT -d 10.0.10.0/24 -s 10.0.10.0/24-j ACCEPT

对于严谨的规则,一般默认规则都是拒绝未知,允许已知
11
/etc/init.d/iptables save

iptables-save > /tmp/iptables

iptables-restore < /tmp/iptables

保存规则到默认配置

保存规则到其它文件

加载iptables文件规则
4
iptables -A INPUT
为INPUT链追加
4
iptables -A INPUT
为INPUT链追加
4
iptables -A INPUT
为INPUT链追加
4
iptables -A INPUT
为INPUT链追加
4
iptables -A INPUT -d 10.0.10.62 -ptcp --dport 80 -j ACCEPT
放行对本机web的访问
4
iptables -A OUTPUT -s 10.0.10.62 -p tcp --sport 80 -j ACCEPT
放行出去的报文,源端口未80
iptables -A OUTPUT -s 10.0.10.62 -d 10.0.10.0/24 -p icmp --icmp-type8 -j ACCEPT
允许本机ping通 10.0.10.0/24网段
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: