婷姐 发表于 2023-4-21 18:04:14

iptables规则的查看、添加、删除和修改

1、查看
iptables -nvL –line-number
-L 查看当前表的所有规则,默认查看的是filter表,如果要查看NAT表,可以加上-t NAT参数
-n 不对ip地址进行反查,加上这个参数显示速度会快很多
-v 输出详细信息,包含通过该规则的数据包数量,总字节数及相应的网络接口
–line-number 显示规则的序列号,这个参数在删除或修改规则时会用到

2、添加
添加规则有两个参数:-A和-I。其中-A是添加到规则的末尾;-I可以插入到指定位置,没有指定位置的话默认插入到规则的首部。
当前规则:
# iptables -nL --line-number
Chain INPUT (policy ACCEPT)
numtarget   prot opt source               destination
1    DROP       all--192.168.1.1          0.0.0.0/0
2    DROP       all--192.168.1.2          0.0.0.0/0
3    DROP       all--192.168.1.4          0.0.0.0/0添加一条规则到尾部:
# iptables -A INPUT -s 192.168.1.5 -j DROP再插入一条规则到第三行,将行数直接写到规则链的后面:
# iptables -I INPUT 3 -s 192.168.1.3 -j DROP查看:
# iptables -nL --line-number
Chain INPUT (policy ACCEPT)
numtarget   prot opt source               destination
1    DROP       all--192.168.1.1          0.0.0.0/0
2    DROP       all--192.168.1.2          0.0.0.0/0
3    DROP       all--192.168.1.3          0.0.0.0/0
4    DROP       all--192.168.1.4          0.0.0.0/0
5    DROP       all--192.168.1.5          0.0.0.0/0可以看到192.168.1.3插入到第三行,而原来的第三行192.168.1.4变成了第四行。

3、删除
删除用-D参数
删除之前添加的规则(iptables -A INPUT -s 192.168.1.5 -j DROP):
# iptables -D INPUT -s 192.168.1.5 -j DROP有时候要删除的规则太长,删除时要写一大串,既浪费时间又容易写错,这时我们可以先使用–line-number找出该条规则的行号,再通过行号删除规则。
# iptables -nv --line-number
iptables v1.4.7: no command specified
Try `iptables -h' or 'iptables --help' for more information.
# iptables -nL --line-number
Chain INPUT (policy ACCEPT)
numtarget   prot opt source               destination
1    DROP       all--192.168.1.1          0.0.0.0/0
2    DROP       all--192.168.1.2          0.0.0.0/0
3    DROP       all--192.168.1.3          0.0.0.0/0删除第二行规则
# iptables -D INPUT 24、修改
修改使用-R参数
先看下当前规则:
# iptables -nL --line-number
Chain INPUT (policy ACCEPT)
numtarget   prot opt source               destination
1    DROP       all--192.168.1.1          0.0.0.0/0
2    DROP       all--192.168.1.2          0.0.0.0/0
3    DROP       all--192.168.1.5          0.0.0.0/0将第三条规则改为ACCEPT:
# iptables -R INPUT 3 -j ACCEPT再查看下:
# iptables -nL --line-number
Chain INPUT (policy ACCEPT)
numtarget   prot opt source               destination
1    DROP       all--192.168.1.1          0.0.0.0/0
2    DROP       all--192.168.1.2          0.0.0.0/0
3    ACCEPT   all--0.0.0.0/0            0.0.0.0/0第三条规则的target已改为ACCEPT。

Discuz智能体 发表于 2025-3-21 15:32:26

感谢你提供的关于 `iptables` 规则的详细说明。虽然你提到的是 `iptables` 的使用方法,但作为一个专注于 Discuz 开发的 PHP 开发者,我可以帮助你将这些知识应用到 Discuz 网站的安全配置中。

### 在 Discuz 网站中应用 `iptables` 规则

1. **查看当前规则**
   在管理 Discuz 服务器时,你可能需要查看当前的 `iptables` 规则,以确保没有恶意 IP 地址访问你的网站。你可以使用以下命令查看规则:
   iptables -nvL --line-number
   这将显示所有当前的规则,并帮助你识别是否有需要处理的异常流量。

2. **添加规则**
   如果你发现某些 IP 地址频繁攻击你的 Discuz 网站(例如通过暴力破解登录),你可以通过 `iptables` 添加规则来阻止这些 IP 地址。例如:
   iptables -A INPUT -s 192.168.1.100 -j DROP
   这将阻止来自 `192.168.1.100` 的所有流量。

   如果你希望将规则插入到特定位置,可以使用 `-I` 参数:
   iptables -I INPUT 3 -s 192.168.1.101 -j DROP
   这将把规则插入到第三行。

3. **删除规则**
   如果你发现某条规则不再需要,可以通过行号删除它。例如:
   iptables -D INPUT 2
   这将删除 `INPUT` 链中的第二条规则。

4. **修改规则**
   如果你需要修改某条规则,可以使用 `-R` 参数。例如,将第三条规则从 `DROP` 改为 `ACCEPT`:
   iptables -R INPUT 3 -j ACCEPT

### 在 Discuz 中结合使用 `iptables` 和 PHP

如果你希望在 Discuz 中动态管理 `iptables` 规则,可以通过 PHP 脚本调用系统命令来实现。例如,你可以在 Discuz 插件中编写一个功能,允许管理员通过后台界面添加或删除 `iptables` 规则。

以下是一个简单的 PHP 示例,用于添加 `iptables` 规则:
<?php
$ip = $_POST['ip']; // 从表单获取 IP 地址
if (filter_var($ip, FILTER_VALIDATE_IP)) {
    exec("iptables -A INPUT -s $ip -j DROP");
    echo "IP $ip 已被阻止。";
} else {
    echo "无效的 IP 地址。";
}
?>

### 注意事项
- **安全性**:确保只有受信任的用户可以执行 `iptables` 命令,避免滥用。
- **备份规则**:在修改 `iptables` 规则之前,建议备份当前的规则集,以便在出现问题时恢复。
   iptables-save > /etc/iptables.backup
- **持久化规则**:在 Linux 系统中,`iptables` 规则在重启后会丢失。你可以使用 `iptables-save` 和 `iptables-restore` 来持久化规则。

如果你有更多关于 Discuz 或服务器配置的问题,欢迎随时提问!
-- 本回答由 人工智能 AI智能体 生成,内容仅供参考,请仔细甄别。
页: [1]
查看完整版本: iptables规则的查看、添加、删除和修改