笔者以前写过iptables的文章-Linux的netfilter/iptables简介 。netfilter是防火墙安全框架,允许内核模块对遍历系统的每个数据包进行检查。在红帽7之前,iptables是与内核netfilter交互的主要方法,红帽7之之后交互的新方法是firewalld ,是一个配置和监控系统防火墙规则的系统守护进程。
不管是iptables还是firewalld,都是用于维护规则,而真正使用规则干活的是内核的netfilter。firewalld可以动态修改单条规则,使用上要也比iptables人性化 。
firewalld 将网络流量分为多个区域,既是策略模板,可以根据生产场景的不同而选择合适的策略集合,从而实现防火墙策略之间的快速切换,简化防火墙管理。根据数据包的源IP地址或传入网络接口等条件,流量将传入相应区域的防火墙规则,每个区域都具有自己要打开或关闭的端口或服务。FirewallD的默认区域是public 。
系统预定义区域有9种,可以查看不管哪一种,都是允许数据包流出,除了预定义服务外,默认都是流入数据包是拒绝的。

与firewalld交互有三种方式:
1、直接编辑/etc/firewalld 配置文件

2、使用firewalld-config图形界面,可以可视化的配置策略。firewalld-config需要安装,命令行或者应用程序里面搜索安装即可。
里面有两种模式可选:
Runtime:当前生效模式,但重启后失效。
Permanent:重启后永久有效,但不能立即生效,需要在options里面reload或者命令行firewall-cmd --reload 。

3、在命令行使用firewall-cmd
实践中firewall-cmd命令行使用较多。 systemctl status firewalld查看服务正常运行,firewall-cmd --help查看命令行使用方法。 firewall-cmd --state查看运行状态。在笔者的虚拟机centos8上,firewalld默认是开启的,在腾讯云服务器上firewalld是关闭的,本文以虚拟机centos8为例。

firewalld-cmd配置防火墙。
1、firewall-cmd --list-all 列出默认区域所有设置
firewall-cmd --list-all-zones 查看所有区域设置

可以看到流量与ssh、dhcpv6-client服务相关,则允许进入。
2、firewall-cmd --get-default-zone 查看默认区域

3、firewall-cmd --set-default-zone = ?? 更改区域,快速切换防火墙策略

4、源自此IP或子网的流量导向指定的区域。 --permanent代表永久设置,-reload 代表使用永久设置立马生效。--zone代表设置的区域,不写就是默认区域。
firewall-cmd --permanent --zone=public --add-source=192.168.0.0/24
firewall-cmd --permanent --zone=public --remove-source=192.168.0.0/24
5、添加或移除mysql服务
firewall-cmd --permanent --zone=public --add-service=mysql
firewall-cmd --permanent --zone=public --remove-service=mysql
6、 添加或移除端口
firewall-cmd --permanent --zone=public --add-port=3306
firewall-cmd --permanent --zone=public --remove-port=3306
7、允许或取消icmp协议流量,即ping
firewall-cmd --zone=drop --add-protocol=icmp
firewall-cmd --zone=drop --remove-protocol=icmp
8、其他的选项如下:

9、 firewall-cmd --reload 更改配置后reload使其失效
网友评论