首页/开发运维/Linux 防火墙命令生成器

Linux 防火墙命令生成器:开放端口 + 保存、重载、开机自启

选好系统和端口,一次生成完整的一套命令——从临时放行、写永久规则,到重载生效、开机自启,全都给你列清楚。支持 firewalld、iptables、UFW、nftables、Windows 防火墙和 CSF 六种常用防火墙,另附 60+ 条常用命令速查。

6 种防火墙系统 开放 / 拒绝端口 规则保存 重载生效 开机自启 一键复制
全部命令需 root / 管理员权限
生成内容含:立即生效 · 永久保存 · 重载生效 · 开机自启

生成的防火墙命令

系统 / 发行版默认防火墙服务名保存规则重载生效开机自启
CentOS 7/8、RHEL 7+、Rocky、Alma、openEuler firewalldfirewalld --permanent 参数即写入永久配置 firewall-cmd --reload systemctl enable firewalld
CentOS 6、Debian 9-、老版 Linux iptablesiptables service iptables save service iptables restart chkconfig iptables on
CentOS 7+ 用 iptables 时 iptables-serviceiptables iptables-save > /etc/sysconfig/iptables systemctl reload iptables systemctl enable iptables
Ubuntu 18.04+、Debian 10+ UFWufw 规则自动写入 /etc/ufw/*.rules ufw reload systemctl enable ufw
Debian 10+、CentOS 8+、openSUSE nftablesnftables nft list ruleset > /etc/nftables.conf systemctl reload nftables systemctl enable nftables
Windows Server 2012+ / Win10 11 Windows Defender 防火墙mpssvc 规则写入注册表,自动持久化 规则即时生效,无需重载 Set-Service mpssvc -StartupType Automatic
cPanel、宝塔企业版、部分安全套件 CSFcsf 改 /etc/csf/csf.conf 的 TCP_IN / UDP_IN csf -r systemctl enable csf
注意:firewalld 与 iptables 同时开启会互相覆盖规则,建议只保留一套。CentOS 7 以上默认用 firewalld;如果你习惯 iptables,需要先 systemctl stop firewalld && systemctl disable firewalld,再安装并启用 iptables-services。修改防火墙前先确认 SSH 端口已放行,避免把自己锁在服务器外面。
防火墙规则为什么重启服务器后就失效了?
大多数临时规则只存在于内存里。firewalld 必须带 --permanent 参数再加 firewall-cmd --reload;iptables 要用 iptables-save 把规则写到 /etc/sysconfig/iptables(或执行 service iptables save);UFW 的 ufw allow 会自动持久化,但需要确认 ufw 服务已设为开机启动;nftables 必须把规则集保存到 /etc/nftables.conf 并启用服务。
“重载(reload)”和“重启(restart)”有什么区别?
重载是重新读取配置文件,不断开已有连接,例如 firewall-cmd --reload、ufw reload、systemctl reload nftables;重启是停掉服务再启动,会短暂中断连接,例如 systemctl restart firewalld。日常改规则优先用重载,只有服务卡死、配置异常时才重启。
开放端口后还是连不上,怎么排查?
按顺序检查:① 防火墙是否在运行(firewall-cmd --state / ufw status);② 规则是否真的加上(firewall-cmd --list-ports);③ 云服务器安全组是否放行同一端口(这一步最常被漏掉);④ 服务是否监听在 0.0.0.0 而不是 127.0.0.1;⑤ 用 ss -lntp 看端口状态,用 telnet IP 端口 做外部连通测试。
怎么只允许指定 IP 访问某个端口?
在生成器里填“来源 IP / 网段”即可。firewalld 用 rich rule:--add-rich-rule='rule family="ipv4" source address="1.2.3.4" port port="3306" protocol="tcp" accept';iptables 加 -s 1.2.3.4;UFW 用 ufw allow from 1.2.3.4 to any port 3306 proto tcp;Windows 加 -RemoteAddress 1.2.3.4。
误封了自己的 IP / SSH 端口怎么办?
如果还能通过控制台(VNC)登录:firewalld 执行 firewall-cmd --panic-off 临时关闭全部规则,或 systemctl stop firewalld;iptables 执行 iptables -F 清空规则;UFW 执行 ufw disable;CSF 执行 csf -f 并 csf -e 关闭。处理完再逐条补回正确规则,别忘了 SSH 端口。
已复制到剪贴板