Firewalld
- Centos 7系统中集成了多款防火墙管理工具,其中firewalld(DynamicFirewall Manager of Linux systems,Linux系统的动态防火墙管理器)服务是默认的防火墙配置管理工具,它拥有基于CLI(命令行界面)和基于GUI(图形用户界面)的两种管理方式。
- 相较于传统的防火墙管理配置工具,firewalld支持动态更新技术并加入了区域(zone)的概念。简单来说,区域就是firewalld预先准备了几套防火墙策略集合(策略模板),用户可以根据生产场景的不同而选择合适的策略集合,从而实现防火墙策略之间的快速切换。例如,我们有一台笔记本电脑,每天都要在办公室、咖啡厅和家里使用。按常理来讲,这三者的安全性按照由高到低的顺序来排列,应该是家庭、公司办公室、咖啡厅。当前,我们希望为这台笔记本电脑指定如下防火墙策略规则:在家中允许访问所有服务;在办公室内仅允许访问文件共享服务;在咖啡厅仅允许上网浏览。在以往,我们需要频繁地手动设置防火墙策略规则,而现在只需要预设好区域集合,然后只需轻点鼠标就可以自动切换了,从而极大地提升了防火墙策略的应用效率
区域默认规则策略
- drop:丢弃所有进入的包,而不给出任何回应
- block:拒绝所有外部发起的链接,允许内部发起的链接
- public:允许指定的进入链接
- external:通public,处理伪装的进入链接,一般用于路由转发
- dmz:允许受限制的进入链接
- work:允许受信任的计算机被限制的进入链接
- home:同work,如果流量和ssh,dhcpv6-client等服务相关,则允许
- internal:同work,范围针对所有互联网用户
- trusted:信任所有链接
- 不同的区域之间的差异是其对待数据包的默认行为不同,firewalld的默认区域为public
Firewalld的基础操作
启动
[root@localhost ~]# systemctl start firewalld.service
查看状态
[root@localhost ~]# systemctl status firewalld.service
● firewalld.service - firewalld - dynamic firewall daemon
Loaded: loaded (/usr/lib/systemd/system/firewalld.service; enabled; vendor preset: enabled)
Active: active (running) since Fri 2020-06-05 13:47:02 CST; 17s ago
Docs: man:firewalld(1)
Main PID: 2388 (firewalld)
CGroup: /system.slice/firewalld.service
└─2388 /usr/bin/python -Es /usr/sbin/firewalld --nofork --nopid
Jun 05 13:47:02 localhost.localdomain systemd[1]: Starting firewalld - dynamic firewall daemon...
Jun 05 13:47:02 localhost.localdomain systemd[1]: Started firewalld - dynamic firewall daemon.
或
[root@localhost ~]# firewall-cmd --state
running
停止
[root@localhost ~]# systemctl disable firewalld.service
Removed symlink /etc/systemd/system/dbus-org.fedoraproject.FirewallD1.service.
Removed symlink /etc/systemd/system/basic.target.wants/firewalld.service.
禁用
[root@localhost ~]# systemctl stop firewalld.service
开放或限制端口
1、开放端口
(1)开放80给所有客户机
[root@localhost ~]# firewall-cmd --add-port=80/tcp --permanent
success
–permanent: 永久生效
(2)重新载入一下防火墙设置,使设置生效
[root@localhost ~]# firewall-cmd --reload
success
(3)查看是否生效
[root@localhost ~]# firewall-cmd --query-port=80/tcp
yes
(4)查看当前系统打开的所有端口
[root@localhost ~]# firewall-cmd --list-ports
80/tcp
2.限制端口
(1)关掉刚刚打开的80端口
[root@localhost ~]# firewall-cmd --remove-port=80/tcp --permanent
success
(2)重新载入一下防火墙设置,使设置生效
[root@localhost ~]# firewall-cmd --reload
success
(3)查看系统所有开放的端口,会发现刚刚开放的80端口已经没了
[root@localhost ~]# firewall-cmd --list-ports
3.批量开放或限制端口
(1)批量开放端口,开放从100到200这之间的端口
[root@localhost ~]# firewall-cmd --add-port=100-200/tcp --permanent
success
(2)重新载入一下防火墙设置,使设置生效
[root@localhost ~]# firewall-cmd --reload
success
(3)查看系统所有开放的端口
[root@localhost ~]# firewall-cmd --list-ports
100-200/tcp
(4)限制
[root@localhost ~]# firewall-cmd --remove-port=100-200/tcp --permanent
success
[root@localhost ~]# firewall-cmd --reload
success
[root@localhost ~]# firewall-cmd --list-ports
开放或限制IP
1.限制IP
(1)限制IP为192.168.1.10的地址禁止访问80端口
[root@localhost ~]# firewall-cmd --permanent --add-rich-rule="rule family="ipv4" source address="192.168.1.10" port protocol="tcp" port="80" reject"
success
[root@localhost ~]# firewall-cmd --reload
success
reject : 拒绝
(2)查看设置的规则
[root@localhost ~]# firewall-cmd --list-rich-rules
rule family="ipv4" source address="192.168.1.10" port port="80" protocol="tcp" reject
或
[root@localhost ~]# firewall-cmd --list-all
public (active)
target: default
icmp-block-inversion: no
interfaces: ens33
sources:
services: dhcpv6-client ssh
ports: 80/tcp
protocols:
masquerade: no
forward-ports:
sourceports:
icmp-blocks:
rich rules:
rule family="ipv4" source address="192.168.1.10" port port="80" protocol="tcp" reject
如设置未生效,可尝试直接编辑规则文件,删掉原来的设置规则,重新载入一下防火墙即可
[root@localhost ~]# vim /etc/firewalld/zones/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="dhcpv6-client"/>
<service name="ssh"/>
<source address="192.168.1.10"/>
<port protocol="tcp" port="80"/>
<reject/>
</rule>
</zone>
2.解除IP地址限制
解除刚才被限制的IP
[root@localhost ~]# firewall-cmd --permanent --add-rich-rule="rule family="ipv4" source address="192.168.1.10" port protocol="tcp" port="80" accept"
success
[root@localhost ~]# firewall-cmd --reload
success
[root@localhost ~]# firewall-cmd --list-rich-rules
rule family="ipv4" source address="192.168.1.10" port port="80" protocol="tcp" accept
accept:允许
3.限制IP地址段
(1)如我们需要限制10.0.0.0-10.0.0.255这一整个段的IP,禁止他们访问80端口
[root@localhost ~]# firewall-cmd --permanent --add-rich-rule="rule family="ipv4" source address="10.0.0.0/24" port protocol="tcp" port="80" reject"
success
[root@localhost ~]# firewall-cmd --reload
success
[root@localhost ~]# firewall-cmd --list-rich-rules
rule family="ipv4" source address="10.0.0.0/24" port port="80" protocol="tcp" reject
(2)接触限制同上一样
[root@localhost ~]# firewall-cmd --permanent --add-rich-rule="rule family="ipv4" source address="10.0.0.0/24" port protocol="tcp" port="80" accept"
success
[root@localhost ~]# firewall-cmd --reload
success
删除设置的规则
[root@localhost ~]# firewall-cmd --permanent --remove-rich-rule="rule family="ipv4" source address="10.0.0.0/24" port port="80" protocol="tcp" reject "
success
[root@localhost ~]# firewall-cmd --reload
success
[root@localhost ~]# firewall-cmd --list-rich-rules
–remove:删除