centos7防火墙机制iptables与ebtables差别

一、iptables

iptables是网络层的流量控制,主要适用于流量的来源和去向至少有一个是本机的情况。

1.iptables常用规则编写

这条规则的意思是,FORWARD链上,任何对22端口建立好的tcp连接,都不允许通过。

iptables -A FORWARD -p tcp -m state --state established,related -m tcp --dport 22 -j DROP

其中常用参数说明如下:

 

-t #tables 指的是操作的表,有四种(filter  nat   mangle   raw),不指定就默认是filter.

-A #指定添加的链路,一般是PREROUTING(转发前)  POSTROUTING(转发后)  FORWARD(转发链路)
-I #指定添加的链路,不同的是,-A添加,-I插入,-I指定插入顺序,不指定就默认放在最前面,-A放在所有规则之后。iptables规则是从上到下执行的。 -p #指定协议(一般是tcp协议) -m #指定模块的扩展功能(tcp模块扩展了--dport, --tcp-flags, --sync等功能) -m state --state <状态> #一般有三种状态 ▪ INVALID:无效的封包,例如数据破损的封包状态 ▪ ESTABLISHED:已经联机成功的联机状态; ▪ NEW:想要新建立联机的封包状态;

-j #指定进行的操作。DROP代表丢弃,ACCEPT接受

  

2.iptables常用命令

#列出OUTPUT链路的规则,-n代表不对ip地址进行反查,-L查看当前表的所有规则,--line-numbers代表展示行号,-t表示查看的表,不指定默认filter表
iptables -nL OUTPUT --line-numbers

#删除OUTPUT链的第5条规则,因为刚才已经得到行号了
iptables -D OUTPUT 5

#清空所有规则-F
iptables -F

#修改规则-R
iptables -R INPUT 3 -j ACCEPT

 

3.iptables参数参考

Usage: iptables -[AD] chain rule-specification [options]
       iptables -I chain [rulenum] rule-specification [options]
       iptables -R chain rulenum rule-specification [options]
       iptables -D chain rulenum [options]
       iptables -[LS] [chain [rulenum]] [options]
       iptables -[FZ] [chain] [options]
       iptables -[NX] chain
       iptables -E old-chain-name new-chain-name
       iptables -P chain target [options]
       iptables -h (print this help information)
 
Commands:
Either long or short options are allowed.
  --append  -A chain            Append to chain
  --delete  -D chain            Delete matching rule from chain
  --delete  -D chain rulenum
                                Delete rule rulenum (1 = first) from chain
  --insert  -I chain [rulenum]
                                Insert in chain as rulenum (default 1=first)
  --replace -R chain rulenum
                                Replace rule rulenum (1 = first) in chain
  --list    -L [chain [rulenum]]
                                List the rules in a chain or all chains
  --list-rules -S [chain [rulenum]]
                                Print the rules in a chain or all chains
  --flush   -F [chain]          Delete all rules in  chain or all chains
  --zero    -Z [chain [rulenum]]
                                Zero counters in chain or all chains
  --new     -N chain            Create a new user-defined chain
  --delete-chain
            -X [chain]          Delete a user-defined chain
  --policy  -P chain target
                                Change policy on chain to target
  --rename-chain
            -E old-chain new-chain
                                Change chain name, (moving any references)

  

二.ebtables

ebtables是数据链路层网桥防火墙,适用于来源和去向都不是本机,但是来源和去向至少有一个是和本机桥接网络的服务器。(可能是本机上创建的虚拟机之类的)

1.ebtables常用规则编写

这条规则的意思是,指定filter表,指定FORWARD链,指定ipv4网络协议,指定ip协议为tcp,指定数据包来源是x.0.0.1,指定操作类型是拒绝

ebtables -t filter -A FORWARD -p ipv4  --ip-proto 6 --ip-src x.0.0.1  -j DROPq

 

其中参数说明如下:

-A:指定FORWARD链,也就是桥接时候,来源和去向都不是自己,只转发
-p:指明使用的协议类型,ipv4,arp等可选(使用时必选)详情见/etc/ethertypes
--ip-proto:IP包的类型,1为ICMP包,6为TCP包,17为UDP包,在/etc/protocols下有详细说明
--ip-src:IP包的源地址
--ip-dst:IP包的目的地址
--ip-sport:IP包的源端口
--ip-dport:IP包的目的端口

-j:指定操作,ACCEPT是接受,DROP是拒绝

这两个一般不常用 -i:输入interface -o:输出interface

  

2.ebtables常用命令

#清空当前规则
ebtables -F

#删除一条ebtables命令

 

3.一些注意事项

(1)在centos7使用ebtables的过程中,发现ebtables竟然是和firewall一致的。

  也就是说,如果命令行使用了ebtables命令,立马就可以生效,但是只要不是永久保存,使用【firewall-cmd --reload】就会一夜回到解放前,所有ebtables规则都清空了。但是ebtables规则的保存目前来看意义不是很大。建议先用firewall命令,然后用ebtables命令,然后用iptables命令。

(2)ebtables指定的ip可以是一个网段,例如x.0.0.1可以换成x.0.0.0/24等。关于网关的说明,要继续学习,不过/8是从第二位开始全网段,/24就是最后一位不确定全网段。

 


参考文件:

iptables

https://www.cnblogs.com/miracle-luna/p/13718436.html

https://www.cnblogs.com/ftl1012/p/iptables.html

ebtables:

https://www.cnblogs.com/xuanxuanBOSS/p/11424290.html

posted @ 2021-07-22 18:23  玉石非玉  阅读(599)  评论(0编辑  收藏  举报