一.初识firewalld
1.出了点小故障防火墙文件找不到,解决方式如下:
[root@localhost ~]# yum install firewalld -y
[root@localhost ~]# systemctl unmask firewalld
[root@localhost ~]# systemctl enable firewalld
[root@localhost ~]# systemctl start firewalld
2.查找防火墙服务名的技巧
[root@localhost ~]# systemctl list-units |grep fire
firewalld.service loaded active running firewalld - dynamic firewall daemon
3.这个命令,其实是找到一个服务脚本文件
[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 Tue 2023-11-07 04:41:25 CST; 2h 39min ago
Docs: man:firewalld(1)
Main PID: 112106 (firewalld)
CGroup: /system.slice/firewalld.service
└─112106 /usr/bin/python2 -Es /usr/sbin/firewalld --nofork --nopid
Nov 07 04:41:25 localhost.localdomain systemd[1]: Starting firewalld - dynamic firewall daemon...
Nov 07 04:41:25 localhost.localdomain systemd[1]: Started firewalld - dynamic firewall daemon.
Nov 07 04:41:25 localhost.localdomain firewalld[112106]: WARNING: AllowZoneDrifting is enabled. This is considered an insecure configuration o...it now.
Hint: Some lines were ellipsized, use -l to show in full.
4.这个firewalld.service文件在哪?
[root@localhost ~]# find / -type f -name 'firewalld.service'
/usr/lib/systemd/system/firewalld.service
5.这个脚本,其实就是执行了运行防火墙命令的一个脚本文件 ,直接看这个脚本的,第11 12 13行
[root@localhost ~]# cat -n /usr/lib/systemd/system/firewalld.service
1 [Unit]
2 Description=firewalld - dynamic firewall daemon
3 Before=network-pre.target
4 Wants=network-pre.target
5 After=dbus.service
6 After=polkit.service
7 Conflicts=iptables.service ip6tables.service ebtables.service ipset.service
8 Documentation=man:firewalld(1)
9
10 [Service]
11 EnvironmentFile=-/etc/sysconfig/firewalld
12 ExecStart=/usr/sbin/firewalld --nofork --nopid $FIREWALLD_ARGS
13 ExecReload=/bin/kill -HUP $MAINPID
14 # supress to log debug and error output also to /var/log/messages
15 StandardOutput=null
16 StandardError=null
17 Type=dbus
18 BusName=org.fedoraproject.FirewallD1
19 KillMode=mixed
20
21 [Install]
22 WantedBy=multi-user.target
23 Alias=dbus-org.fedoraproject.FirewallD1.service
二.使用防火墙命令,查看系统提供了哪些模板
1.列出区域模板,以及具体信息
[root@localhost ~]# firewall-cmd --help |grep list
[root@localhost ~]# firewall-cmd --list-all-zones
2.列出所有区域的名字
[root@localhost ~]# firewall-cmd --get-zones
block dmz drop external home internal public trusted work
3.列出当前使用的区域
[root@localhost ~]# firewall-cmd --get-default-zone
public
4.列出当前使用的区域,以及详细信息
[root@localhost ~]# firewall-cmd --list-all
public (active)
target: default
icmp-block-inversion: no
interfaces: ens33
sources:
services: dhcpv6-client ssh
ports:
protocols:
masquerade: no
forward-ports:
source-ports:
icmp-blocks:
rich rules:
5.先运行一个80端口的服务
[root@localhost ~]# python -m SimpleHTTPServer 80
6.给当前的防火墙区域,添加一个策略,允许80端口通过
[root@localhost ~]# firewall-cmd --add-port=80/tcp
success
就可以直接在浏览器上访问了,可以看到里面的文件
7.删除,添加的端口规则
[root@localhost ~]# firewall-cmd --remove-port=80/tcp
success
[root@localhost ~]# firewall-cmd --list-all
public (active)
target: default
icmp-block-inversion: no
interfaces: ens33
sources:
services: dhcpv6-client ssh
ports:
protocols:
masquerade: no
forward-ports:
source-ports:
icmp-blocks:
rich rules:
8.针对服务名添加,比如ntp服务
[root@localhost ~]# firewall-cmd --add-service=ntp
success
9.查看当前public区域,使用了哪些规则
[root@localhost ~]# firewall-cmd --list-all
public (active)
target: default
icmp-block-inversion: no
interfaces: ens33
sources:
services: dhcpv6-client ntp ssh
ports:
protocols:
masquerade: no
forward-ports:
source-ports:
icmp-blocks:
rich rules:
10. firewalld,作用其实是添加iptables的规则
查看系统上所有iptables的命令
[root@localhost ~]# iptables -L
tcp 是一个安全可靠的连接,需要双向确认,客户端,和服务端,都要确认对方以及连接上了
udp 是一个不可靠的额连接协议,客户端可以随便给服务端发,不需要对方确认
比如一个很差的网络环境下,网页无法访问,无法做dns解析(网络服务,网站服务,用的都是tcp协议)
但是qq可以收发消息(qq用的是udp协议,以及ntp用的也是udp协议)
11.查看到firewalld命令,添加的防火墙规则如下
[root@localhost ~]# iptables -L | grep ntp
ACCEPT udp -- anywhere anywhere udp dpt:ntp ctstate NEW,UNTRACKED
三.永久修改防火墙规则
1. 永久性添加 8000/tcp的策略
[root@localhost ~]# firewall-cmd --permanent --add-port=8000/tcp
2.需要重新加载firewalld服务
[root@localhost ~]# firewall-cmd --reload
success
3.重新加载后,规则自动生效了
[root@localhost ~]# firewall-cmd --list-all
public (active)
target: default
icmp-block-inversion: no
interfaces: ens33
sources:
services: dhcpv6-client ssh
ports: 8000/tcp
protocols:
masquerade: no
forward-ports:
source-ports:
icmp-blocks:
rich rules:
四.防火墙放行http服务
1.查询支持的所有服务名字有哪些(nginx,httpd,统一被他制作为了 http服务,放行80端口)
[root@localhost ~]# firewall-cmd --get-services
2. 查询当前区域,所用的服务名有哪些
[root@localhost ~]# firewall-cmd --list-services
dhcpv6-client ssh
3. 添加http服务,放行80端口即可
[root@localhost ~]# firewall-cmd --add-service=http
success
4.移除该服务,禁用80端口的请求
[root@localhost ~]# firewall-cmd --remove-service=http
success
5.建议,最好还是直接针对端口号,协议号,添加规则,
firewalld真不好用,不可用
iptables 支持很多复杂的参数,针对协议,来源端口,目标端口,等等
公有云的安全组(阿里云提供的硬件防火墙),也是基于iptables这样的规则添加的