Linux Minimal 配置防火墙实操指南
一、先确认系统与防火墙栈
cat /etc/os-release二、RHEL/CentOS Minimal 使用 firewalld(推荐)
sudo yum install -y firewalld(或 sudo dnf install -y firewalld)sudo systemctl start firewalld && sudo systemctl enable firewalldsudo systemctl status firewalldsudo firewall-cmd --permanent --zone=public --add-port=8080/tcpsudo firewall-cmd --permanent --zone=public --add-service=http 与 --add-service=httpssudo firewall-cmd --reloadsudo firewall-cmd --list-all 或按端口:sudo firewall-cmd --list-portssudo firewall-cmd --permanent --zone=public --remove-port=8080/tcp 或 --remove-service=httpsudo firewall-cmd --permanent --add-service=ssh && sudo firewall-cmd --reload。三、Debian/Ubuntu Minimal 使用 UFW(若未安装先安装)
sudo apt update && sudo apt install -y ufwsudo ufw enable(首次启用会设置开机自启)sudo ufw status verbosesudo ufw allow 22/tcp(SSH),sudo ufw allow 80,443/tcp(HTTP/HTTPS)sudo ufw allow from 192.168.1.0/24sudo ufw delete allow 22/tcpsudo ufw default deny incoming 与 sudo ufw default allow outgoing四、RHEL/CentOS 7 Minimal 使用 iptables(传统方式)
sudo yum install -y iptables-servicessudo systemctl stop firewalld && sudo systemctl disable firewalldsudo systemctl start iptables && sudo systemctl enable iptablessudo iptables -A INPUT -i lo -j ACCEPT 与 -A INPUT -m state --state ESTABLISHED,RELATED -j ACCEPTsudo iptables -A INPUT -p tcp --dport 22 -j ACCEPTsudo iptables -A INPUT -p tcp --dport 80,443 -j ACCEPTsudo iptables -P INPUT DROPsudo service iptables save 或 sudo iptables-save > /etc/sysconfig/iptablessudo iptables-restore < /etc/sysconfig/iptables五、安全与排错要点