防火墙配置
最后更新: 2026-01-12
作者: Linux Team
页面目录
目录
防火墙概述
防火墙是网络安全的第一道防线,用于控制进出网络流量。
┌─────────────────────────────────────────────────────────────┐
│ 防火墙工作原理 │
├─────────────────────────────────────────────────────────────┤
│ │
│ 入站请求 ──→ 防火墙 ──→ 规则匹配 │
│ │ │
│ ┌─────────┼─────────┐ │
│ ↓ ↓ ↓ │
│ ACCEPT DROP REJECT │
│ │ │ │ │
│ ↓ ↓ ↓ │
│ 允许通过 丢弃无响应 拒绝返回 │
│ │
└─────────────────────────────────────────────────────────────┘
iptables
基础概念
┌─────────────────────────────────────────────────────────────┐
│ iptables 架构 │
├─────────────────────────────────────────────────────────────┤
│ │
│ Tables ──→ Chains ──→ Rules │
│ │
│ Tables: │
│ ├── filter ── 数据包过滤(默认) │
│ ├── nat ──── 网络地址转换 │
│ ├── mangle ── 数据包修改 │
│ └── raw ──── 连接跟踪 exemptions │
│ │
│ Chains (filter表): │
│ ├── INPUT ─── 目标为本机的包 │
│ ├── OUTPUT ── 源自本机的包 │
│ ├── FORWARD ── 需要转发的包 │
│ └── (PREROUTING, POSTROUTING in nat/mangle) │
│ │
└─────────────────────────────────────────────────────────────┘
基本命令
# 查看规则
sudo iptables -L # 列出所有规则
sudo iptables -L -n # 数字格式
sudo iptables -L -n -v # 详细输出
sudo iptables -L INPUT # 指定链
sudo iptables -t nat -L # nat表
sudo iptables -L -n --line-numbers # 显示行号
# 追加规则 -A
sudo iptables -A INPUT -p tcp --dport 22 -j ACCEPT
# 插入规则 -I
sudo iptables -I INPUT 1 -p tcp --dport 22 -j ACCEPT
# 删除规则 -D
sudo iptables -D INPUT -p tcp --dport 22 -j ACCEPT
sudo iptables -D INPUT 3 # 删除第3条
# 替换规则 -R
sudo iptables -R INPUT 3 -p tcp --dport 22 -j ACCEPT
# 清空规则 -F
sudo iptables -F # 清空filter表
sudo iptables -F INPUT # 清空INPUT链
sudo iptables -t nat -F # 清空nat表
# 设置默认策略 -P
sudo iptables -P INPUT DROP
sudo iptables -P FORWARD DROP
sudo iptables -P OUTPUT ACCEPT
常用规则
# 允许已建立连接的包
sudo iptables -A INPUT -m state --state ESTABLISHED,RELATED -j ACCEPT
# 允许本地回环
sudo iptables -A INPUT -i lo -j ACCEPT
# 允许SSH (端口22)
sudo iptables -A INPUT -p tcp --dport 22 -j ACCEPT
# 允许HTTP/HTTPS
sudo iptables -A INPUT -p tcp --dport 80 -j ACCEPT
sudo iptables -A INPUT -p tcp --dport 443 -j ACCEPT
# 允许ping
sudo iptables -A INPUT -p icmp --icmp-type echo-request -j ACCEPT
# 允许特定IP访问
sudo iptables -A INPUT -s 192.168.1.100 -j ACCEPT
# 允许端口范围
sudo iptables -A INPUT -p tcp --dport 1000:2000 -j ACCEPT
# 拒绝并返回错误
sudo iptables -A INPUT -p tcp --dport 3306 -j REJECT
# 丢弃(无响应)
sudo iptables -A INPUT -p tcp --dport 3306 -j DROP
连接状态
┌─────────────────────────────────────────────────────────────┐
│ 连接状态 │
├─────────────────────────────────────────────────────────────┤
│ │
│ NEW ── 新建连接请求 │
│ ESTABLISHED ── 已建立的连接 │
│ RELATED ── 关联的连接(如FTP数据连接) │
│ INVALID ── 无效的连接状态 │
│ UNTRACKED ── 未被跟踪的连接 │
│ │
└─────────────────────────────────────────────────────────────┘
NAT配置
# 开启IP转发
echo 1 | sudo tee /proc/sys/net/ipv4/ip_forward
sudo sysctl -w net.ipv4.ip_forward=1
# 永久开启IP转发
echo "net.ipv4.ip_forward = 1" | sudo tee -a /etc/sysctl.conf
# SNAT(源地址转换)- 伪装
sudo iptables -t nat -A POSTROUTING -s 192.168.1.0/24 -o eth0 -j MASQUERADE
# DNAT(目标地址转换)- 端口转发
sudo iptables -t nat -A PREROUTING -p tcp --dport 80 -i eth0 -j DNAT --to-destination 192.168.1.100:8080
# 端口重定向
sudo iptables -t nat -A PREROUTING -p tcp --dport 8080 -j REDIRECT --to-port 80
保存和恢复
# Debian/Ubuntu - 保存规则
sudo apt install iptables-persistent
sudo netfilter-persistent save
# CentOS/RHEL
sudo service iptables save
sudo systemctl restart iptables
# 手动保存
sudo iptables-save > /etc/iptables.rules
# 恢复规则
sudo iptables-restore < /etc/iptables.rules
# 开机自动加载
# /etc/network/interfaces
iface eth0 inet static
pre-up iptables-restore < /etc/iptables.rules
iptables脚本示例
#!/bin/bash
# firewall.sh - iptables配置脚本
# 清空现有规则
sudo iptables -F
sudo iptables -X
sudo iptables -t nat -F
sudo iptables -t nat -X
# 设置默认策略
sudo iptables -P INPUT DROP
sudo iptables -P FORWARD DROP
sudo iptables -P OUTPUT ACCEPT
# 允许本地回环
sudo iptables -A INPUT -i lo -j ACCEPT
# 允许已建立连接
sudo iptables -A INPUT -m state --state ESTABLISHED,RELATED -j ACCEPT
# 允许SSH
sudo iptables -A INPUT -p tcp --dport 22 -j ACCEPT
# 允许HTTP/HTTPS
sudo iptables -A INPUT -p tcp --dport 80 -j ACCEPT
sudo iptables -A INPUT -p tcp --dport 443 -j ACCEPT
# 允许ping
sudo iptables -A INPUT -p icmp --icmp-type echo-request -j ACCEPT
# 允许特定IP段
sudo iptables -A INPUT -s 192.168.1.0/24 -j ACCEPT
# 日志Drop的包
sudo iptables -A INPUT -m limit --limit 5/min -j LOG --log-prefix "iptables-dropped: " --log-level 4
# 丢弃其他所有
sudo iptables -A INPUT -j DROP
echo "Firewall rules applied successfully!"
firewalld (CentOS/RHEL/Fedora)
基本概念
┌─────────────────────────────────────────────────────────────┐
│ firewalld 概念 │
├─────────────────────────────────────────────────────────────┤
│ │
│ Zones ── 预定义规则集合 │
│ ├── drop ── 丢弃所有 │
│ ├── block ── 拒绝所有 │
│ ├── public ── 公共区域 │
│ ├── external ── 外部网络 │
│ ├── dmz ── DMZ区域 │
│ ├── work ── 工作区域 │
│ ├── home ── 家庭区域 │
│ ├── internal ── 内部网络 │
│ └── trusted ── 信任区域 │
│ │
│ Services ── 预定义服务 │
│ └── ssh, dhcpv6-client, samba, http, https, ... │
│ │
└─────────────────────────────────────────────────────────────┘
基本命令
# 查看状态
sudo firewall-cmd --state
sudo systemctl status firewalld
# 查看默认zone
sudo firewall-cmd --get-default-zone
# 查看活动zone
sudo firewall-cmd --get-active-zones
# 查看zone信息
sudo firewall-cmd --zone=public --list-all
# 查看所有可用zone
sudo firewall-cmd --get-zones
# 查看所有服务
sudo firewall-cmd --get-services
运行时配置
# 设置默认zone
sudo firewall-cmd --set-default-zone=public
# 添加服务
sudo firewall-cmd --zone=public --add-service=ssh
sudo firewall-cmd --zone=public --add-service=http
sudo firewall-cmd --zone=public --add-service=https
# 移除服务
sudo firewall-cmd --zone=public --remove-service=http
# 添加端口
sudo firewall-cmd --zone=public --add-port=8080/tcp
sudo firewall-cmd --zone=public --add-port=1000-2000/tcp
# 移除端口
sudo firewall-cmd --zone=public --remove-port=8080/tcp
# 允许IP/网段
sudo firewall-cmd --zone=public --add-source=192.168.1.0/24
sudo firewall-cmd --zone=public --add-rich-rule='rule family="ipv4" source address="192.168.1.100" accept'
永久配置
# 永久生效(需reload)
sudo firewall-cmd --permanent --zone=public --add-service=http
sudo firewall-cmd --permanent --zone=public --add-port=8080/tcp
sudo firewall-cmd --reload
# 或者使用 --add-service --zone=public --permanent
# 查看永久配置
sudo firewall-cmd --permanent --zone=public --list-all
# 移除永久配置
sudo firewall-cmd --permanent --zone=public --remove-service=http
sudo firewall-cmd --reload
富规则(Rich Rules)
# 允许特定IP的所有连接
sudo firewall-cmd --zone=public --add-rich-rule='rule family="ipv4" source address="192.168.1.100" accept'
# 允许特定IP访问特定端口
sudo firewall-cmd --zone=public --add-rich-rule='rule family="ipv4" source address="192.168.1.100" port port="3306" protocol="tcp" accept'
# 拒绝特定IP
sudo firewall-cmd --zone=public --add-rich-rule='rule family="ipv4" source address="10.0.0.0/8" reject'
# 端口转发
sudo firewall-cmd --zone=external --add-masquerade
sudo firewall-cmd --zone=external --add-forward-port=port=80:proto=tcp:toport=8080:toaddr=192.168.1.100
# 日志记录
sudo firewall-cmd --zone=public --add-rich-rule='rule service name="ssh" log prefix="SSH Access: " level="info" accept'
# 查看所有富规则
sudo firewall-cmd --list-rich-rules
direct规则
# 直接添加iptables规则
sudo firewall-cmd --direct --add-rule ipv4 filter INPUT 0 -p tcp --dport 22 -j ACCEPT
# 查看direct规则
sudo firewall-cmd --direct --get-all-rules
# 移除direct规则
sudo firewall-cmd --direct --remove-rule ipv4 filter INPUT 0 -p tcp --dport 22 -j ACCEPT
UFW (Ubuntu/Debian)
基本命令
# 查看状态
sudo ufw status
sudo ufw status verbose
# 启用/禁用
sudo ufw enable
sudo ufw disable
# 默认策略
sudo ufw default deny incoming # 拒绝入站
sudo ufw default allow outgoing # 允许出站
sudo ufw default deny forward # 拒绝转发
# 重置
sudo ufw reset
允许/拒绝规则
# 允许服务
sudo ufw allow ssh
sudo ufw allow http
sudo ufw allow https
# 允许端口
sudo ufw allow 22/tcp
sudo ufw allow 80/tcp
sudo ufw allow 443/tcp
sudo ufw allow 1000:2000/tcp # 端口范围
# 允许应用
sudo ufw allow OpenSSH
sudo ufw allow 'Nginx Full'
# 拒绝端口
sudo ufw deny 3306/tcp
# 允许特定IP
sudo ufw allow from 192.168.1.100
sudo ufw allow from 192.168.1.0/24
# 特定IP访问特定端口
sudo ufw allow from 192.168.1.100 to any port 22
sudo ufw allow from 192.168.1.100 to any port 3306 proto tcp
# 删除规则
sudo ufw delete allow ssh
sudo ufw delete allow 22/tcp
sudo ufw delete numbered # 按编号删除
高级配置
# 限制连接(防止暴力破解)
sudo ufw limit ssh
sudo ufw limit 22/tcp
# 日志
sudo ufw logging on
sudo ufw logging off
sudo ufw logging low
sudo ufw logging medium
sudo ufw logging high
# 查看规则
sudo ufw status numbered
sudo ufw show listening # 监听端口
sudo ufw show builtins # 默认规则
# 编辑规则文件
sudo vim /etc/ufw/applications.d/openssh-server
UFW配置文件
# /etc/default/ufw
ENABLED=yes
DEFAULT_INPUT_CHAIN="DROP"
DEFAULT_OUTPUT_CHAIN="ACCEPT"
DEFAULT_FORWARD_CHAIN="DROP"
DEFAULT_APPLICATION_POLICY="SKIP"
# /etc/ufw/ufw.conf
ENABLED=yes
LOGLEVEL=low
# /etc/ufw/user.rules - 用户规则
# /etc/ufw/user6.rules - IPv6规则
对比总结
| 功能 | iptables | firewalld | ufw |
|---|---|---|---|
| 适用发行版 | 通用 | RHEL/CentOS/Fedora | Ubuntu/Debian |
| 配置方式 | 命令/脚本 | 命令/区域 | 命令 |
| 持久化 | iptables-save/restore | 默认持久 | 默认持久 |
| 语法复杂度 | 高 | 中 | 低 |
| 学习曲线 | 陡峭 | 平缓 | 平缓 |
课后练习
实践任务
- 启用防火墙,只开放SSH、HTTP、HTTPS
- 配置UFW/firewalld,只允许特定IP访问SSH
- 使用iptables配置NAT网关
- 配置端口转发
- 编写自动化防火墙脚本
- 测试防火墙规则有效性
下一篇预告:我们将学习Shell脚本基础,掌握自动化运维技能。