iptables -- 实际使用技巧及原理解析...

本文深入探讨了现代Linux内核中的Netfilter数据包过滤框架及其用户层命令行工具iptables。Netfilter提供了一系列操作来控制进出系统的流量数据包,而iptables则具备强大的防火墙配置功能,允许添加规则构建防火墙策略。文章还介绍了iptables的复杂功能、语法以及规则备份与还原的方法。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

简介

现代 Linux 内核带有一个叫 Netfilter 的数据包过滤框架。Netfilter 提供了允许、丢弃以及修改等操作来控制进出系统的流量数据包。
基于 Netfilter 框架的用户层命令行工具 iptables 提供了强大的防火墙配置功能,允许你添加规则来构建防火墙策略。
iptables 丰富复杂的功能以及其巴洛克式命令语法可能让人难以驾驭。
我将持续更新了解到的新用法及原理!

规则备份与还原

iptables-save > /root/iptables-rules/iptables-works-`date +%F`
iptables-restore < /root/iptables-rules/iptables-works-lastest

注意:这里不是',而是 `

语句基本原则

iptables -t 表名 <-A/I/D/R> 规则链名 [规则号] <-i/o 网卡名> -p 协议名 <-s 源IP/源子网> --sport 源端口 <-d 目标IP/目标子网> --dport 目标端口 -j 动作

表名:
默认filter


    raw:高级功能,如:网址过滤。
    mangle:数据包修改(QOS),用于实现服务质量。
    net:地址转换,用于网关路由器。
    filter:包过滤,用于防火墙规则。

操作类型:

-A   :append附加规则,将新增的规则添加到链的尾部
-I[n] :插入为第n条规则
-D   : 删除第n条规则
-R[n] : 替换第N条

实例:

iptables -A INPUT -p tcp --dport 22 -s 172.16.110.102 -d 10.194.10.1 -j DROP 

参考链接:
参考一:linux中国开源社区
参考二:linux命令

[root@stg-postgres ~]# iptables -nvL Chain INPUT (policy ACCEPT 0 packets, 0 bytes) pkts bytes target prot opt in out source destination 0 0 DROP all -- * * 172.17.167.29 0.0.0.0/0 0 0 DROP all -- * * 172.17.167.28 0.0.0.0/0 151M 157G ACCEPT all -- * * 0.0.0.0/0 0.0.0.0/0 0 0 IOA_IP_INTERCEPT all -- * * 0.0.0.0/0 0.0.0.0/0 Chain FORWARD (policy ACCEPT 0 packets, 0 bytes) pkts bytes target prot opt in out source destination Chain OUTPUT (policy ACCEPT 34152 packets, 27M bytes) pkts bytes target prot opt in out source destination 53M 79G SF_DNS_INTERCEPT all -- * * 0.0.0.0/0 0.0.0.0/0 Chain IOA_IP_INTERCEPT (1 references) pkts bytes target prot opt in out source destination Chain SFIPS (1 references) pkts bytes target prot opt in out source destination 0 0 ACCEPT tcp -- * * 0.0.0.0/0 0.0.0.0/0 tcp spt:54120 0 0 NFQUEUE tcp -- * * 0.0.0.0/0 127.0.0.1 tcp dpt:5433 NFQUEUE num 4 bypass 0 0 NFQUEUE all -- * * !127.0.0.1 0.0.0.0/0 NFQUEUE num 4 bypass Chain SFIPS_CTRL (0 references) pkts bytes target prot opt in out source destination 0 0 SFIPS all -- * * 0.0.0.0/0 0.0.0.0/0 Chain SFIPS_OUT (1 references) pkts bytes target prot opt in out source destination 0 0 ACCEPT tcp -- * * 0.0.0.0/0 0.0.0.0/0 tcp dpt:54120 0 0 NFQUEUE tcp -- * * 127.0.0.1 0.0.0.0/0 tcp spt:5433 NFQUEUE num 4 bypass 0 0 NFQUEUE tcp -- * * !127.0.0.1 0.0.0.0/0 length 0:1024 NFQUEUE num 4 bypass Chain SFIPS_OUT_CTRL (0 references) pkts bytes target prot opt in out source destination 0 0 SFIPS_OUT all -- * * 0.0.0.0/0 0.0.0.0/0 Chain SF_DNS_INTERCEPT (1 references) pkts bytes target prot opt in out source destination上面是我的iptables规则,让后我只执行了iptables -A INPUT -p tcp --dport 5432 -j ACCEPT接着systemctl restart iptables ,在执行iptables -nvL却出现了 [root@stg-postgres ~]# iptables -nvL Chain INPUT (policy ACCEPT 0 packets, 0 bytes) pkts bytes target prot opt in out source destination 0 0 DROP all -- * * 172.17.167.29 0.0.0.0/0 0 0 DROP all -- * * 172.17.167.28 0.0.0.0/0 134 54685 ACCEPT all -- * * 0.0.0.0/0 0.0.0.0/0 0 0 ACCEPT tcp -- * * 0.0.0.0/0 0.0.0.0/0 tcp dpt:5432 Chain FORWARD (policy ACCEPT 0 packets, 0 bytes) pkts bytes target prot opt in out source destination Chain OUTPUT (policy ACCEPT 116 packets, 66510 bytes) pkts bytes target prot opt in out source destination 为啥内容就变少了呢?
03-09
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值