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

简介:
iptables规则的查看、添加、删除和修改
这里只列出比较常用的参数,详细的请查看man iptables

1、查看
iptables -nvL –line-number

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

2、添加
添加规则有两个参数:-A和-I。其中-A是添加到规则的末尾;-I可以插入到指定位置,没有指定位置的话默认插入到规则的首部。

当前规则:

[root@test ~]# iptables -nL --line-number
Chain INPUT (policy ACCEPT)
num  target     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
添加一条规则到尾部:

[root@test ~]# iptables -A INPUT -s 192.168.1.5 -j DROP
再插入一条规则到第三行,将行数直接写到规则链的后面:

[root@test ~]# iptables -I INPUT 3 -s 192.168.1.3 -j DROP
查看:

[root@test ~]# iptables -nL --line-number
Chain INPUT (policy ACCEPT)
num  target     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):

[root@test ~]# iptables -D INPUT -s 192.168.1.5 -j DROP
有时候要删除的规则太长,删除时要写一大串,既浪费时间又容易写错,这时我们可以先使用–line-number找出该条规则的行号,再通过行号删除规则。

[root@test ~]# iptables -nv --line-number
iptables v1.4.7: no command specified
Try `iptables -h' or 'iptables --help' for more information.
[root@test ~]# iptables -nL --line-number
Chain INPUT (policy ACCEPT)
num  target     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
删除第二行规则

[root@test ~]# iptables -D INPUT 2
4、修改
修改使用-R参数

先看下当前规则:

[root@test ~]# iptables -nL --line-number
Chain INPUT (policy ACCEPT)
num  target     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:

[root@test ~]# iptables -R INPUT 3 -j ACCEPT
再查看下:

[root@test ~]# iptables -nL --line-number
Chain INPUT (policy ACCEPT)
num  target     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。




本文转自 xoyabc 51CTO博客,原文链接:http://blog.51cto.com/xoyabc/1659162,如需转载请自行联系原作者

相关文章
|
安全 Linux 网络安全
组网神器WireGuard安装与配置教程(超详细)
组网神器WireGuard安装与配置教程(超详细)
74262 2
|
弹性计算 JavaScript Ubuntu
WebSocket协议相关的测试命令工具使用简介
本文介绍了针对WebSocket的测试工具wscat和websocat的基本使用方法,以及通过curl命令测试HTTP/HTTPS协议的方式。对于WebSocket,直接使用curl测试较为复杂,推荐使用wscat或websocat。文中详细说明了这两种工具的安装步骤、常用参数及连接示例,例如在ECS上开启8080端口监听并进行消息收发测试。此外,还提供了curl命令的手动设置头部信息以模拟WebSocket握手的示例,但指出curl仅能作为客户端测试工具,无法模拟服务器。
4589 5
|
4月前
|
人工智能 安全 5G
阿里企业邮箱多少钱一年?2026年收费价格标准版、AI尊享版及国产化版费用整理
阿里企业邮箱2026年最新定价:标准版540元/年(5–100账号)、AI尊享版720元/年(5–300账号,含智能写信、翻译、润色等AI功能)、国产化版810元/年(5–1000账号)。各版本网盘容量、附件大小及适用场景不同,满足企业多样化需求。阿里企业邮箱官网:https://t.aliyun.com/U/gNeTEB
wxid加好友工具插件,微信号wxid转换器, 在线wxid转微信号
本资源提供微信wxid相关技术源码及生成器高级用法示例,包括数据流处理、斐波那契数列生成和批处理生成器等功能。
|
8月前
|
Rust 安全 Ubuntu
手把手教你用bindgen:让Rust轻松调用C库
bindgen是Rust官方推荐的FFI绑定生成工具,可自动将C/C++头文件转换为安全、正确的Rust FFI代码,精准处理类型映射、内存布局与符号导出。本文以bzip2为例,详解从环境配置、build.rs脚本编写到压缩/解压缩功能验证的完整实践流程。
WGFIX资产设备系统 v1.0 全新发布 功能介绍
WGFIX是wgcloud团队最近发布的一个资产管理系统,免费实用,而且使用非常简单,部署也很方便,非常适合中小企业使用的一款IT固定资产管理系统
|
Ubuntu Unix Linux
Linux 用户使用sudo时 显示xxx is not in the sudoers file.This incident will be reported.的解决方法
Linux 用户使用sudo时 显示xxx is not in the sudoers file.This incident will be reported.的解决方法
3871 0
|
Web App开发
WebRTC 音视频同步原理与实现
所有的基于网络传输的音视频采集播放系统都会存在音视频同步的问题,作为现代互联网实时音视频通信系统的代表,WebRTC 也不例外。本文将对音视频同步的原理以及 WebRTC 的实现做深入分析。
WebRTC 音视频同步原理与实现

热门文章

最新文章