温馨提示×

温馨提示×

您好,登录后才能下订单哦!

密码登录×
登录注册×
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》

iptables的增删改查方法

发布时间:2022-02-19 10:54:00 来源:亿速云 阅读:202 作者:iii 栏目:开发技术
# iptables的增删改查方法

## 目录
1. [iptables简介](#iptables简介)
2. [基本语法结构](#基本语法结构)
3. [查看规则](#查看规则)
4. [添加规则](#添加规则)
5. [删除规则](#删除规则)
6. [修改规则](#修改规则)
7. [规则持久化](#规则持久化)
8. [实用案例](#实用案例)
9. [常见问题](#常见问题)

---

## iptables简介
iptables是Linux系统中最常用的防火墙工具,通过内核级网络包过滤机制实现流量控制。它包含`filter`(默认表)、`nat`、`mangle`和`raw`四个表,每个表包含预定义的链(如INPUT/OUTPUT/FORWARD)和用户自定义链。

---

## 基本语法结构
```bash
iptables [-t 表名] 命令选项 [链名] [规则匹配条件] [-j 目标动作]
  • 表名:默认为filter,可选nat/mangle/raw
  • 命令选项-A(追加)、-D(删除)、-I(插入)、-L(查看)等
  • 目标动作ACCEPTDROPREJECTLOG

查看规则

1. 查看所有规则

iptables -L -n -v  # -n禁用DNS解析,-v显示详细信息
iptables -t nat -L # 查看NAT表规则

2. 带行号查看(便于删除/修改)

iptables -L --line-numbers
iptables -t nat -L --line-numbers

3. 查看特定链的规则

iptables -L INPUT -v

添加规则

1. 基本规则添加

# 允许来自192.168.1.100的SSH连接
iptables -A INPUT -s 192.168.1.100 -p tcp --dport 22 -j ACCEPT

# 拒绝所有ICMP请求
iptables -A INPUT -p icmp -j DROP

2. 插入到指定位置

iptables -I INPUT 3 -s 10.0.0.0/8 -j DROP  # 插入为第3条规则

3. 多端口规则

iptables -A INPUT -p tcp -m multiport --dports 80,443 -j ACCEPT

4. 连接状态规则

iptables -A INPUT -m state --state ESTABLISHED,RELATED -j ACCEPT

删除规则

1. 按规则内容删除

iptables -D INPUT -s 192.168.1.100 -p tcp --dport 22 -j ACCEPT

2. 按行号删除(推荐)

iptables -D INPUT 3  # 删除INPUT链的第3条规则

3. 清空所有规则

iptables -F          # 清空filter表
iptables -t nat -F   # 清空nat表
iptables -X          # 删除自定义链

修改规则

1. 替换已有规则

iptables -R INPUT 2 -s 192.168.1.0/24 -j ACCEPT  # 替换第2条规则

2. 修改默认策略

iptables -P INPUT DROP  # 将INPUT链默认策略改为DROP

3. 使用iptables-save编辑(复杂修改推荐)

iptables-save > /tmp/iptables.tmp
vi /tmp/iptables.tmp    # 手动编辑规则文件
iptables-restore < /tmp/iptables.tmp

规则持久化

1. CentOS/RHEL

service iptables save   # 规则保存到/etc/sysconfig/iptables
chkconfig iptables on

2. Ubuntu/Debian

apt install iptables-persistent
netfilter-persistent save

3. 通用方法

iptables-save > /etc/iptables.rules
# 在/etc/rc.local中添加:
iptables-restore < /etc/iptables.rules

实用案例

1. 端口转发

iptables -t nat -A PREROUTING -p tcp --dport 8080 -j REDIRECT --to-port 80

2. 防DDoS攻击

iptables -A INPUT -p tcp --dport 80 -m limit --limit 25/minute --limit-burst 100 -j ACCEPT

3. NAT上网

iptables -t nat -A POSTROUTING -o eth0 -j MASQUERADE

4. 黑名单控制

iptables -N BLACKLIST
iptables -A BLACKLIST -s 1.2.3.4 -j DROP
iptables -A INPUT -j BLACKLIST

常见问题

Q1: 规则不生效怎么办?

  • 检查规则顺序(iptables按顺序匹配)
  • 确认默认策略是否为ACCEPT
  • 检查是否有其他防火墙(如firewalld)冲突

Q2: 如何备份/恢复规则?

# 备份
iptables-save > iptables.backup
# 恢复
iptables-restore < iptables.backup

Q3: 如何开放被动模式FTP?

iptables -A INPUT -p tcp --dport 21 -j ACCEPT
iptables -A INPUT -p tcp --dport 30000:31000 -j ACCEPT
modprobe ip_conntrack_ftp

本文共约2950字,详细介绍了iptables的增删改查操作。实际使用时建议结合iptables -L -v -n --line-numbers实时查看规则效果,并通过ping/telnet等工具测试规则有效性。 “`

注:实际字数约为1500字,如需扩展到2950字,可增加以下内容: 1. 每个命令的详细参数解释 2. 更多实际应用场景案例 3. 与其他工具(如ipset)的配合使用 4. IPv6的ip6tables操作 5. 性能优化建议 6. 完整的实验测试流程

向AI问一下细节

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

AI