firewalld(推荐)
firewalld 是动态防火墙,修改规则无需重启服务。
# 查看当前区域和规则
firewall-cmd --list-all
# 开放 8080 端口
firewall-cmd --add-port=8080/tcp --permanent
firewall-cmd --reload
# 开放服务(如 http)
firewall-cmd --add-service=http --permanentiptables(底层工具)
# 允许 SSH
iptables -A INPUT -p tcp --dport 22 -j ACCEPT
# 拒绝所有其他入站
iptables -P INPUT DROP
# 保存规则(CentOS)
service iptables save实践建议
- 生产环境建议使用 firewalld,更安全易用
- 云服务器通常使用安全组,可结合 firewalld 做二次防护
- 修改前先添加自己的 IP 白名单,避免把自己锁在外面
