您好,登录后才能下订单哦!
密码登录
登录注册
点击 登录注册 即表示同意《亿速云用户服务条款》
# Linux中iptables常用方法有哪些
## 一、iptables基础概念
### 1.1 什么是iptables
iptables是Linux系统内置的防火墙工具,用于配置、维护和检查IPv4数据包过滤规则表。它通过内核中的netfilter框架实现对网络数据包的控制,能够执行包过滤、网络地址转换(NAT)和数据包修改等操作。
### 1.2 iptables与netfilter的关系
- **netfilter**:Linux内核中的框架,实际执行过滤功能
- **iptables**:用户空间工具,用于配置netfilter规则
### 1.3 四表五链结构
#### 四个表(table):
1. **filter表**:默认表,用于包过滤
2. **nat表**:网络地址转换
3. **mangle表**:修改数据包内容
4. **raw表**:决定数据包是否被状态跟踪
#### 五个链(chain):
1. **INPUT**:处理进入本机的数据包
2. **OUTPUT**:处理本机发出的数据包
3. **FORWARD**:处理转发数据包
4. **PREROUTING**:路由前处理
5. **POSTROUTING**:路由后处理
## 二、iptables基本语法
### 2.1 命令格式
```bash
iptables [-t 表名] 命令选项 [链名] [规则匹配条件] [-j 目标动作]
选项 | 说明 |
---|---|
-A | 追加规则到链尾 |
-I | 插入规则到指定位置 |
-D | 删除指定规则 |
-R | 替换指定规则 |
-L | 列出规则 |
-F | 清空链中规则 |
-N | 新建用户自定义链 |
-X | 删除用户自定义链 |
-P | 设置链的默认策略 |
-Z | 计数器清零 |
# 允许来自192.168.1.0/24的SSH连接
iptables -A INPUT -s 192.168.1.0/24 -p tcp --dport 22 -j ACCEPT
# 拒绝所有ICMP请求
iptables -A INPUT -p icmp -j DROP
# 允许本地回环接口
iptables -A INPUT -i lo -j ACCEPT
# 允许已建立的连接和相关的连接
iptables -A INPUT -m state --state ESTABLISHED,RELATED -j ACCEPT
# 允许新的HTTP连接
iptables -A INPUT -p tcp --dport 80 -m state --state NEW -j ACCEPT
# 将80端口转发到8080端口
iptables -t nat -A PREROUTING -p tcp --dport 80 -j REDIRECT --to-port 8080
# 源地址NAT(SNAT)
iptables -t nat -A POSTROUTING -s 192.168.1.0/24 -o eth0 -j MASQUERADE
# 目标地址NAT(DNAT)
iptables -t nat -A PREROUTING -p tcp -d 203.0.113.1 --dport 80 -j DNAT --to-destination 192.168.1.100:80
# 设置默认策略
iptables -P INPUT DROP
iptables -P FORWARD DROP
iptables -P OUTPUT ACCEPT
# 允许已建立的连接
iptables -A INPUT -m state --state ESTABLISHED,RELATED -j ACCEPT
# 允许SSH访问
iptables -A INPUT -p tcp --dport 22 -j ACCEPT
# 允许HTTP/HTTPS访问
iptables -A INPUT -p tcp --dport 80 -j ACCEPT
iptables -A INPUT -p tcp --dport 443 -j ACCEPT
# 允许PING
iptables -A INPUT -p icmp --icmp-type echo-request -j ACCEPT
# 限制单个IP的连接数
iptables -A INPUT -p tcp --dport 80 -m connlimit --connlimit-above 50 -j REJECT
# 限制SYN洪水攻击
iptables -A INPUT -p tcp --syn -m limit --limit 1/s --limit-burst 3 -j ACCEPT
iptables -A INPUT -p tcp --syn -j DROP
# 限制ICMP洪水
iptables -A INPUT -p icmp -m limit --limit 1/s --limit-burst 10 -j ACCEPT
# 启用IP转发
echo 1 > /proc/sys/net/ipv4/ip_forward
# 设置NAT
iptables -t nat -A POSTROUTING -o eth0 -j MASQUERADE
# 转发SSH到内部服务器
iptables -t nat -A PREROUTING -p tcp --dport 2222 -j DNAT --to-destination 192.168.1.100:22
iptables -A FORWARD -p tcp -d 192.168.1.100 --dport 22 -j ACCEPT
# 保存规则(Ubuntu/Debian)
iptables-save > /etc/iptables.rules
# 恢复规则
iptables-restore < /etc/iptables.rules
# CentOS/RHEL保存规则
service iptables save
# 查看所有规则(带行号)
iptables -L -n -v --line-numbers
# 查看NAT表规则
iptables -t nat -L -n -v
# 查看规则匹配统计
iptables -L -n -v
# 重置计数器
iptables -Z
# 创建日志链
iptables -N LOGGING
iptables -A INPUT -j LOGGING
# 记录被拒绝的包
iptables -A LOGGING -m limit --limit 2/min -j LOG --log-prefix "IPTables-Dropped: " --log-level 4
iptables -A LOGGING -j DROP
# 检查是否启用了连接跟踪
lsmod | grep conntrack
# 检查是否有其他防火墙工具(如firewalld)冲突
systemctl status firewalld
# 确认IP转发已启用
cat /proc/sys/net/ipv4/ip_forward
# 临时启用IP转发
echo 1 > /proc/sys/net/ipv4/ip_forward
# 永久启用(在/etc/sysctl.conf中)
net.ipv4.ip_forward = 1
sysctl -p
# 创建自定义链
iptables -N CUSTOM_CHN
# 将流量跳转到自定义链
iptables -A INPUT -j CUSTOM_CHN
# 在自定义链中添加规则
iptables -A CUSTOM_CHN -s 192.168.1.100 -j DROP
# 只在工作时间(9-18点)允许SSH
iptables -A INPUT -p tcp --dport 22 -m time --timestart 09:00 --timestop 18:00 -j ACCEPT
# 允许特定MAC地址访问
iptables -A INPUT -m mac --mac-source 00:1A:2B:3C:4D:5E -j ACCEPT
nftables是iptables的替代品,提供更简洁的语法和更好的性能:
# 基本nftables规则示例
nft add table ip filter
nft add chain ip filter input { type filter hook input priority 0 \; }
nft add rule ip filter input tcp dport 22 accept
# 将iptables规则转换为nftables
iptables-save > iptables.rules
iptables-restore-translate -f iptables.rules > nftables.rules
nft -f nftables.rules
firewalld是Red Hat开发的动态防火墙管理器,提供更高级的抽象:
# 基本firewalld命令
firewall-cmd --zone=public --add-port=80/tcp --permanent
firewall-cmd --reload
iptables作为Linux系统中最强大的防火墙工具之一,虽然学习曲线较陡峭,但掌握其核心概念和常用方法后,能够实现灵活高效的网络流量控制。随着nftables等新技术的出现,建议新系统可以考虑使用更现代的替代方案,但在现有环境中,iptables仍然是不可或缺的重要工具。 “`
免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。