在现代 Linux 系统中,nftables 正逐步取代传统的 iptables,成为新一代的包过滤框架。作为 Red Hat Enterprise Linux 的社区版,RockyLinux 默认采用 nftables 作为其防火墙解决方案。本文将手把手教你如何使用 RockyLinux nftables命令 来配置和管理防火墙规则,即使你是零基础的小白也能轻松上手!
相比旧版 iptables,nftables 具有以下优势:
首先,请确保你使用的是 RockyLinux 8 或更高版本,并以 root 用户或具有 sudo 权限的用户登录。
检查 nftables 是否已安装:
rpm -q nftables 如果未安装,可使用以下命令安装:
sudo dnf install nftables -y 在 nftables 中,防火墙规则组织为三层结构:
下面我们创建一个简单的防火墙规则集,允许 SSH(端口22)和 HTTP(端口80),拒绝其他所有入站连接。
1. 清空现有规则(谨慎操作!):
sudo nft flush ruleset 2. 创建一个新的 IPv4 表:
sudo nft add table ip filter 3. 在表中添加 input 链:
sudo nft add chain ip filter input { type filter hook input priority 0 \; } 4. 添加具体规则:
sudo nft add rule ip filter input ct state established,related acceptsudo nft add rule ip filter input tcp dport 22 acceptsudo nft add rule ip filter input tcp dport 80 acceptsudo nft add rule ip filter input icmp type echo-request acceptsudo nft add rule ip filter input drop 上述规则含义如下:
当前规则仅在内存中生效,重启后会丢失。要使其永久生效,需保存到配置文件:
sudo nft list ruleset > /etc/nftables.conf 然后启用并启动 nftables 服务:
sudo systemctl enable --now nftables nft list rulesetnft delete rule ip filter input handle 3nft flush ruleset通过本教程,你已经掌握了 RockyLinux nftables命令 的基本用法,并能独立配置一个安全的防火墙策略。nftables 作为 新一代Linux防火墙 工具,不仅功能强大,而且语法清晰。建议你在测试环境中多加练习,熟练掌握后即可在生产环境中部署。
如果你正在寻找一份完整的 RockyLinux防火墙教程,希望本文能满足你的需求。记住,良好的网络安全始于合理的 nftables防火墙配置!
本文由主机测评网于2025-12-12发表在主机测评网_免费VPS_免费云服务器_免费独立服务器,如有疑问,请联系我们。
本文链接:https://www.vpshk.cn/2025126434.html