firewalld[^1] Concepts firewalld 采用区域与服务的概念来控制流量的出入。
firewalld 是 CentOS 7/8, Red Hat Enterprise Linux 7 (RHEL 7), Fedora 18+等 Linux 发行版本默认防火墙程序。 其命令行工具为 firewall-cmd
Zones
zones 预设级别不同的防火墙策略,用户可以自定义 zones,也可以使用预设的 zones。 默认使用 public zone 根据拦截级别严格程度,预设 zones 如下
zone
拦截说明
drop
丢弃区,所有接入连接都会进行无消息响应拦截,只有输出连接被允许。
block
拦截区,接入连接都会被拦截,会响应 icmp-host-prohibited 等拦截消息,只有输出连接被允许。
public
公共区,用于不受信任的公共区域,仅允许自选接入连接,默认允许 ssh 和 dhcpv6-client
external
外部区,用于外部网络,系统充当网关角色,仅允许自选接入连接,默认允许 ssh
internal
内部区,用于内部网络,系统充当网关角色,仅允许自选接入连接
dmz
隔离区,demilitarized zone(隔离区,军事缓冲区),用于允许部分服务被外网访问,仅允许自选接入连接
work
工作区,用于工作网络,信任网络中其他机器,仅允许自选接入连接,默认允许 ssh,ipp-client 和 dhcpv6-client
home
家庭区,用于工作家庭网络,信任网络中其他机器,仅允许自选接入连接,默认允许 ssh,ipp-client,mdns,samba-client 和 dhcpv6-client
trusted
信任区,并接受所有网络连接
sudo firewall-cmd --new-zone=memcached --permanent
sudo firewall-cmd --get-default-zone
sudo firewall-cmd --set -default-zone=home
sudo firewall-cmd --get-zones
sudo firewall-cmd --list-all-zones
sudo firewall-cmd --get-active-zones
sudo firewall-cmd --zone=public --list-all
Services services 在 zones 中为不同的服务预设不同的配置。 例如,可以为服务提供不同的端口等配置
sudo firewall-cmd --get-services
sudo firewall-cmd --list-services
sudo firewall-cmd --zone=public --add-service=http
sudo firewall-cmd --zone=public --remove-service=http
所有服务的配置都在/usr/lib/firewalld/services 目录下 例如 http 服务的配置为
<?xml version="1.0" encoding="utf-8"?> <service > <short > WWW (HTTP)</short > <description > HTTP is the protocol used to serve Web pages. If you plan to make your Web server publicly available, enable this option. This option is not required for viewing pages locally or developing Web pages.</description > <port protocol ="tcp" port ="80" /> </service >
sudo cp /usr/lib/firewalld/services/ssh.xml /etc/firewalld/services/diyservice.xml
<?xml version="1.0" encoding="utf-8"?> <service version ="1.0" > <short > diyservice</short > <description > Plex is a streaming media server that brings all your video, music and photo collections together and stream them to your devices at anytime and from anywhere.</description > <port protocol ="udp" port ="1900" /> <port protocol ="tcp" port ="32400" /> </service >
重启防火墙后就可以看到该服务。
Runtime Firewalld 提供运行时与永久两种配置有效期限。
runtime 只有当次开机有效,重启会恢复到默认配置,permanent 会让配置永久生效。 默认指定防火墙相关配置时使用的是 runtime。 要让指定的配置永久有效,需要指定 –permanent
sudo firewall-cmd <options>
上述配置生效后只在 runtime 有效
sudo firewall-cmd --permanent <options>
sudo firewall-cmd --runtime-to-permanent
sudo firewall-cmd --reload
State
sudo systemctl stop firewalld
上述操作只对当前运行时有效
sudo systemctl stop firewalld
零时关闭防火墙并防止开机重启
sudo systemctl disable firewalld
防止其他程序启动防火墙
sudo systemctl mask --now firewalld
sudo systemctl start firewalld
Port
firewall-cmd --permanent --add-port 8300-8400/tcp
协议可选 tcp, udp, sctp, or dccp
firewall-cmd --permanent --add-port 1191/tcp
临时暴露需要去除 --permanent
,重启后端口暴露将会失效。 执行暴露端口的命令后,需要重启防火墙
firewall-cmd --list-ports
firewall-cmd --remove-port 8300-8400/tcp --permanent
注意开启时添加了--permanent
,关闭时也需要
sudo firewall-cmd --list-services
Source
sudo firewall-cmd --zone=public --add-source=192.168.100.30/32 --permanent
sudo firewall-cmd --zone=public --list-sources
sudo firewall-cmd --zone=public --remove-source=192.168.1.10
在进行转发前,需要开启转发支持
sudo firewall-cmd --zone=external --add-masquerade --permanent
同主机
sudo firewall-cmd --zone=external --add-forward-port=port=80:proto=tcp:toport=8080
上述命令会把 80 端口的 tcp 全部转发到当前服务器的 8080 端口
转发到其他主机
sudo firewall-cmd --zone=external --add-forward-port=port=80:proto=tcp:toaddr=10.10.10.2
上述命令会把 80 端口的 tcp 全部转发到服务器 10.10.10.2 的 80 端口 [^1]: 本节命令在 centos7 下验证
BMW WARNING
本文首发于 skyline.show 欢迎访问。
I am a bucolic migrant worker but I never walk backwards.
参考资料如下列出,部分引用可能遗漏或不可考,侵删。
https://linuxize.com/post/how-to-configure-and-manage-firewall-on-centos-8/
本文作者: Skyline(lty) 授权声明: 本博客所有文章除特别声明外, 均采用 CC BY - NC - SA 3.0 协议。 转载请注明出处!
CC BY - NC - SA 3.0
打赏支持
如果觉得我的文章对您有用,请随意打赏。您的支持将鼓励我继续创作!