在 Linux 下,“防火墙”通常指 iptables / nftables,或者发行版自带的简化工具(firewalld / ufw)。下面按常见场景和工具给你一套实用配置说明。
# 查看是否使用 firewalld
systemctl status firewalld
# 查看是否使用 ufw(Ubuntu 常见)
ufw status
# 查看 iptables 是否有规则
iptables -L -n
sudo ufw enable
sudo ufw status
sudo ufw allow 22/tcp # SSH
sudo ufw allow 80/tcp
sudo ufw allow 443/tcp
sudo ufw deny 3306/tcp
sudo ufw delete allow 80/tcp
sudo systemctl start firewalld
sudo systemctl enable firewalld
sudo firewall-cmd --permanent --add-port=80/tcp
sudo firewall-cmd --permanent --add-port=443/tcp
sudo firewall-cmd --reload
sudo firewall-cmd --permanent --add-service=ssh
sudo firewall-cmd --reload
sudo firewall-cmd --list-all
iptables -A INPUT -p tcp --dport 22 -j ACCEPT
iptables -A INPUT -p tcp --dport 80 -j ACCEPT
iptables -A INPUT -p tcp --dport 443 -j ACCEPT
iptables -A INPUT -i lo -j ACCEPT
iptables -P INPUT DROP
iptables -P FORWARD DROP
iptables -P OUTPUT ACCEPT
# CentOS
service iptables save
# Ubuntu
iptables-save > /etc/iptables.rules
✅ 永远先放行 SSH(22) 再改默认策略
✅ 生产服务器默认 DROP INPUT
✅ 不用的端口全部拒绝
✅ 云服务器还要配合 安全组
我可以直接给你现成配置,只要你说:
例如:
“CentOS 7,跑 Nginx 和 MySQL,只让公司 IP 访问数据库”