当前位置:网站首页>iptables常用命令小清单
iptables常用命令小清单
2022-07-24 15:32:00 【InfoQ】
iptables常用命令小清单

iptables简介
- filter 表:控制数据包是否允许进出及转发,可以控制的链路有 INPUT、FORWARD 和 OUTPUT。
- nat 表:控制数据包中地址转换,可以控制的链路有 PREROUTING、INPUT、OUTPUT 和 POSTROUTING。
- mangle:修改数据包中的原数据,可以控制的链路有 PREROUTING、INPUT、OUTPUT、FORWARD 和 POSTROUTING。
- raw:控制 nat 表中连接追踪机制的启用状况,可以控制的链路有 PREROUTING、OUTPUT。
- INPUT(入站数据过滤)、
- OUTPUT(出站数据过滤)、
- FORWARD(转发数据过滤)、
- PREROUTING(路由前过滤)
- 和POSTROUTING(路由后过滤),

iptables语法
iptables [-t table] command [match] [target]
command
match
1、通用匹配
2、显示匹配
target
iptables命令选项的输入顺序:
iptables -t 表名 <-A/I/D/R> 规则链名 [规则号] <-i/o 网卡名> -p 协议名 <-s 源IP/源子网> --sport 源端口 <-d 目标IP/目标子网> --dport 目标端口 -j 动作
- raw:高级功能,如:网址过滤
- mangle:数据包修改(QOS),用于实现服务质量
- net:地址转换,用于网关路由器
- filter:包过滤,用于防火墙规则
- INPUT链:处理输入数据包
- OUTPUT链:处理输出数据包
- PORWARD链:处理转发数据包
- PREROUTING链:用于目标地址转换(DNAT)
- POSTOUTING链:用于源地址转换(SNAT)
- accept:接收数据包
- DROP:丢弃数据包
- REDIRECT:重定向,映射,透明代理
- SNAT:源地址转换
- DNAT:目标地址转换
- MASQUERADE:IP伪装(NAT),用于ADSL
- LOG:日志记录
实例
清除已有iptables规则
iptables -F
iptables -X
iptables -Z
开发指定的端口
iptables -A INPUT -s 127.0.0.1 -d 127.0.0.1 -j ACCEPT #允许本地回环接口(即运行本机访问本机)
iptables -A INPUT -m state --state ESTABLISHED,RELATED -j ACCEPT #允许已建立的或相关连的通行
iptables -A OUTPUT -j ACCEPT #允许所有本机向外的访问
iptables -A INPUT -p tcp --dport 22 -j ACCEPT #允许访问22端口
iptables -A INPUT -p tcp --dport 80 -j ACCEPT #允许访问80端口
iptables -A INPUT -p tcp --dport 21 -j ACCEPT #允许ftp服务的21端口
iptables -A INPUT -p tcp --dport 20 -j ACCEPT #允许FTP服务的20端口
iptables -A INPUT -j reject #禁止其他未允许的规则访问
iptables -A FORWARD -j REJECT #禁止其他未允许的规则访问
屏蔽IP
iptables -I INPUT -s 123.45.6.7 -j DROP #屏蔽单个IP的命令
iptables -I INPUT -s 123.0.0.0/8 -j DROP #封整个段即从123.0.0.1到123.255.255.254的命令
iptables -I INPUT -s 124.45.0.0/16 -j DROP #封IP段即从123.45.0.1到123.45.255.254的命令
iptables -I INPUT -s 123.45.6.0/24 -j DROP #封IP段即从123.45.6.1到123.45.6.254的命令是
查看已添加的iptables规则
iptables -L -n -v
Chain INPUT (policy DROP 48106 packets, 2690K bytes)
pkts bytes target prot opt in out source destination
5075 589K ACCEPT all -- lo * 0.0.0.0/0 0.0.0.0/0
191K 90M ACCEPT tcp -- * * 0.0.0.0/0 0.0.0.0/0 tcp dpt:22
1499K 133M ACCEPT tcp -- * * 0.0.0.0/0 0.0.0.0/0 tcp dpt:80
4364K 6351M ACCEPT all -- * * 0.0.0.0/0 0.0.0.0/0 state RELATED,ESTABLISHED
6256 327K ACCEPT icmp -- * * 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 3382K packets, 1819M bytes)
pkts bytes target prot opt in out source destination
5075 589K ACCEPT all -- * lo 0.0.0.0/0 0.0.0.0/0
将所有iptables以序号标记显示,执行:
iptables -L -n --line-numbers
删除已添加的iptables规则
iptables -D INPUT 8
iptables调试
1.准备ipt_LOG内核模块
modprobe ipt_LOG
modprobe nf_log_ipv4
sysctl net.netfilter.nf_log.2=nf_log_ipv4
2.使用raw表,加入跟踪规则
iptables -t raw -A PREROUTING -p icmp -j TRACE
iptables -t raw -A OUTPUT -p icmp -j TRACE
iptables -t raw -A PREROUTING -p tcp -dport 80 -j TRACE
iptables -t raw -A OUTPUT -p tcp -dport 80 -j TRACE
3.查看日志
tail -f /var/log/kern.log
参考来源:
边栏推荐
- Which securities company is good at opening an account with flush? Excuse me, is it safe to open an account with mobile phone or stock?
- YOLO5Face:为什么要重新发明人脸检测器
- 遭受DDoS时,高防IP和高防CDN的选择
- MATLAB image defogging technology GUI interface - global balance histogram
- 新手第一次怎么买股票 哪家证券公司开户最好最安全
- 【量化测试】
- 维护香港服务器安全的9个关键措施
- 报错【项目报错】
- Five principles of solid are indispensable for good architecture design
- C. Recover an RBS
猜你喜欢

力扣 31.下一个排列--双指针法

【USENIX ATC'22】支持异构GPU集群的超大规模模型的高效的分布式训练框架Whale

MATLAB image defogging technology GUI interface - global balance histogram

你不能只会flex居中布局,精制动画讲解所有flex布局方式!通俗易懂纯干货教程!...

VAE(变分自编码器)的一些难点分析

You can't just focus on flex layout and elaborate animation to explain all flex layout methods! Easy to understand dry goods tutorial
![[300 opencv routines] 238. Harris corner detection in opencv](/img/d7/abdada7cd97060c55861b1bc2a2e13.png)
[300 opencv routines] 238. Harris corner detection in opencv

遭受DDoS时,高防IP和高防CDN的选择

Android SQLite database practice

Intuitive understanding of various normalization
随机推荐
Leetcode-09 (next rank + happy number + full rank)
MySQL learning notes (summary)
Existence form and legitimacy of real data in C language (floating point number)
每天20分钟之feign
Activity Registration: how to quickly start the open source tapdata live data platform on a zero basis?
Applet tab
DS binary tree - maximum distance of binary tree nodes
Error in anyjson setup command: use_ 2to3 is invalid.
Reentrantlock reentrant lock
基于Lambert函数的时滞系统稳定性研究
上课作业(6)——#575. 单词的划分(word)
Nine key measures to maintain server security in Hong Kong
2022 robocom world robot developer competition - undergraduate group (provincial competition) -- question 3 running team robot (finished)
MongoDB入门学习
【AdaptiveAvgPool3d】pytorch教程
MySQL学习笔记(总结)
MySQL function
【ACWing】909. 下棋游戏
25. From disk to file
JSON file editor