https://blog.csdn.net/gg_18826075157/article/details/72834694
从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的语法来进行规则的增删
- firewalld.conf:主配置文件(只有5个配置项)
- 目录:
- 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>
- 1
- 2
- 3
- 4
- 5
- 6
- 7
当然,你也可以定义自己的区域,只要在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
- 2
- 3
- 4
- 5
- 6
- 7
配置实例
1.开启firewalld
# systemctl enable firewalld
# systemctl start firewalld
- 1
- 2
2.往trusted区域中注册IP地址
# firewall-cmd --zone=trusted --add-source=xxx.xxx.xxx.xxx
- 1
3.往public区域中注册网络接口
# firewall-cmd --zone=public --add-interface=eth0
- 1
4.往public区域中注册新服务
# firewall-cmd --zone=public --add-service=http
# firewall-cmd --zone=public --add-service=https
- 1
- 2
5.删除public区域中不需要的服务
# firewall-cmd --zone=public --remove-service=dhcpv6-client
# firewall-cmd --zone=public --remove-service=ssh
- 1
- 2
6.查看各个区域的配置情况
# firewall-cmd --get-active-zones
public
interfaces: eth0
trusted
sources: xxx.xxx.xxx.xxx
- 1
- 2
- 3
- 4
- 5
7.查看某个区域的服务
# firewall-cmd --zone=public --list-services
# firewall-cmd --zone=trusted --list-services
- 1
- 2
https://blog.csdn.net/gg_18826075157/article/details/72834694
从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的语法来进行规则的增删
- firewalld.conf:主配置文件(只有5个配置项)
- 目录:
- 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>
- 1
- 2
- 3
- 4
- 5
- 6
- 7
当然,你也可以定义自己的区域,只要在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
- 2
- 3
- 4
- 5
- 6
- 7
配置实例
1.开启firewalld
# systemctl enable firewalld
# systemctl start firewalld
- 1
- 2
2.往trusted区域中注册IP地址
# firewall-cmd --zone=trusted --add-source=xxx.xxx.xxx.xxx
- 1
3.往public区域中注册网络接口
# firewall-cmd --zone=public --add-interface=eth0
- 1
4.往public区域中注册新服务
# firewall-cmd --zone=public --add-service=http
# firewall-cmd --zone=public --add-service=https
- 1
- 2
5.删除public区域中不需要的服务
# firewall-cmd --zone=public --remove-service=dhcpv6-client
# firewall-cmd --zone=public --remove-service=ssh
- 1
- 2
6.查看各个区域的配置情况
# firewall-cmd --get-active-zones
public
interfaces: eth0
trusted
sources: xxx.xxx.xxx.xxx
- 1
- 2
- 3
- 4
- 5
7.查看某个区域的服务
# firewall-cmd --zone=public --list-services
# firewall-cmd --zone=trusted --list-services
- 1
- 2