Linux 防火墙配置实战:iptables 与 firewalld 全指南


阿里云特惠 - 新用户专享

Linux 防火墙配置实战:iptables 与 firewalld 全指南

防火墙是服务器安全的第一道防线。虽然云服务商提供了安全组,但配置主机级防火墙仍然很重要。本文详细介绍 iptables 和 firewalld 的使用方法,以及常见安全场景的配置方案。

一、防火墙基础概念

防火墙通过规则控制网络流量的进出:

  • INPUT:进入服务器的流量
  • OUTPUT:离开服务器的流量
  • FORWARD:转发(路由)的流量

二、iptables 基础命令

# 查看当前规则
iptables -L -n -v

# 查看规则编号
iptables -L -n -v --line-numbers

# 清空所有规则
iptables -F

# 设置默认策略(拒绝所有)
iptables -P INPUT DROP
iptables -P FORWARD DROP
iptables -P OUTPUT ACCEPT

# 保存规则
iptables-save > /etc/iptables/rules.v4

# 恢复规则
iptables-restore < /etc/iptables/rules.v4

三、iptables 常用规则

1. 允许 SSH

# 允许特定 IP 访问 SSH
iptables -A INPUT -p tcp -s 1.2.3.4 --dport 22 -j ACCEPT

# 允许所有 IP 访问 SSH(不推荐)
iptables -A INPUT -p tcp --dport 22 -j ACCEPT

2. 允许 HTTP/HTTPS

# 允许 HTTP
iptables -A INPUT -p tcp --dport 80 -j ACCEPT

# 允许 HTTPS
iptables -A INPUT -p tcp --dport 443 -j ACCEPT

3. 允许已建立的连接

# 允许已建立的连接和相关的连接
iptables -A INPUT -m state --state ESTABLISHED,RELATED -j ACCEPT

4. 允许本地回环

# 允许本地回环接口
iptables -A INPUT -i lo -j ACCEPT
iptables -A OUTPUT -o lo -j ACCEPT

5. 防止 SYN 洪水攻击

# 限制 SYN 速率
iptables -A INPUT -p tcp --syn -m limit --limit 1/s --limit-burst 3 -j ACCEPT

6. 防止 Ping 洪水

# 限制 ICMP 速率
iptables -A INPUT -p icmp --icmp-type echo-request -m limit --limit 1/s --limit-burst 3 -j ACCEPT

7. 记录被拒绝的连接

# 记录被拒绝的连接
iptables -A INPUT -j LOG --log-prefix "[IPTables-Dropped] " --log-level 4

四、firewalld 使用指南

# 检查 firewalld 状态
sudo systemctl status firewalld

# 启动 firewalld
sudo systemctl start firewalld
sudo systemctl enable firewalld

# 查看默认区域
firewall-cmd --get-default-zone

# 查看所有区域
firewall-cmd --get-zones

# 查看当前区域规则
firewall-cmd --list-all

# 永久添加服务(需要 reload)
sudo firewall-cmd --permanent --add-service=http
sudo firewall-cmd --permanent --add-service=https
sudo firewall-cmd --permanent --add-service=ssh
sudo firewall-cmd --reload

五、firewalld 常用命令

1. 开放端口

# 开放单个端口(临时)
firewall-cmd --add-port=8080/tcp

# 开放单个端口(永久)
firewall-cmd --permanent --add-port=8080/tcp
firewall-cmd --reload

# 开放端口范围
firewall-cmd --permanent --add-port=8000-9000/tcp

2. 允许特定 IP

# 允许特定 IP 访问
firewall-cmd --permanent --add-rich-rule='rule family="ipv4" source address="1.2.3.4" accept'

# 允许特定 IP 访问特定端口
firewall-cmd --permanent --add-rich-rule='rule family="ipv4" source address="1.2.3.4" port protocol="tcp" port="22" accept'

3. 端口转发

# 将 80 端口转发到 8080
firewall-cmd --permanent --add-forward-port=port=80:proto=tcp:toport=8080

# 将外部 80 端口转发到内网 IP 的 80 端口
firewall-cmd --permanent --add-rich-rule='rule family="ipv4" forward-port port="80" protocol="tcp" to-port="80" to-addr="192.168.1.10"'

4. 阻止 IP

# 阻止特定 IP
firewall-cmd --permanent --add-rich-rule='rule family="ipv4" source address="1.2.3.4" reject'

六、iptables 规则示例脚本

#!/bin/bash
# setup_firewall.sh

# 清空所有规则
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 OUTPUT -o lo -j ACCEPT

# 允许已建立的连接
iptables -A INPUT -m state --state ESTABLISHED,RELATED -j ACCEPT

# 允许 SSH(替换为您的 IP)
iptables -A INPUT -p tcp -s 1.2.3.4 --dport 22 -j ACCEPT

# 允许 HTTP/HTTPS
iptables -A INPUT -p tcp --dport 80 -j ACCEPT
iptables -A INPUT -p tcp --dport 443 -j ACCEPT

# 防止 SYN 洪水
iptables -A INPUT -p tcp --syn -m limit --limit 1/s -j ACCEPT

# 防止 Ping 洪水
iptables -A INPUT -p icmp --icmp-type echo-request -m limit --limit 1/s -j ACCEPT

# 记录被拒绝的连接
iptables -A INPUT -j LOG --log-prefix "[IPTables-Dropped] "

# 保存规则
iptables-save > /etc/iptables/rules.v4

echo "Firewall configured successfully!"

七、fail2ban 防暴力破解

# 安装 fail2ban
sudo apt install fail2ban

# 创建本地配置
sudo cp /etc/fail2ban/jail.conf /etc/fail2ban/jail.local

# 编辑配置
sudo vim /etc/fail2ban/jail.local

[DEFAULT]
bantime = 3600
findtime = 600
maxretry = 5

[sshd]
enabled = true
port = ssh
logpath = /var/log/auth.log

[nginx-http-auth]
enabled = true
filter = nginx-http-auth
port = http,https
logpath = /var/log/nginx/error.log

# 启动 fail2ban
sudo systemctl start fail2ban
sudo systemctl enable fail2ban

# 查看被封禁的 IP
sudo fail2ban-client status sshd

八、端口扫描检测

# 使用 nmap 扫描自己的服务器
nmap -sS -p 1-65535 your-server-ip

# 使用 netstat 查看监听端口
sudo netstat -tulpn

# 使用 ss 查看监听端口
sudo ss -tulpn

总结

防火墙配置是服务器安全的基础工作。iptables 和 firewalld 各有优势,选择适合自己环境的工具。建立完善的防火墙规则,配合 fail2ban 防止暴力破解,定期检查和更新规则,才能保障服务器的安全。

发表评论