答案是通过firewalld的区域概念配置端口开放与网络规则。首先确认firewalld运行并启用,使用--add-port或--add-service在指定区域(如public)开放端口或服务,配合--permanent实现永久生效,并执行--reload应用配置;若访问仍失败,需排查未重载配置、区域分配错误、应用未监听、SELinux限制及外部防火墙等因素;firewalld的区域按信任级别划分,应遵循最小权限原则选择合适区域;此外,firewalld还支持端口转发、富规则、IP伪装和IP集等高级功能,实现精细化流量控制。

在Linux系统上,利用
firewalld
我个人在使用Linux服务器时,
firewalld
iptables
首先,你得确认
firewalld
sudo systemctl status firewalld
如果它没跑起来,就启动它并设置开机自启:
sudo systemctl start firewalld sudo systemctl enable firewalld
firewalld
public
sudo firewall-cmd --get-active-zones sudo firewall-cmd --list-all --zone=public # 查看public区域的详细配置
假设你想开放一个TCP端口,比如8080,用于你的Web应用。我通常会先在运行时(runtime)添加,测试没问题后再使其永久生效。
sudo firewall-cmd --zone=public --add-port=8080/tcp
这个命令只是临时生效的,系统重启后就会失效。如果你觉得这个端口确实需要一直开放,那就得加上
--permanent
sudo firewall-cmd --zone=public --add-port=8080/tcp --permanent
请记住,带有
--permanent
firewalld
sudo firewall-cmd --reload
如果想开放一个预定义的服务,比如HTTP(端口80)或HTTPS(端口443),
firewalld
sudo firewall-cmd --zone=public --add-service=http --permanent sudo firewall-cmd --zone=public --add-service=https --permanent sudo firewall-cmd --reload
要确认端口或服务是否已经开放,可以再次查看区域的配置:
sudo firewall-cmd --list-all --zone=public
如果某个端口或服务不再需要开放,移除它的方式也很简单,把
--add-port
--add-service
--remove-port
--remove-service
--permanent
--reload
sudo firewall-cmd --zone=public --remove-port=8080/tcp --permanent sudo firewall-cmd --zone=public --remove-service=http --permanent sudo firewall-cmd --reload
这真是个让人头疼的问题,我遇到过不止一次。你明明按照教程把端口开了,
firewall-cmd --list-all
首先,最常见的错误就是忘记firewall-cmd --reload
--permanent
其次,区域(Zone)选择不当。你的服务器可能配置了多个网络接口,或者你的接口被分配到了一个你意想不到的区域。比如,你把端口开在了
public
internal
sudo firewall-cmd --get-active-zones
再者,应用程序本身的问题。防火墙只是网络的第一道关卡,它允许流量进来,但不保证你的应用一定在监听那个端口。你得确保你的服务(比如Nginx、Apache、Node.js应用)确实启动了,并且正在监听你开放的那个端口。我通常会用
sudo netstat -tulnp | grep 8080
sudo ss -tulnp | grep 8080
127.0.0.1
0.0.0.0
还有,SELinux。尤其是在RHEL/CentOS系列系统上,SELinux是个强大的安全机制,它可能会阻止某些服务在非标准端口上运行,即使
firewalld
sudo setsebool -P httpd_can_network_connect_db on
最后,别忘了上游网络设备或云服务商的防火墙。如果你在云服务器上,比如AWS的Security Groups、阿里云的安全组,或者你的网络前面还有硬件防火墙,这些都会在你的Linux服务器防火墙之前拦截流量。这些外部防火墙的规则也需要相应地配置。
firewalld
iptables
一个区域(Zone)实际上是一组预定义的规则集合,它决定了哪些流量可以进入或离开与该区域关联的网络接口。每个网络接口(比如
eth0
enp0s3
firewalld
drop
block
drop
host-prohibited
port-unreachable
public
external
internal
home
internal
work
trusted
那么,该怎么选择呢?这真的取决于你的网络环境和安全需求。
public
internal
home
drop
block
你可以用
sudo firewall-cmd --get-active-zones
eth0
public
internal
sudo firewall-cmd --zone=internal --change-interface=eth0 --permanent sudo firewall-cmd --reload
选择区域的原则是:最小权限原则。只开放你需要的,只信任你确信的。对于大部分服务器,
public
firewalld
一个我经常用到的高级功能是端口转发(Port Forwarding)。有时候,你可能想让外部访问你服务器的80端口,但你的应用实际上运行在8080端口上。这时,你就可以设置一个转发规则:
sudo firewall-cmd --zone=public --add-forward-port=port=80:proto=tcp:toport=8080 --permanent sudo firewall-cmd --reload
这个命令告诉
firewalld
public
另一个强大的特性是富规则(Rich Rules)。富规则提供了比直接添加端口或服务更细粒度的控制,你可以根据源IP地址、目标IP地址、协议、端口、甚至时间等条件来定义复杂的规则。比如,我只想允许特定IP地址访问我的SSH服务:
sudo firewall-cmd --zone=public --add-rich-rule='rule family="ipv4" source address="192.168.1.100" port port="22" protocol="tcp" accept' --permanent sudo firewall-cmd --reload
这条规则就意味着,只有来自
192.168.1.100
firewalld
sudo firewall-cmd --zone=external --add-masquerade --permanent sudo firewall-cmd --reload
此外,你还可以使用
firewalld
对于那些对
iptables
nftables
firewalld
iptables
nftables
firewalld
firewalld
总的来说,
firewalld
以上就是Linux怎么使用firewalld管理端口开放的详细内容,更多请关注php中文网其它相关文章!
每个人都需要一台速度更快、更稳定的 PC。随着时间的推移,垃圾文件、旧注册表数据和不必要的后台进程会占用资源并降低性能。幸运的是,许多工具可以让 Windows 保持平稳运行。
Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号