防火墙是保护系统安全的重要组成部分,它可以帮助阻止未授权的访问。本文介绍了如何在不同的 Linux 系统中开启和管理防火墙,包括 Ubuntu/Debian、CentOS/RHEL、openSUSE 和 Arch Linux 系统。我们详细讲解了每个系统中常用的防火墙工具和命令,例如 ufw、firewalld、SuSEfirewall2、iptables 和 nftables。通过这些步骤,你可以在各种 Linux 发行版上有效地开启防火墙并配置规则,从而增强系统的安全性。
Ubuntu/Debian 系统 Ubuntu 和 Debian 系统默认使用 ufw(Uncomplicated Firewall)作为防火墙工具。
检查防火墙状态
开启防火墙
禁用防火墙
配置示例 允许 SSH 连接:
CentOS/RHEL 系统 CentOS 和 RHEL 系统默认使用 firewalld 作为防火墙工具。
检查防火墙状态 sudo systemctl status firewalld
开启防火墙 sudo systemctl start firewalldsudo systemctl enable firewalld
禁用防火墙 sudo systemctl stop firewalldsudo systemctl disable firewalld
配置示例 允许 HTTP 服务:
sudo firewall-cmd --permanent --add-service=httpsudo firewall-cmd --reload
openSUSE 系统 openSUSE 系统默认使用 firewalld,但某些版本可能使用 SuSEfirewall2。
使用 firewalld 检查防火墙状态 sudo systemctl status firewalld
开启防火墙 sudo systemctl start firewalldsudo systemctl enable firewalld
配置示例 允许 HTTPS 服务:
sudo firewall-cmd --permanent --add-service=httpssudo firewall-cmd --reload
使用 SuSEfirewall2 检查防火墙状态 sudo SuSEfirewall2 status
开启防火墙
Arch Linux Arch Linux 通常使用 iptables 或 nftables 来管理防火墙。
使用 iptables 检查防火墙规则
添加规则并保存 允许所有来自端口 22 的连接:
sudo iptables -A INPUT -p tcp --dport 22 -j ACCEPTsudo iptables-save > /etc/iptables/iptables.rules
开启防火墙 sudo systemctl start iptablessudo systemctl enable iptables
使用 nftables 检查防火墙状态
添加规则并保存 允许 HTTP 流量:
sudo nft add rule ip filter input tcp dport 80 acceptsudo nft list ruleset > /etc/nftables.conf
启用 nftables 服务 sudo systemctl start nftablessudo systemctl enable nftables
常见问题排查
通过以上命令,你可以在不同的 Linux 系统中有效地开启防火墙并配置规则,从而增强系统的安全性。