Linux防火墙常用命令(iptables/firewalld/ufw)
-
- 一、iptables(传统防火墙)
-
- [1. 查看规则](#1. 查看规则)
- [2. 清空与重置规则](#2. 清空与重置规则)
- [3. 添加规则](#3. 添加规则)
- [4. 插入与删除规则](#4. 插入与删除规则)
- [5. 保存与恢复规则](#5. 保存与恢复规则)
- [二、firewalld(Red Hat系防火墙)](#二、firewalld(Red Hat系防火墙))
-
- [1. 基础管理](#1. 基础管理)
- [2. 区域管理](#2. 区域管理)
- [3. 端口与服务管理](#3. 端口与服务管理)
- [4. 富规则(Rich Rules)](#4. 富规则(Rich Rules))
- [5. 重载与应用配置](#5. 重载与应用配置)
- 三、ufw(Ubuntu防火墙)
-
- [1. 基本操作](#1. 基本操作)
- [2. 规则管理](#2. 规则管理)
- [3. 高级配置](#3. 高级配置)
- [4. 日志管理](#4. 日志管理)
- 四、常用防火墙场景
-
- [1. 基础服务器配置](#1. 基础服务器配置)
- [2. 端口转发示例](#2. 端口转发示例)
- [3. DDoS防护基础](#3. DDoS防护基础)
- 五、常用命令速查
-
- [1. 快速查看连接](#1. 快速查看连接)
- [2. 防火墙选择建议](#2. 防火墙选择建议)
- [3. 配置文件位置](#3. 配置文件位置)
- [4. 调试技巧](#4. 调试技巧)
一、iptables(传统防火墙)
1. 查看规则
bash
# 查看所有规则
iptables -L -n -v # 详细查看所有规则
iptables -L INPUT -n --line-numbers # 查看INPUT链并显示行号
# 查看特定链
iptables -L INPUT # 查看INPUT链
iptables -L OUTPUT # 查看OUTPUT链
iptables -L FORWARD # 查看FORWARD链
# 查看NAT表规则
iptables -t nat -L -n # 查看NAT表
iptables -t mangle -L -n # 查看mangle表
2. 清空与重置规则
bash
# 清空所有规则
iptables -F # 清空filter表
iptables -t nat -F # 清空nat表
iptables -t mangle -F # 清空mangle表
# 删除链
iptables -X # 删除自定义链
# 重置计数器
iptables -Z # 重置计数器
# 设置默认策略
iptables -P INPUT ACCEPT/DROP # 设置INPUT链默认策略
iptables -P OUTPUT ACCEPT/DROP # 设置OUTPUT链默认策略
iptables -P FORWARD ACCEPT/DROP # 设置FORWARD链默认策略
3. 添加规则
bash
# 基本语法
iptables -A chain rule-specification -j target
# 允许特定端口
iptables -A INPUT -p tcp --dport 22 -j ACCEPT # 允许SSH
iptables -A INPUT -p tcp --dport 80 -j ACCEPT # 允许HTTP
iptables -A INPUT -p tcp --dport 443 -j ACCEPT # 允许HTTPS
# 允许特定IP
iptables -A INPUT -s 192.168.1.100 -j ACCEPT # 允许特定IP
iptables -A INPUT -s 192.168.1.0/24 -j ACCEPT # 允许网段
# 限制连接
iptables -A INPUT -p tcp --syn --dport 80 -m connlimit --connlimit-above 20 -j DROP
4. 插入与删除规则
bash
# 插入规则(指定位置)
iptables -I INPUT 2 -p tcp --dport 3306 -j ACCEPT # 在INPUT链第2位插入
# 删除规则
iptables -D INPUT 3 # 删除INPUT链第3条规则
iptables -D INPUT -p tcp --dport 80 -j ACCEPT # 删除匹配的规则
5. 保存与恢复规则
bash
# CentOS/RHEL
service iptables save # 保存规则
service iptables restart # 重启服务
# Debian/Ubuntu
iptables-save > /etc/iptables.rules # 保存规则
iptables-restore < /etc/iptables.rules # 恢复规则
# 通用方法
iptables-save > /path/to/file # 保存到文件
iptables-restore < /path/to/file # 从文件恢复
二、firewalld(Red Hat系防火墙)
1. 基础管理
bash
# 服务管理
systemctl status firewalld # 查看状态
systemctl start firewalld # 启动
systemctl stop firewalld # 停止
systemctl enable firewalld # 开机自启
systemctl disable firewalld # 禁用开机自启
# 查看信息
firewall-cmd --state # 查看运行状态
firewall-cmd --list-all # 列出所有配置
firewall-cmd --get-active-zones # 查看活动区域
firewall-cmd --get-zones # 查看所有区域
2. 区域管理
bash
# 查看区域配置
firewall-cmd --zone=public --list-all # 查看public区域
firewall-cmd --zone=internal --list-all # 查看internal区域
# 设置默认区域
firewall-cmd --set-default-zone=public # 设置默认区域
# 为接口指定区域
firewall-cmd --zone=public --change-interface=eth0 # 为eth0指定区域
3. 端口与服务管理
bash
# 临时开放端口(重启后失效)
firewall-cmd --zone=public --add-port=80/tcp
# 永久开放端口
firewall-cmd --zone=public --add-port=80/tcp --permanent
# 开放服务
firewall-cmd --zone=public --add-service=http # 开放HTTP服务
firewall-cmd --zone=public --add-service=https # 开放HTTPS服务
firewall-cmd --zone=public --add-service=ssh # 开放SSH服务
# 移除端口/服务
firewall-cmd --zone=public --remove-port=8080/tcp
firewall-cmd --zone=public --remove-service=samba
# 查看已开放的端口和服务
firewall-cmd --zone=public --list-ports
firewall-cmd --zone=public --list-services
4. 富规则(Rich Rules)
bash
# 允许特定IP访问
firewall-cmd --zone=public --add-rich-rule='rule family="ipv4" source address="192.168.1.0/24" accept'
# 允许特定IP访问特定端口
firewall-cmd --zone=public --add-rich-rule='rule family="ipv4" source address="192.168.1.100" port protocol="tcp" port="22" accept'
# 拒绝特定IP
firewall-cmd --zone=public --add-rich-rule='rule family="ipv4" source address="10.0.0.0/8" reject'
# 端口转发
firewall-cmd --zone=public --add-forward-port=port=80:proto=tcp:toport=8080
5. 重载与应用配置
bash
firewall-cmd --reload # 重载配置(不中断现有连接)
firewall-cmd --complete-reload # 完全重载(中断所有连接)
firewall-cmd --runtime-to-permanent # 将运行时配置转为永久配置
三、ufw(Ubuntu防火墙)
1. 基本操作
bash
# 启用/禁用
sudo ufw enable # 启用防火墙
sudo ufw disable # 禁用防火墙
sudo ufw reset # 重置防火墙
# 查看状态
sudo ufw status # 查看状态
sudo ufw status verbose # 详细状态
sudo ufw status numbered # 带编号的状态
2. 规则管理
bash
# 允许/拒绝连接
sudo ufw allow 22 # 允许SSH(默认TCP)
sudo ufw allow 22/tcp # 允许TCP 22
sudo ufw allow 22/udp # 允许UDP 22
sudo ufw deny 23 # 拒绝端口23
# 允许特定服务
sudo ufw allow http # 允许HTTP
sudo ufw allow https # 允许HTTPS
sudo ufw allow ssh # 允许SSH
# 允许特定IP
sudo ufw allow from 192.168.1.100 # 允许特定IP
sudo ufw allow from 192.168.1.0/24 # 允许网段
sudo ufw allow from 192.168.1.100 to any port 22 # 允许IP访问特定端口
# 删除规则
sudo ufw delete allow 22 # 删除允许22端口的规则
sudo ufw delete 3 # 删除编号为3的规则
3. 高级配置
bash
# 设置默认策略
sudo ufw default allow outgoing # 默认允许出站
sudo ufw default deny incoming # 默认拒绝入站
# 限制连接速率
sudo ufw limit 22/tcp # 限制SSH连接速率
# 应用特定配置
sudo ufw app list # 列出应用配置
sudo ufw app info 'Apache Full' # 查看Apache Full配置
sudo ufw allow 'Apache Full' # 允许Apache Full
4. 日志管理
bash
sudo ufw logging on # 开启日志
sudo ufw logging off # 关闭日志
sudo ufw logging low|medium|high # 设置日志级别
四、常用防火墙场景
1. 基础服务器配置
bash
# iptables示例
iptables -P INPUT DROP # 默认拒绝所有入站
iptables -A INPUT -i lo -j ACCEPT # 允许本地回环
iptables -A INPUT -m state --state ESTABLISHED,RELATED -j ACCEPT
iptables -A INPUT -p tcp --dport 22 -j ACCEPT # SSH
iptables -A INPUT -p tcp --dport 80 -j ACCEPT # HTTP
iptables -A INPUT -p tcp --dport 443 -j ACCEPT # HTTPS
2. 端口转发示例
bash
# iptables端口转发
iptables -t nat -A PREROUTING -p tcp --dport 80 -j REDIRECT --to-port 8080
iptables -t nat -A PREROUTING -p tcp --dport 443 -j DNAT --to-destination 192.168.1.10:443
# firewalld端口转发
firewall-cmd --add-forward-port=port=80:proto=tcp:toport=8080
3. DDoS防护基础
bash
# 限制连接数
iptables -A INPUT -p tcp --dport 80 -m connlimit --connlimit-above 50 -j DROP
# 限制新建连接速率
iptables -A INPUT -p tcp --syn --dport 80 -m limit --limit 20/minute --limit-burst 30 -j ACCEPT
五、常用命令速查
1. 快速查看连接
bash
# 查看当前连接
ss -tunap # 查看所有TCP/UDP连接
netstat -tunap # 传统方式查看连接
lsof -i:80 # 查看80端口占用情况
2. 防火墙选择建议
- CentOS/RHEL 7+:优先使用
firewalld - Ubuntu/Debian:优先使用
ufw - 老版本系统/需要精细控制:使用
iptables
3. 配置文件位置
bash
# iptables配置文件
/etc/sysconfig/iptables # CentOS/RHEL
/etc/iptables/rules.v4 # Debian/Ubuntu
# firewalld配置文件
/etc/firewalld/ # 主配置目录
/etc/firewalld/zones/ # 区域配置
/etc/firewalld/services/ # 服务定义
# ufw配置文件
/etc/ufw/ # 主配置目录
/etc/ufw/user.rules # 用户规则
/etc/ufw/before.rules # 前规则
4. 调试技巧
bash
# 测试端口连通性
nc -zv hostname port # 测试TCP端口
nc -zvu hostname port # 测试UDP端口
telnet hostname port # 传统方式测试
# 查看防火墙日志
journalctl -u firewalld # firewalld日志
dmesg | grep iptables # iptables日志
tail -f /var/log/ufw.log # ufw日志
记住关键原则:先放行必要的服务,再设置默认拒绝策略,避免把自己锁在外面!