Linux服务器防火墙是服务器安全加固的第一道防线,iptables和firewalld是CentOS/RHEL和Ubuntu系统中最常用的两种防火墙管理工具。iptables基于Netfilter框架逐条匹配规则链,firewalld在iptables/nftables之上提供区域(Zone)抽象和动态管理能力。本文分别演示iptables规则编写和firewalld区域配置,覆盖端口放行、IP白名单、NAT转发、规则持久化等生产场景。
iptables五链四表结构与数据包流向
iptables的规则组织在五个链(Chain)中:PREROUTING、INPUT、FORWARD、OUTPUT、POSTROUTING。数据包进入网卡后依次经过PREROUTING(路由前处理)、INPUT(发往本机)或FORWARD(转发到其他主机)、OUTPUT(本机发出)、POSTROUTING(路由后处理)。
四张表按优先级依次处理:raw(连接跟踪豁免)-> mangle(数据包修改)-> nat(地址转换)-> filter(过滤)。filter表是最常用的表,包含INPUT、FORWARD、OUTPUT三条链。
iptables基础规则编写
# 查看当前规则(带行号)
iptables -L -n --line-numbers
# 清空所有规则
iptables -F
iptables -X
# 设置默认策略:入站丢弃,转发丢弃,出站允许
iptables -P INPUT DROP
iptables -P FORWARD DROP
iptables -P OUTPUT ACCEPT
# 允许回环接口
iptables -A INPUT -i lo -j ACCEPT
# 允许已建立连接和相关连接的数据包
iptables -A INPUT -m state --state ESTABLISHED,RELATED -j ACCEPT
# 允许SSH(22端口)和HTTP/HTTPS
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
# 允许ICMP(ping)
iptables -A INPUT -p icmp --icmp-type echo-request -j ACCEPT
iptables IP白名单与限速配置
生产环境中常需限制SSH仅允许管理IP访问,并对异常流量进行限速:
# SSH仅允许192.168.1.0/24网段访问
iptables -A INPUT -p tcp -s 192.168.1.0/24 --dport 22 -j ACCEPT
# 禁止某个IP访问所有服务
iptables -A INPUT -s 10.0.0.66 -j DROP
# SSH防暴力破解:每分钟最多5个新连接
iptables -A INPUT -p tcp --dport 22 -m state --state NEW \
-m recent --set --name SSH
iptables -A INPUT -p tcp --dport 22 -m state --state NEW \
-m recent --update --seconds 60 --hitcount 6 --name SSH \
-j DROP
# HTTP限速:单个IP每秒最多20个新连接
iptables -A INPUT -p tcp --dport 80 -m state --state NEW \
-m limit --limit 20/s --limit-burst 40 -j ACCEPT
iptables -A INPUT -p tcp --dport 80 -m state --state NEW -j DROP
iptables NAT转发与端口映射
# 开启内核转发
echo 1 > /proc/sys/net/ipv4/ip_forward
# SNAT:内网机器通过本机访问外网(eth0为外网网卡)
iptables -t nat -A POSTROUTING -o eth0 -j MASQUERADE
# DNAT:将外网8080端口转发到内网192.168.1.100:80
iptables -t nat -A PREROUTING -p tcp --dport 8080 \
-j DNAT --to-destination 192.168.1.100:80
iptables -t nat -A POSTROUTING -p tcp -d 192.168.1.100 --dport 80 \
-j SNAT --to-source 192.168.1.1
# 端口重定向:本机80转发到8080
iptables -t nat -A PREROUTING -p tcp --dport 80 -j REDIRECT --to-port 8080
iptables规则持久化保存
# CentOS/RHEL:安装iptables-services
yum install -y iptables-services
systemctl enable iptables
service iptables save
# 规则保存到 /etc/sysconfig/iptables
# Ubuntu/Debian:安装iptables-persistent
apt install -y iptables-persistent
# 保存规则
netfilter-persistent save
# 规则保存到 /etc/iptables/rules.v4
# 手动导出和导入
iptables-save > /etc/iptables/rules.v4
iptables-restore < /etc/iptables/rules.v4
firewalld区域管理与富规则配置
firewalld预定义了public、trusted、home、work、dmz等区域,每个区域有不同的默认策略。网卡绑定到区域后继承该区域的规则。
# 查看所有区域
firewall-cmd --get-zones
# 查看默认区域
firewall-cmd --get-default-zone
# 查看public区域的详情
firewall-cmd --zone=public --list-all
# 将eth0加入trusted区域(信任所有流量)
firewall-cmd --permanent --zone=trusted --add-interface=eth0
# 在public区域开放端口
firewall-cmd --permanent --zone=public --add-port=80/tcp
firewall-cmd --permanent --zone=public --add-port=443/tcp
firewall-cmd --permanent --zone=public --add-port=22/tcp
# 开放服务(服务定义在/usr/lib/firewalld/services/)
firewall-cmd --permanent --zone=public --add-service=http
firewall-cmd --permanent --zone=public --add-service=https
firewall-cmd --permanent --zone=public --add-service=ssh
# 重新加载配置(不中断现有连接)
firewall-cmd --reload
firewalld富规则(Rich Rules)实战
富规则提供比基本端口/服务更精细的控制,支持源IP、日志、限速等条件组合:
# 仅允许192.168.1.0/24访问SSH
firewall-cmd --permanent --zone=public \
--add-rich-rule='rule family="ipv4" source address="192.168.1.0/24" service name="ssh" accept'
# 拒绝某个IP的所有流量
firewall-cmd --permanent --zone=public \
--add-rich-rule='rule family="ipv4" source address="10.0.0.66" reject'
# 限速:每分钟最多5个SSH连接,超过则记录日志并丢弃
firewall-cmd --permanent --zone=public \
--add-rich-rule='rule service name="ssh" limit value="5/m" accept'
firewall-cmd --permanent --zone=public \
--add-rich-rule='rule service name="ssh" limit value="5/m" log prefix="ssh_dropped" level="warning" drop'
# 端口转发:80转到8080
firewall-cmd --permanent --zone=public \
--add-forward-port=port=80:proto=tcp:toport=8080
# 跨机端口转发
firewall-cmd --permanent --zone=public \
--add-forward-port=port=80:proto=tcp:toaddr=192.168.1.100:toport=80
firewall-cmd --reload
iptables与firewalld选型建议
iptables适合需要精确控制每条规则顺序、使用NAT/路由场景的服务器,如网关、负载均衡器。firewalld适合常规应用服务器,动态管理不需要重启服务、规则变更不影响现有连接。两者不要同时运行,firewalld底层也使用nftables/iptables,同时操作会产生冲突。CentOS 7+默认使用firewalld,可通过systemctl stop firewalld && systemctl disable firewalld切换回iptables。
无论使用哪种工具,防火墙配置的核心原则是默认拒绝(Default Deny):先设置DROP默认策略,再逐条放行需要的端口和服务。配置完成后务必通过nmap或telnet从外部验证端口开放情况,避免误封SSH导致无法远程登录。
原创文章,作者:小编,如若转载,请注明出处:https://www.yunthe.com/linux-fu-wu-qi-fang-huo-qiang-pei-zhi-shi-zhan-iptables-gui/