【iptables 实战】03 自定义链

一、新建一个自定义链

当前的机器IP为:10.1.0.10

自定义链IN_WEB,拒绝指定源ip的报文

bash 复制代码
[root@test-c ~]# iptables -t filter -I IN_WEB -s 10.1.0.11 -j REJECT
[root@test-c ~]# iptables -t filter --line -nvL IN_WEB
Chain IN_WEB (0 references)
num   pkts bytes target     prot opt in     out     source               destination         
1        0     0 REJECT     all  --  *      *       10.1.0.11            0.0.0.0/0            reject-with icmp-port-unreachable

二、引用自定义链

定义INPUT规则,引用 IN_WEB规则

bash 复制代码
[root@test-c ~]# iptables -I INPUT -p icmp -j IN_WEB
[root@test-c ~]# iptables -nvL
Chain INPUT (policy ACCEPT 0 packets, 0 bytes)
 pkts bytes target     prot opt in     out     source               destination         
    0     0 IN_WEB     icmp --  *      *       0.0.0.0/0            0.0.0.0/0  
 Chain IN_WEB (1 references)
 pkts bytes target     prot opt in     out     source               destination         
    0     0 REJECT     all  --  *      *       10.1.0.11            0.0.0.0/0            reject-with icmp-port-unreachable

可以看到IN_WEB链,这个时候references的数量为1了,表示有链在引用

bash 复制代码
[root@localhost ~]# ping 10.1.0.10
PING 10.1.0.10 (10.1.0.10) 56(84) bytes of data.
From 10.1.0.10 icmp_seq=1 Destination Port Unreachable

ping 10.1.0.10,可以看到,此时不通了

三、删除自定义链

3.1 先尝试清空IN_WEB,清空后,发现依然有引用。此时再ping10.1.0.10,能ping通了

bash 复制代码
[root@test-c ~]# iptables -F IN_WEB
[root@test-c ~]# iptables -nvL 
Chain INPUT (policy ACCEPT 0 packets, 0 bytes)
 pkts bytes target     prot opt in     out     source               destination         
    5   420 IN_WEB     icmp --  *      *       0.0.0.0/0            0.0.0.0/0           
Chain IN_WEB (1 references)
 pkts bytes target     prot opt in     out     source               destination    

3.2 还原刚才清空的自定义链

bash 复制代码
[root@test-c ~]# iptables -t filter -I IN_WEB -s 10.1.0.11 -j REJECT

3.3 删除自定义链

使用"-X"选项可以删除自定义链,但是删除自定义链时,需要满足两个条件:

1、自定义链没有被任何默认链引用,即自定义链的引用计数为0。

2、自定义链中没有任何规则,即自定义链为空。

那么,我们来删除自定义链WEB试试。

bash 复制代码
[root@test-c ~]# iptables -X IN_WEB
iptables v1.8.4 (nf_tables):  CHAIN_USER_DEL failed (Device or resource busy): chain IN_WEB

删除失败,因为有引用

bash 复制代码
[root@test-c ~]# iptables -nvL INPUT --line
Chain INPUT (policy ACCEPT 0 packets, 0 bytes)
num   pkts bytes target     prot opt in     out     source               destination         
1        7   588 IN_WEB     icmp --  *      *       0.0.0.0/0            0.0.0.0/0           
[root@test-c ~]# iptables -D INPUT 1
[root@test-c ~]# iptables -X IN_WEB
iptables v1.8.4 (nf_tables):  CHAIN_USER_DEL failed (Device or resource busy): chain IN_WEB

发现还是删除失败

我们清空IN_WEB的规则以后,再尝试删除试下

bash 复制代码
[root@test-c ~]# iptables -F IN_WEB
[root@test-c ~]# iptables -X IN_WEB

删除成功了。可能是新版本的删除规则链,要清空规则后,才能删除

相关推荐
码出钞能力35 分钟前
更换libc.so导致linux变砖,通过LD_PRELOAD挽救
linux·服务器
小马学嵌入式~35 分钟前
嵌入式 SQLite 数据库开发笔记
linux·c语言·数据库·笔记·sql·学习·sqlite
青 .1 小时前
数据结构---二叉搜索树的实现
c语言·网络·数据结构·算法·链表
小猪咪piggy1 小时前
【JavaEE】(24) Linux 基础使用和程序部署
linux·运维·服务器
AORO20251 小时前
三防手机的三防是指什么?推荐一款实用机型
网络·5g·智能手机·制造·信息与通信
Haven-1 小时前
Linux常见命令
linux·基本指令
IT 小阿姨(数据库)2 小时前
PgSQL中pg_stat_user_tables 和 pg_stat_user_objects参数详解
linux·运维·数据库·sql·postgresql·oracle
MChine慕青2 小时前
顺序表与单链表:核心原理与实战应用
linux·c语言·开发语言·数据结构·c++·算法·链表
虎头金猫2 小时前
如何在Linux上使用Docker在本地部署开源PDF工具Stirling PDF:StirlingPDF+cpolar让专业操作像在线文档一样简单
linux·运维·ubuntu·docker·pdf·开源·centos
努力学习的小廉3 小时前
深入了解linux系统—— 线程同步
linux·服务器·数据库·算法