基础服务管理篇(三)——firewalld防火墙
1.概念
防火墙能够防范一些网络攻击,一般有硬件防火墙和软件防火墙两种
- 硬件防火墙:在硬件级别实现部分防火墙功能,另一部分功能基于软件实现,性能高,成本高
- 软件防火墙:应用软件处理逻辑运行于通用硬件平台之上的防火墙,性能低,成本低
2.作用及功能
防火墙具有很好的保护作用,入侵者必须首先穿越防火墙的安全防线,才能接触目标计算机。
防火墙的功能主要包括:
- 对流经它的网络通信进行扫描,能够过滤掉一些攻击
- 可以关闭不使用的端口
- 能禁止特定端口的流出通信。
- 可以禁止来自特殊站点的访问,从而防止来自不明入侵者的所有通信
3.防火墙的区域
区域是指firewalld预先准备了几套防火墙策略的集合,类似于策略的模板,用户可以根据需求选择区域。
4.firewalld防火墙的配置
4.1 查看、开启、停止firewalld服务
语法: systemctl 选项 firewall
status 检查服务的运行状况
start 启动指定服务
stop 停止指定服务
restart 重启指定服务
reload 重新加载指定服务的配置文件
# 查找防火墙服务名
[root@localhost ~]# systemctl list-units | grep fire
firewalld.service loaded active running firewalld - dynamic firewall daemon
4.2查看firewall配置
语法:firewall-cmd [参数选项1] [参数选项2]
# 查看firewalld.service文件的位置
[root@localhost ~]# find / -type f -name 'firewalld.service'
/usr/lib/systemd/system/firewalld.service
# 查看所有可用区域
[root@localhost ~]# firewall-cmd --get-zones
block dmz drop external home internal public trusted work
# 查看支持的所有服务名字
[root@localhost ~]# firewall-cmd --get-services
# 查询当前区域,所用的服务名
[root@localhost ~]# firewall-cmd --list-services
ssh dhcpv6-client
#列出所有区域的详细配置信息
[root@localhost ~]# firewall-cmd --list-all-zones
block
target: %%REJECT%%
icmp-block-inversion: no
interfaces:
sources:
services:
ports:
protocols:
masquerade: no
forward-ports:
source-ports:
icmp-blocks:
rich rules:
dmz
target: default
icmp-block-inversion: no
interfaces:
sources:
services: ssh
ports:
protocols:
masquerade: no
forward-ports:
source-ports:
icmp-blocks:
rich rules:
drop
target: DROP
icmp-block-inversion: no
interfaces:
sources:
services:
ports:
protocols:
masquerade: no
forward-ports:
source-ports:
icmp-blocks:
rich rules:
# 列出当前使用区域详细配置
[root@localhost ~]# firewall-cmd (--zone=public)--list-all
public
target: default
icmp-block-inversion: no
interfaces:
sources:
services: ssh dhcpv6-client
ports:
protocols:
masquerade: no
forward-ports:
source-ports:
icmp-blocks:
rich rules:
# 列出指定区域的详细配置
[root@localhost ~]# firewall-cmd --zone=home --list-all
home
target: default
icmp-block-inversion: no
interfaces:
sources:
services: ssh mdns samba-client dhcpv6-client
ports:
protocols:
masquerade: no
forward-ports:
source-ports:
icmp-blocks:
rich rules:
# 查看默认使用的区域
[root@localhost ~]# firewall-cmd --get-default-zone
public
4.3管理firewall配置——添加/删除服务
1.临时添加允许通过的服务
# 添加允许ntp服务的防火墙策略
[root@localhost ~]# firewall-cmd --add-service=ntp
success
# 添加允许http服务的防火墙策略
[root@localhost ~]# firewall-cmd --add-service=http
success
[root@localhost ~]# firewall-cmd --list-all
public
target: default
icmp-block-inversion: no
interfaces:
sources:
services: ssh dhcpv6-client http ntp
ports:
protocols:
masquerade: no
forward-ports:
source-ports:
icmp-blocks:
rich rules:
2.临时删除允许通过的服务
# 删除允许http服务的防火墙策略
[root@localhost ~]# firewall-cmd --remove-service=http
success
# 删除允许ntp服务的防火墙策略
[root@localhost ~]# firewall-cmd --remove-service=ntp
success
[root@localhost ~]# firewall-cmd --remove-service=http
success
[root@localhost ~]# firewall-cmd --remove-service=ntp
success
[root@localhost ~]# firewall-cmd --list-all
public
target: default
icmp-block-inversion: no
interfaces:
sources:
services: ssh dhcpv6-client
ports:
protocols:
masquerade: no
forward-ports:
source-ports:
icmp-blocks:
rich rules:
3.永久添加允许通过的服务
# 添加允许ntp服务的防火墙策略
[root@localhost ~]# firewall-cmd --permanent --add-service=ntp
success
# 添加允许http服务的防火墙策略
[root@localhost ~]# firewall-cmd --permanent --add-service=http
success
4.永久删除允许通过的服务
# 删除允许http服务的防火墙策略
[root@localhost ~]# firewall-cmd --permanent --remove-service=http
success
# 删除允许ntp服务的防火墙策略
[root@localhost ~]# firewall-cmd --permanent --remove-service=ntp
success
[root@localhost ~]# firewall-cmd --reload
success
4.4管理firewall配置——添加/删除端口
1.添加端口
[root@localhost ~]# firewall-cmd --zone=public --add-port=80/tcp
success
[root@localhost ~]# firewall-cmd --list-all
public
target: default
icmp-block-inversion: no
interfaces:
sources:
services: ssh dhcpv6-client
ports: 80/tcp
protocols:
masquerade: no
forward-ports:
source-ports:
icmp-blocks:
rich rules:
2.删除端口
[root@localhost ~]# firewall-cmd --zone=public --remove-port=80/tcp
success
[root@localhost ~]# firewall-cmd --list-all
public
target: default
icmp-block-inversion: no
interfaces:
sources:
services: ssh dhcpv6-client
ports:
protocols:
masquerade: no
forward-ports:
source-ports:
icmp-blocks:
rich rules:
临时修改:配置的防火墙策略立即生效,服务器立马放行该端口/服务,但是不写入配置文件,firewall重启后,该配置丢失
永久修改:配置的防火墙策略非立即生效,即命令执行完了不会立即看到效果,但是你的操作被记录到一个配置文件中,每次重启firewall服务,都会再次读取该文件,实现永久生效
firewalld的作用其实是添加iptables的规则,
# 查看系统上所有iptables
[root@localhost ~]# iptables -L
# 查看指定的iptables
[root@localhost ~]# iptables -L |grep ntp
ACCEPT udp -- anywhere anywhere udp dpt:ntp ctstate NEW