linux下的软件防火墙iptables——法则的查看与革除、定义默许战
iptables与firewalld
在centOS6下,默许的软件防火墙是iptables,而到了centos7,则是firewalld。它们之间有什么联络了,其实firewalld就是在原iptables上新封装成的一个软件。
学习iptables时,倡议先关闭firewalld,并开启iptables
yum install iptables-services systemctl stop firewalld systemctl start iptables
iptables的表和链
iptables的不一样的表代表着不一样的功能,默许有4个表
filter(过滤器) nat(地址转换) mangle raw
不一样的表下面,有着本人的法则链:
filter(INPUT/OUTPUT/FORWARD)
nat(prerouting/output/postouting)
这些链代表的意义如下:
INPUT链——进来的数据包利用此法则链中的法则
OUTPUT链——外出的数据包利用此法则链中的法则
FORWARD链——转发数据包时利用此法则链中的法则
PREROUTING链——对数据包作路由选中前利用此链中的法则
POSTROUTING链——对数据包作路由选中后利用此链中的法则
iptables的法则查看与革除
法则查看
用途示例:iptables [-t tables] -L [-nv]
选项与参数:
-t后接表类型,省去该选项,则默许为filter表。
-L列出目前表的法则
-n 不进行域名与ip反查
-v 显示更多信息
# 查看filter表的法则 # iptables -nvL Chain INPUT (policy ACCEPT 0 packets, 0 bytes) pkts bytes target prot opt in out source destination 67 4444 ACCEPT all -- * * 0.0.0.0/0 0.0.0.0/0 state RELATED,ESTABLISHED 0 0 ACCEPT icmp -- * * 0.0.0.0/0 0.0.0.0/0 0 0 ACCEPT all -- lo * 0.0.0.0/0 0.0.0.0/0 0 0 ACCEPT tcp -- * * 0.0.0.0/0 0.0.0.0/0 state NEW tcp dpt:22 2 286 REJECT all -- * * 0.0.0.0/0 0.0.0.0/0 reject-with icmp-host-prohibited Chain FORWARD (policy ACCEPT 0 packets, 0 bytes) pkts bytes target prot opt in out source destination 0 0 REJECT all -- * * 0.0.0.0/0 0.0.0.0/0 reject-with icmp-host-prohibited Chain OUTPUT (policy ACCEPT 38 packets, 4664 bytes) pkts bytes target prot opt in out source destination # 查看nat表的法则 iptables -t nat -L -nv
链下的法则选项的含义如下:
target:代表进行的操纵,ACCEPT放行、drop丢弃、reject拒绝
prot:代表运用的数据包协定,有tcp、udp以及icmp
opt:注明信息
source:对某来源主机进行限定
destination:对某指标主机进行限定
上面显示的INPUT链的5条法则含义如下:
只有数据包的状态为RELATED,ESTABLISHED,都承受
只有是icmp包都承受
只有是当地回环网卡,所有数据都承受
只有是发送给22端口的自动式连贯的TCP数据包都承受。
拒绝所有的数据包
分明iptables的法则
默许安装centOS7后,系统就已经有很多iptables的法则,这里教大家怎样去革除这些法则。
用途示例:iptables [-t tables] [-FXZ]
选项与参数:
-F 清算所有已定制的法则
-X 清算所实用户自定义的法则
-Z 将所有的统计计数置零
# iptables -F # iptables -X # iptables -Z
查看具体的法则
运用iptables-save可以查看具体的法则
用途:iptables-save [-t tables]
# iptables-save -t filter # Generated by iptables-save v1.4.21 on Sat Nov 14 21:51:56 2020 *filter :INPUT ACCEPT [0:0] :FORWARD ACCEPT [0:0] :OUTPUT ACCEPT [56:7196] -A INPUT -m state --state RELATED,ESTABLISHED -j ACCEPT -A INPUT -p icmp -j ACCEPT -A INPUT -i lo -j ACCEPT -A INPUT -p tcp -m state --state NEW -m tcp --dport 22 -j ACCEPT -A INPUT -j REJECT --reject-with icmp-host-prohibited -A FORWARD -j REJECT --reject-with icmp-host-prohibited # Completed on Sat Nov 14 21:51:56 2020
定义默许战略
当我们分明完法则后,就只剩下默许的战略了。什么是默许的战略,就是当谴责脚我们任何一条法则时,就采纳默许法则。默许的战略有ACCEPT(承受数据包)和DROP(丢弃数据包)
用途:iptables [-t tables] -P [INPUT|OUTPUT|FORWARD……] [ACCEPT|DROP]
此刻,我们尝试将filter的INPUT链的默许修改为DROP、OUTPUT及FORWARD链修改为ACCETP
iptables -t filter -P INPUT DROP # 注意,该下令敲完后,你的终端就可能会断开连贯了 iptables -P OUTPUT ACCEPT iptables -P FORWARD ACCEPT