Linux防火墙

Linux防火墙是保护系统安全的第一道防线,用于控制网络流量的进出。

防火墙工具对比

工具特点适用场景
iptables底层工具,功能强大传统系统、精细控制
nftablesiptables继任者,语法更清晰新系统推荐
UFWUbuntu简化工具Ubuntu桌面/简单场景
firewalld动态管理,支持zoneRHEL/CentOS系

UFW (Ubuntu)

基本操作

# 安装(通常已预装)
sudo apt install ufw
 
# 启用/禁用
sudo ufw enable
sudo ufw disable
 
# 查看状态
sudo ufw status
sudo ufw status numbered         # 带编号的状态
 
# 重置所有规则
sudo ufw reset

规则管理

# 允许服务/端口
sudo ufw allow 22                # 允许SSH
sudo ufw allow 80/tcp            # 允许HTTP
sudo ufw allow 443               # 允许HTTPS
sudo ufw allow 'Nginx Full'      # 使用预定义服务名
 
# 指定来源IP
sudo ufw allow from 192.168.1.0/24       # 允许局域网
sudo ufw allow from 192.168.1.100 to any port 22  # 特定IP访问SSH
 
# 拒绝规则
sudo ufw deny 23                 # 拒绝Telnet
sudo ufw deny from 10.0.0.1      # 拒绝特定IP
 
# 删除规则
sudo ufw delete allow 80         # 按规则内容删除
sudo ufw delete 3                # 按编号删除

默认策略

# 设置默认策略
sudo ufw default deny incoming   # 拒绝所有入站
sudo ufw default allow outgoing  # 允许所有出站

日志

sudo ufw logging on              # 启用日志
sudo ufw logging medium          # 日志级别:low/medium/high
sudo tail -f /var/log/ufw.log    # 查看日志

firewalld (RHEL/CentOS)

基本操作

# 安装
sudo apt install firewalld       # Debian/Ubuntu
sudo dnf install firewalld       # RHEL/CentOS
 
# 服务管理
sudo systemctl start firewalld
sudo systemctl enable firewalld
sudo systemctl status firewalld
 
# 查看状态
firewall-cmd --state
firewall-cmd --reload            # 重载配置

Zone概念

firewalld使用zone来管理不同信任级别的网络连接:

Zone说明
drop丢弃所有入站,仅允许出站
block拒绝入站,返回ICMP消息
public公共网络,选择性开放
trusted信任所有连接
home家庭网络
work工作网络
dmz非军事区,有限开放
# 查看默认zone
firewall-cmd --get-default-zone
 
# 查看所有zone
firewall-cmd --get-zones
 
# 查看当前活跃zone
firewall-cmd --get-active-zones
 
# 设置默认zone
firewall-cmd --set-default-zone=public

规则管理

# 查看当前规则
firewall-cmd --list-all
 
# 开放端口
firewall-cmd --add-port=80/tcp              # 临时
firewall-cmd --add-port=80/tcp --permanent  # 永久
firewall-cmd --reload                       # 重载生效
 
# 开放服务
firewall-cmd --add-service=http
firewall-cmd --add-service=https --permanent
 
# 移除规则
firewall-cmd --remove-port=80/tcp
firewall-cmd --remove-service=http
 
# 允许特定IP
firewall-cmd --add-source=192.168.1.100
firewall-cmd --add-rich-rule='rule family="ipv4" source address="192.168.1.100" port protocol="tcp" port="22" accept'
 
# 端口转发
firewall-cmd --add-forward-port=port=80:proto=tcp:toport=8080

iptables (底层工具)

查看规则

# 查看所有规则
sudo iptables -L
sudo iptables -L -n              # 数字显示地址
sudo iptables -L -v              # 详细信息
sudo iptables -L --line-numbers  # 显示行号
 
# 查看特定链
sudo iptables -L INPUT
sudo iptables -L FORWARD
sudo iptables -L OUTPUT

规则操作

# 允许规则
sudo iptables -A INPUT -p tcp --dport 22 -j ACCEPT    # 追加规则
sudo iptables -I INPUT 1 -p tcp --dport 80 -j ACCEPT  # 插入到第1行
 
# 拒绝规则
sudo iptables -A INPUT -p tcp --dport 23 -j DROP      # 静默丢弃
sudo iptables -A INPUT -p tcp --dport 23 -j REJECT    # 返回拒绝
 
# 按来源/目标
sudo iptables -A INPUT -s 192.168.1.100 -j ACCEPT
sudo iptables -A INPUT -s 10.0.0.0/8 -j DROP
 
# 删除规则
sudo iptables -D INPUT 3                               # 按行号删除
sudo iptables -D INPUT -p tcp --dport 80 -j ACCEPT    # 按规则删除
 
# 清空所有规则
sudo iptables -F

默认策略

sudo iptables -P INPUT DROP
sudo iptables -P FORWARD DROP
sudo iptables -P OUTPUT ACCEPT

保存规则

# Debian/Ubuntu
sudo apt install iptables-persistent
sudo netfilter-persistent save
 
# RHEL/CentOS
sudo service iptables save
 
# 手动保存
sudo iptables-save > /etc/iptables/rules.v4
sudo iptables-restore < /etc/iptables/rules.v4

常用规则示例

# 允许本地回环
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 8 -j ACCEPT
 
# 防止SSH暴力破解(限制连接频率)
sudo iptables -A INPUT -p tcp --dport 22 -m state --state NEW -m recent --set
sudo iptables -A INPUT -p tcp --dport 22 -m state --state NEW -m recent --update --seconds 60 --hitcount 4 -j DROP

nftables (新一代工具)

# 查看规则
sudo nft list ruleset
 
# 创建表
sudo nft add table inet filter
 
# 创建链
sudo nft add chain inet filter input { type filter hook input priority 0 \; }
 
# 添加规则
sudo nft add rule inet filter input tcp dport 22 accept
sudo nft add rule inet filter input tcp dport { 80, 443 } accept
 
# 删除规则(需要handle)
sudo nft list ruleset -a        # 显示handle号
sudo nft delete rule inet filter input handle 3

常用场景配置

Web服务器

# UFW方式
sudo ufw default deny incoming
sudo ufw default allow outgoing
sudo ufw allow 22/tcp
sudo ufw allow 80/tcp
sudo ufw allow 443/tcp
sudo ufw enable

仅允许特定IP管理

# UFW
sudo ufw allow from 192.168.1.100 to any port 22
 
# firewalld
firewall-cmd --add-rich-rule='rule family="ipv4" source address="192.168.1.100" service name="ssh" accept' --permanent

端口转发

# iptables
sudo iptables -t nat -A PREROUTING -p tcp --dport 80 -j REDIRECT --to-port 8080
 
# firewalld
firewall-cmd --add-forward-port=port=80:proto=tcp:toport=8080 --permanent

排查技巧

# 查看监听端口
sudo ss -tlnp
 
# 测试端口连通性
nc -zv host 80
telnet host 80
 
# 查看连接状态
sudo ss -tunap
 
# 查看防火墙日志
sudo tail -f /var/log/ufw.log          # UFW
sudo tail -f /var/log/firewalld        # firewalld
sudo tail -f /var/log/kern.log         # iptables