iptables



netfilter (iptables)


netfilter   工作在内核软件,实现数据包的fpih
iptables 工作应用层一个软件,用来控制netfilter


包过滤防火墙 (tcp/ip 四层)


1.应用层          通过软件为用户提供接口
2. 传输层            TCP,UDP 使用端口
3. 网络层               路由和选址  (icmp) sip  dip
4. 数据链接层                     s_mac
过滤的依据: s_mac/sip/dip/sport/dport/状态(三次握手/四次断开)


netfilter防火墙的元素及关系:
netfilter==>表==>链==>规则


三张表: 
filter  防火墙表,允许和拒绝都在这里实现
nat  地址转换  
mang   数据包整形


五条链    
INPUT              本机进站的数据流
OUTPUT          本机出站的数据流
FORWARD          路由的数据流
POSTROUTING             路由后的数据流
PREROUTING              路由前的数据流




--------------------
表跟链的对应关系:
filter:INPUT,OUTPUT,FORWARD
nat: OUTPUT,PREROUTING,POSTROUTING
mangle:INPUT,OUTPUT,FORWARD,PREROUTING,POSTROUTING


------------
四种数据流:
本机进站的数据流:packet-->ethX-->PREROUTING-->INPUT-->本机
本机出站的数据流:packet-->OUTPUT-->POSTROUTING-->ethX-->destination
路由的数据流:
本机访问本机:      本机-->packet-->lo-->PREROUTING-->INPUT-->本机
   本机-->packet-->OUTPUT-->POSTROUTING--lo-->本机
   
   
   
防火墙规则匹配顺序:
1.按顺序匹配,如果第一条匹配到了就直接执行这条规则的动作,不往下匹配其它规则.
2.如果第一条匹配不到,第二条也匹配不到,继续往下匹配直到找到匹配的规则,如果找不到匹配默认规则.
--------------------    
   
iptables操作命令:




追加规则:
# iptables -t filter -A INPUT -i lo -j ACCEPT


插入规则:
# iptables -t filter -I INPUT -i eth0 -j ACCEPT --插入成为第一条
# iptables -t filter -I INPUT 3 -i eth0 -j ACCEPT --插入成为第三条规则


替换规则:
# iptables -t filter -R INPUT 3 -i eth1 -j ACCEPT --替换成为指定规则

删除规则:
# iptables -t filter -D INPUT 2 --删除指定链指定编号的规则


+++++++++++
#iptables -t filter -A INPUT -i lo -j ACCEPT
#iptables -t filter -A OUTPUT -o lo -j ACCEPT
#iptables -t filter -A INPUT -i eth0 -s 192.168.0.0/24 -d 192.168.0.250 -j ACCEPT
# iptables -t filter -A INPUT -i eth1 -j ACCEPT
# iptables -L INPUT -n --line -v
# iptables -t filer -I INPUT 2 -i eth2 -j ACCEPT   插入默认第二条
# iptables -t filter -I INPUT  -i eth3 -j ACCEPT    插入默认第一条
# iptables -t filter -R INPUT 1 -i eth4 -j ACCEPT  替换默认第一条
#iptables -t filter -D INPUT 1 删除默认第一条
# iptables -t filter -F INPUT 清空filter所有INPUT链
#iptables -t filter -F


#iptables -L INPUT -n --line -v




+++++++++++++++++++
清空规则:
1、清空一张表
# iptables -t filter -F


2、清空一条链中的规则
# iptables -t filter -F INPUT


新建/删除用户自定义的链:
# iptables -t filter -N uplooking 新建
# iptables -t filter -X uplooking 删除
# iptables -t filter -X 清空filter表中所有用户自定义链
+++++++++
iptables -t filter -N TCP
iptables -t filter -N UDP
iptables -t filter -A INPUT -i lo -j ACCEPT
iptables -t filter -A INPUT -p tcp -j TCP
++++++++
更改默认规则:
# iptables -t filter -P INPUT ACCEPT
# iptables -t filter -P INPUT DROP


   
实例应用一(本机的访问控制):
拒绝所有:允许本机能访问所有服务,允许访问DNS,postfix,dovecot,vsftpd,telnet,web,


iptables -t filter -P INPUT DROP
iptables -t filter -P OUTPUT DROP
1.允许本机访问本机的所有服务:
[root@mail ~]# iptables -t filter -A INPUT -i lo -j ACCEPT
[root@mail ~]# iptables -t filter -A OUTPUT -o lo -j ACCEPT
+++++++++
iptables -t filter -A INPUT -p tcp -dport 5902 -i br0 -s 192.168.0.45 -j ACCEPT
iptables -t filter -A OUTPUT -o br0 -p tcp --sport 5902 -d 192.168.0.45 -j ACCEPT
iptables -L -n -v --line
iptables -t filter -A INPUT -i br0 -p tcp --dport 5900:5902 -s 192.168.0.0/24 -j ACCEPT
iptables -t filter -A OUTPUT -o br0 -p tcp --sport 5900:5902 -d 192.168.0.0/24 -j ACCEPT
++++++++++++++


2.允许指定客户机访问我的80端口
[root@mail ~]# iptables -t filter -A INPUT -i eth0 -p tcp --dport 80 -s 10.1.1.111 -j ACCEPT
[root@mail ~]# iptables -t filter -A OUTPUT -o eth0 -p tcp --sport 80 -d 10.1.1.111 -j ACCEPT


[root@mail ~]# iptables -t filter -A INPUT -i eth0 -p tcp --dport 80 -s 10.1.1.0/24 -j ACCEPT
[root@mail ~]# iptables -t filter -A OUTPUT -o eth0 -p tcp --sport 80 -d 10.1.1.0/24 -j ACCEPT




[root@mail ~]# iptables   -A INPUT -i eth0 -p tcp --dport 22 -j ACCEPT
[root@mail ~]# iptables -A OUTPUT -o eth0 -p tcp --sport 22 -j ACCEPT


# iptables -t filter -R INPUT 2 -p tcp -m multiport --dport 20:21,80,443,5900 -j ACCEPT
# iptables -t filter -R OUTPUT 2 -p tcp -m multiport --sport 20:21,80,443,5900 -j ACCEPT




[root@mail ~]# service iptables save            --防火规则马上写马上生效,但重启机器或者重iptables服务,规则丢失.
将当前规则保存到 /etc/sysconfig/iptables:                 [确定]


从非默认位置恢复规则:
# iptables-save > /tmp/iptables.save
# iptables-restore <  /tmp/iptables.save




icmp:
[root@mail ~]# iptables -t filter -i eth0 -A INPUT -p icmp -j ACCEPT
[root@mail ~]# iptables -t filter -o eth0 -A OUTPUT -p icmp -j ACCEPT
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值