在 Rocky Linux 9 中,可使用 firewalld
配置端口过滤规则。例如,限制 SSH 端口仅允许特定 IP 访问:
firewall-cmd --permanent --add-rich-rule='rule family="ipv4" source address="192.168.1.100" port protocol="tcp" port="22" accept'
firewall-cmd --reload
延伸知识点:firewalld 的富规则(Rich Rules) 富规则允许复杂条件配置:
family
:协议族(ipv4/ipv6)source address
:源 IP 或网段port/protocol
:端口及协议类型action
:accept/reject/drop 例:拒绝 192.168.1.0/24 以外的所有 HTTP 请求:firewall-cmd --permanent --add-rich-rule='rule family=ipv4 source not address=192.168.1.0/24 service name=http drop'
使用--list-rich-rules
查看规则,--remove-rich-rule
删除规则。