从CentOS7
(RHEL7
)开始,官方的标准防火墙设置软件从iptables
变更为firewalld
,相信不少习惯使用iptables
的人会感到十分不习惯,但实际上firewalld
更为简单易用。
大致用法就是:把可信任的IP地址
添加到trusted
区域,把不可信任的IP地址
添加到block
区域,把要公开的网络服务添加到public
区域。
配置方式
firewalld
的配置文件一般存放在下面两个目录:
/etc/firewalld/
/usr/lib/firewalld/
当需要一个文件时firewalld
会优先使用第一个目录的。
这两个配置目录的结构很简单,主要是两个文件和三个目录:
- 文件:
firewalld.conf
:主配置文件(只有5
个配置项)- ①
DefaultZone
:默认使用的zone
; - ②
MinimalMark
:使用标记的最小值; - ③
CleanuupOnExit
:退出后是否清除防火墙规则; - ④
Lockdown
:是否限制别的程序通过D-BUS
接口直接操作firewalld
; - ⑤
IPv6_rpfilter
:判断所接受到的包是否是伪造的
- ①
lockdown-whitelist.xml
:当Lockdown
设置为yes
时,规定哪些程序可以对firewalld
进行操作direct.xml
:直接使用类似iptables
的语法来进行规则的增删
- 目录:
zones
:保存zone
配置文件services
:保存service
配置文件icmptypes
:保存和icmp
类型相关的配置文件
什么是区域zone
所谓的区域就是一个信赖等级,某一等级下对应有一套规则集。划分方法包括:网络接口、IP地址、端口号等等。一般情况下,会有如下的这些默认区域:
drop
:丢弃所有进入的数据包block
:拒绝所有进入的数据包public
:只接受部分选定的数据包external
:应用在NAT设定时的对外网络dmz
:非军事区work
:使用在公司环境home
:使用在家庭环境internal
:应用在NAT设定时的对内网络trusted
:接受所有的数据包
比如,public
区域由public.xml
文件来配置:
<?xml version="1.0" encoding="utf-8"?>
<zone>
<short>Public</short>
<description>For use in public areas. You do not trust the other computers on networks to not harm your computer. Only selected incoming connections are accepted.</description>
<service name="ssh"/>
<service name="dhcpv6-client"/>
</zone>
当然,你也可以定义自己的区域,只要在zones
目录下新建一个同名的xml
文件即可。
什么是服务service
iptables
时代习惯使用端口号来匹配规则,但是如果某一个服务的端口号改变了,那就要同时更改iptables
的规则,十分不方便,同时也不方便阅读理解。
service
配置文件的命名为<服务名>.xml
,比如ssh
的配置文件是ssh.xml
。
<?xml version="1.0" encoding="utf-8"?>
<service>
<short>SSH</short>
<description>Secure Shell (SSH) is a protocol for logging into and executing commands on remote machines. It provides secure encrypted communications. If you plan on accessing your machine remotely via SSH over a firewalled interface, enable this option. You need the openssh-server package installed for this option to be useful.</description>
<port protocol="tcp" port="22"/>
</service>
配置实例
1.开启firewalld
# systemctl enable firewalld
# systemctl start firewalld
2.往trusted区域中注册IP地址
# firewall-cmd --zone=trusted --add-source=xxx.xxx.xxx.xxx
3.往public区域中注册网络接口
# firewall-cmd --zone=public --add-interface=eth0
4.往public区域中注册新服务
# firewall-cmd --zone=public --add-service=http
# firewall-cmd --zone=public --add-service=https
5.删除public区域中不需要的服务
# firewall-cmd --zone=public --remove-service=dhcpv6-client
# firewall-cmd --zone=public --remove-service=ssh
6.查看各个区域的配置情况
# firewall-cmd --get-active-zones
public
interfaces: eth0
trusted
sources: xxx.xxx.xxx.xxx
7.查看某个区域的服务
# firewall-cmd --zone=public --list-services
# firewall-cmd --zone=trusted --list-services