在Linux网络管理和监控领域,conntrack
命令是一个强大的工具,它提供了对netfilter连接跟踪系统的直接访问🔍。这篇文章将深入探讨conntrack
的由来、底层原理、参数意义,以及其常见用法,并对返回结果的每个字段进行详细解释。
conntrack
命令源于Linux的netfilter项目🌐,这是一个内置于Linux内核中的网络包处理模块。Netfilter支持各种网络相关任务,如包过滤(防火墙)🔥、网络地址转换(NAT)🔄和连接跟踪。conntrack
工具最初设计的目的是为了管理和监视netfilter的连接跟踪系统,这个系统记录了所有经过防火墙的网络连接的状态信息。
conntrack
工具的底层原理基于netfilter的连接跟踪表📈,该表位于内核空间。每当网络包经过netfilter时,连接跟踪系统会检查包的信息,如源IP地址、目的IP地址、传输层协议(TCP/UDP)、端口等,并据此更新内部的连接跟踪表。这个表包含了所有活跃连接的状态信息,例如是否已建立连接、连接是否已关闭等。
conntrack
命令支持多种参数,用于执行不同的操作✅:
-L, --list
:列出连接跟踪表中的所有条目。-G, --get
:获取单个连接跟踪条目的信息。-D, --delete
:从连接跟踪表中删除条目。-I, --create
:创建一个新的连接跟踪条目。-U, --update
:更新已存在的连接跟踪条目。-E, --event
:监听连接跟踪事件。当你运行conntrack -L
时,会看到类似以下的输出:
tcp 6 431999 ESTABLISHED src=192.168.1.2 dst=93.184.216.34 sport=34567 dport=80 [UNREPLIED] src=93.184.216.34 dst=192.168.1.2 sport=80 dport=34567
每个字段的含义如下📝:
tcp
、udp
等。6
代表tcp连接、17
代表udp连接等。ESTABLISHED
:已建立连接TIME_WAIT
:等待足够的时间以确保远程TCP接收到连接终止请求的确认)CLOSE_WAIT
:CLOSE_WAIT状态表示对端(远程主机)已经关闭了连接的一半(发送了一个FIN包),并且本地端(你的计算机)已经接收到这个关闭请求,但是本地应用程序还没有关闭(或者说还没有调用close来关闭连接)。简单来说,CLOSE_WAIT状态意味着TCP连接在等待本地应用程序去关闭连接。SYN_SENT
:客户端已发送一个连接请求(SYN包)给服务器,并等待服务器的确认。这表示客户端已经开始了TCP三次握手过程SYN_RECE
:服务器收到客户端的SYN包,并回应一个SYN+ACK包,等待客户端的确认。这个状态表示服务器端已经响应了连接请求,正在进行三次握手的第二步。FIN_WAIT_1
:的一方(通常是客户端)决定关闭连接,并发送一个FIN包给对方,等待对方的确认。这标志着连接关闭过程的开始FIN_WAIT_2
:在发送FIN包并收到ACK包后,连接进入FIN_WAIT_2状态。在这个状态下,连接的关闭一方等待对方的FIN包。CLOSE_WAIT
:当一方收到另一方的FIN包,即对方请求关闭连接时,它会发送一个ACK包作为回应,并进入CLOSE_WAIT状态。在这个状态下,等待本地应用程序关闭连接。CLOSING
:在同时关闭的情况下,当双方几乎同时发送FIN包时,连接会进入CLOSING状态,表示双方都在等待对方的FIN包的确认。LAST_ACK
:当处于CLOSE_WAIT状态的一方发送FIN包,并等待对方的最终ACK包时,连接进入LAST_ACK状态。TIME_WAIT
:在收到对方的FIN包并发送ACK包后,连接进入TIME_WAIT状态。这个状态持续一段时间(2倍的MSL,最大报文生存时间),以确保对方收到了最终的ACK包。这也允许老的重复数据包在网络中消失。CLOSED
:连接完全关闭,两端都释放了连接的资源。src=源IP dst=目的IP
:表示数据包的源和目的IP地址。sport=源端口 dport=目的端口
:表示数据包的源和目的端口号。[UNREPLIED]
:表示从源到目标的连接请求尚未收到回复。[ASSURED]
:表示连接已经被确认,不会因为短时间内没有数据包而被清除。mark
字段用于表示特定的连接跟踪条目被打上的标记(或称为标签)mark
的取值范围是从0
到4294967295
(即2^32-1),其中0
通常表示未被标记的连接。非零的值则根据实际的防火墙规则和策略而定,不同的值可以代表不同的分类或处理逻辑。例如,在某些配置中,mark
可以用来区分经过VPN的流量、被特定规use
字段表示当前有多少个内核组件正在引用这个连接跟踪条目。简而言之,它表明这个连接跟踪条目的“使用度”或“引用计数”。use
的取值是一个正整数,起始值至少为1
,表示至少有一个引用(即连接跟踪本身)。如果有多个内核组件因为某些原因(如特定的路由或防火墙规则)同时需要跟踪这个连接,use
的值会相应增加。一般而言,大多数情况下use
的值会比较低,除非系统配置导致多个组件需要引用同一个连接跟踪条目。命令:conntrack -L -p tcp
示例输出:
tcp 6 431999 ESTABLISHED src=192.168.1.100 dst=192.168.1.1 sport=34567 dport=22 [ASSURED] mark=0 use=1
解释:
ESTABLISHED
。192.168.1.100
的34567
端口到目的地址192.168.1.1
的22
端口。[ASSURED]
表示连接已经被确认。mark=0 use=1
提供了额外的连接标记和使用信息。命令:conntrack -L -p udp
示例输出:
udp 17 28 src=192.168.1.100 dst=192.168.1.255 sport=137 dport=137 [UNREPLIED] src=192.168.1.255 dst=192.168.1.100 sport=137 dport=137 mark=0 use=1
解释:
192.168.1.100
到192.168.1.255
,这是一个局域网广播地址。[UNREPLIED]
标志表示广播请求尚未收到回复。命令:conntrack -E
示例输出:实时显示连接跟踪事件,如新建(NEW)、更新(UPDATE)和销毁(DESTROY)事件。
root@linux-study:~# conntrack -E|head -n 20
[DESTROY] tcp 6 src=192.168.201.109 dst=182.75.23.156 sport=40175 dport=443 src=182.75.23.156 dst=172.30.3.222 sport=443 dport=40175 [ASSURED]
[NEW] tcp 6 120 SYN_SENT src=192.168.201.109 dst=182.75.23.156 sport=40175 dport=443 [UNREPLIED] src=182.75.23.156 dst=172.30.3.222 sport=443 dport=40175
[UPDATE] tcp 6 60 SYN_RECV src=192.168.201.109 dst=182.75.23.156 sport=40175 dport=443 src=182.75.23.156 dst=172.30.3.222 sport=443 dport=40175
[UPDATE] tcp 6 432000 ESTABLISHED src=192.168.201.109 dst=182.75.23.156 sport=40175 dport=443 src=182.75.23.156 dst=172.30.3.222 sport=443 dport=40175 [ASSURED]
[NEW] tcp 6 120 SYN_SENT src=192.168.201.109 dst=182.75.23.156 sport=24526 dport=443 [UNREPLIED] src=182.75.23.156 dst=172.30.3.222 sport=443 dport=24526
[UPDATE] tcp 6 60 SYN_RECV src=192.168.201.109 dst=182.75.23.156 sport=24526 dport=443 src=182.75.23.156 dst=172.30.3.222 sport=443 dport=24526
[UPDATE] tcp 6 432000 ESTABLISHED src=192.168.201.109 dst=182.75.23.156 sport=24526 dport=443 src=182.75.23.156 dst=172.30.3.222 sport=443 dport=24526 [ASSURED]
[NEW] tcp 6 120 SYN_SENT src=192.168.201.110 dst=182.75.23.156 sport=6069 dport=443 [UNREPLIED] src=182.75.23.156 dst=172.30.3.222 sport=443 dport=6069
[UPDATE] tcp 6 60 SYN_RECV src=192.168.201.110 dst=182.75.23.156 sport=6069 dport=443 src=182.75.23.156 dst=172.30.3.222 sport=443 dport=6069
[UPDATE] tcp 6 432000 ESTABLISHED src=192.168.201.110 dst=182.75.23.156 sport=6069 dport=443 src=182.75.23.156 dst=172.30.3.222 sport=443 dport=6069 [ASSURED]
[UPDATE] tcp 6 120 FIN_WAIT src=192.168.201.109 dst=182.75.23.156 sport=24526 dport=443 src=182.75.23.156 dst=172.30.3.222 sport=443 dport=24526 [ASSURED]
[UPDATE] tcp 6 60 CLOSE_WAIT src=192.168.201.109 dst=182.75.23.156 sport=24526 dport=443 src=182.75.23.156 dst=172.30.3.222 sport=443 dport=24526 [ASSURED]
[UPDATE] tcp 6 30 LAST_ACK src=192.168.201.109 dst=182.75.23.156 sport=24526 dport=443 src=182.75.23.156 dst=172.30.3.222 sport=443 dport=24526 [ASSURED]
[UPDATE] tcp 6 120 TIME_WAIT src=192.168.201.109 dst=182.75.23.156 sport=24526 dport=443 src=182.75.23.156 dst=172.30.3.222 sport=443 dport=24526 [ASSURED]
[NEW] tcp 6 120 SYN_SENT src=192.168.201.109 dst=182.75.23.156 sport=7378 dport=443 [UNREPLIED] src=182.75.23.156 dst=172.30.3.222 sport=443 dport=7378
[UPDATE] tcp 6 60 SYN_RECV src=192.168.201.109 dst=182.75.23.156 sport=7378 dport=443 src=182.75.23.156 dst=172.30.3.222 sport=443 dport=7378
[UPDATE] tcp 6 432000 ESTABLISHED src=192.168.201.109 dst=182.75.23.156 sport=7378 dport=443 src=182.75.23.156 dst=172.30.3.222 sport=443 dport=7378 [ASSURED]
[UPDATE] tcp
解释:
考虑以下conntrack
命令的输出示例:
tcp 6 431999 ESTABLISHED src=192.168.1.100 dst=93.184.216.34 sport=34567 dport=80 [ASSURED] mark=0 use=1
udp 17 170 src=192.168.1.100 dst=192.168.1.255 sport=137 dport=137 [UNREPLIED] src=192.168.1.255 dst=192.168.1.100 sport=137 dport=137 mark=0 use=1
在这个示例中:
6
是TCP的协议号,431999
是剩余的计时器值(单位是秒),表示该TCP连接跟踪条目将在大约431999秒后从连接跟踪表中移除,除非有新的数据包更新此连接状态。17
是UDP的协议号,170
是剩余的计时器值,表示该UDP连接跟踪条目将在170秒后从连接跟踪表中移除,如果没有新的数据包到达更新状态。命令:conntrack -L
root@linux-study:~# conntrack -L|head
tcp 6 75 TIME_WAIT src=192.168.201.108 dst=182.75.23.156 sport=12753 dport=443 src=182.75.23.156 dst=172.30.3.222 sport=443 dport=12753 [ASSURED] mark=0 use=1
tcp 6 29 TIME_WAIT src=192.168.201.104 dst=182.75.23.156 sport=22689 dport=443 src=182.75.23.156 dst=172.30.3.222 sport=443 dport=22689 [ASSURED] mark=0 use=1
tcp 6 2 CLOSE src=192.168.201.109 dst=182.75.23.156 sport=55566 dport=443 src=182.75.23.156 dst=172.30.3.222 sport=443 dport=55566 [ASSURED] mark=0 use=1
tcp 6 80 TIME_WAIT src=192.168.201.109 dst=182.75.23.156 sport=3815 dport=443 src=182.75.23.156 dst=172.30.3.222 sport=443 dport=3815 [ASSURED] mark=0 use=1
tcp 6 37 TIME_WAIT src=192.168.201.109 dst=182.75.23.156 sport=5126 dport=443 src=182.75.23.156 dst=172.30.3.222 sport=443 dport=5126 [ASSURED] mark=0 use=1
tcp 6 0 TIME_WAIT src=192.168.201.109 dst=182.75.23.156 sport=64894 dport=443 src=182.75.23.156 dst=172.30.3.222 sport=443 dport=64894 [ASSURED] mark=0 use=1
tcp 6 5 CLOSE src=192.168.201.109 dst=182.75.23.156 sport=15022 dport=443 src=182.75.23.156 dst=172.30.3.222 sport=443 dport=15022 [ASSURED] mark=0 use=1
tcp 6 12 TIME_WAIT src=192.168.201.109 dst=182.75.23.156 sport=46340 dport=443 src=182.75.23.156 dst=172.30.3.222 sport=443 dport=46340 [ASSURED] mark=0 use=1
tcp 6 21 TIME_WAIT src=192.168.201.109 dst=182.75.23.156 sport=62983 dport=443 src=182.75.23.156 dst=172.30.3.222 sport=443 dport=62983 [ASSURED] mark=0 use=1
tcp 6 47 TIME_WAIT src=192.168.201.109 dst=182.75.23.156 sport=63001 dport=443 src=182.75.23.156 dst=172.30.3.222 sport=443 dport=63001 [ASSURED] mark=0 use=1
root@linux-study:~#
只显示TCP连接条目命令:conntrack -L -p tcp
root@linux-study:~# conntrack -L -p tcp|head
tcp 6 40 TIME_WAIT src=192.168.201.109 dst=182.75.23.156 sport=40143 dport=443 src=182.75.23.156 dst=172.30.3.222 sport=443 dport=40143 [ASSURED] mark=0 use=1
tcp 6 46 TIME_WAIT src=192.168.201.109 dst=182.75.23.156 sport=49611 dport=443 src=182.75.23.156 dst=172.30.3.222 sport=443 dport=49611 [ASSURED] mark=0 use=1
tcp 6 96 TIME_WAIT src=192.168.201.108 dst=182.75.23.156 sport=51938 dport=443 src=182.75.23.156 dst=172.30.3.222 sport=443 dport=51938 [ASSURED] mark=0 use=1
tcp 6 25 TIME_WAIT src=192.168.201.109 dst=182.75.23.156 sport=45806 dport=443 src=182.75.23.156 dst=172.30.3.222 sport=443 dport=45806 [ASSURED] mark=0 use=1
tcp 6 10 TIME_WAIT src=192.168.201.108 dst=182.75.23.156 sport=39921 dport=443 src=182.75.23.156 dst=172.30.3.222 sport=443 dport=39921 [ASSURED] mark=0 use=1
tcp 6 23 TIME_WAIT src=192.168.201.108 dst=182.75.23.156 sport=21268 dport=443 src=182.75.23.156 dst=172.30.3.222 sport=443 dport=21268 [ASSURED] mark=0 use=1
tcp 6 114 TIME_WAIT src=192.168.201.109 dst=182.75.23.156 sport=64894 dport=443 src=182.75.23.156 dst=172.30.3.222 sport=443 dport=64894 [ASSURED] mark=0 use=1
tcp 6 59 TIME_WAIT src=192.168.201.109 dst=182.75.23.156 sport=24358 dport=443 src=182.75.23.156 dst=172.30.3.222 sport=443 dport=24358 [ASSURED] mark=0 use=1
tcp 6 39 TIME_WAIT src=192.168.201.109 dst=182.75.23.156 sport=20256 dport=443 src=182.75.23.156 dst=172.30.3.222 sport=443 dport=20256 [ASSURED] mark=0 use=1
tcp 6 49 TIME_WAIT src=192.168.201.109 dst=182.75.23.156 sport=51766 dport=443 src=182.75.23.156 dst=172.30.3.222 sport=443 dport=51766 [ASSURED] mark=0 use=1
root@linux-study:~#
只显示UDP连接条目命令:conntrack -L -p udp
root@linux-study:~# conntrack -L -p udp
udp 17 12 src=192.168.201.101 dst=172.30.3.22 sport=60049 dport=53 src=172.30.3.22 dst=172.30.3.222 sport=53 dport=60049 mark=0 use=1
udp 17 15 src=192.168.201.103 dst=172.30.3.22 sport=56998 dport=53 src=172.30.3.22 dst=172.30.3.222 sport=53 dport=56998 mark=0 use=1
udp 17 26 src=0.0.0.0 dst=255.255.255.255 sport=68 dport=67 [UNREPLIED] src=255.255.255.255 dst=0.0.0.0 sport=67 dport=68 mark=0 use=1
udp 17 7 src=172.22.77.130 dst=172.30.3.222 sport=500 dport=500 src=192.168.201.104 dst=172.22.77.130 sport=20500 dport=500 mark=0 use=1
udp 17 23 src=172.30.3.67 dst=172.30.3.255 sport=138 dport=138 [UNREPLIED] src=172.30.3.255 dst=172.30.3.67 sport=138 dport=138 mark=0 use=1
udp 17 24 src=192.168.201.112 dst=169.254.253.1 sport=23965 dport=514 [UNREPLIED] src=169.254.253.1 dst=172.30.3.222 sport=514 dport=23965 mark=0 use=1
udp 17 96 src=192.168.201.102 dst=172.30.3.22 sport=59242 dport=53 src=172.30.3.22 dst=172.30.3.222 sport=53 dport=59242 [ASSURED] mark=0 use=1
udp 17 16 src=192.168.201.103 dst=172.30.3.22 sport=36173 dport=53 src=172.30.3.22 dst=172.30.3.222 sport=53 dport=36173 [ASSURED] mark=0 use=1
udp 17 28 src=192.168.201.113 dst=169.254.253.1 sport=10503 dport=514 [UNREPLIED] src=169.254.253.1 dst=172.30.3.222 sport=514 dport=10503 mark=0 use=1
udp 17 2 src=172.30.3.138 dst=172.30.3.255 sport=50195 dport=21027 [UNREPLIED] src=172.30.3.255 dst=172.30.3.138 sport=21027 dport=50195 mark=0 use=1
udp 17 109 src=192.168.201.103 dst=172.30.3.22 sport=46015 dport=53 src=172.30.3.22 dst=172.30.3.222 sport=53 dport=46015 [ASSURED] mark=0 use=1
udp 17 65 src=192.168.201.101 dst=172.30.3.22 sport=34401 dport=53 src=172.30.3.22 dst=172.30.3.222 sport=53 dport=34401 [ASSURED] mark=0 use=1
udp 17 35 src=192.168.201.102 dst=172.30.3.22 sport=60903 dport=53 src=172.30.3.22 dst=172.30.3.222 sport=53 dport=60903 [ASSURED] mark=0 use=1
udp 17 118 src=172.22.77.130 dst=172.30.3.222 sport=4500 dport=4500 src=192.168.201.104 dst=172.22.77.130 sport=24500 dport=4500 [ASSURED] mark=0 use=1
udp 17 4 src=192.168.201.101 dst=91.189.91.157 sport=58566 dport=123 src=91.189.91.157 dst=172.30.3.222 sport=123 dport=58566 mark=0 use=1
udp 17 74 src=192.168.201.103 dst=172.30.3.22 sport=52279 dport=53 src=172.30.3.22 dst=172.30.3.222 sport=53 dport=52279 [ASSURED] mark=0 use=1
udp 17 27 src=192.168.201.110 dst=169.254.253.1 sport=63647 dport=514 [UNREPLIED] src=169.254.253.1 dst=172.30.3.222 sport=514 dport=63647 mark=0 use=1
udp 17 28 src=192.168.201.113 dst=169.254.253.1 sport=60794 dport=514 [UNREPLIED] src=169.254.253.1 dst=172.30.3.222 sport=514 dport=60794 mark=0 use=1
conntrack v1.4.5 (conntrack-tools): 18 flow entries have been shown.
root@linux-study:~#
命令:conntrack -D --src 192.168.1.2
只显示处于ESTABLISHED状态的TCP连接:conntrack -L -p tcp --state ESTABLISHED
root@linux-study:~# conntrack -L -p tcp --state ESTABLISHED|head
tcp 6 431991 ESTABLISHED src=172.22.70.136 dst=172.30.3.222 sport=43000 dport=443 src=192.168.201.107 dst=172.22.70.136 sport=59443 dport=43000 [ASSURED] mark=0 use=1
tcp 6 431951 ESTABLISHED src=172.30.5.90 dst=172.30.3.222 sport=65130 dport=22 src=172.30.3.222 dst=172.30.5.90 sport=22 dport=65130 [ASSURED] mark=0 use=1
tcp 6 431996 ESTABLISHED src=192.168.201.2 dst=192.168.201.101 sport=37554 dport=6443 src=192.168.201.101 dst=192.168.201.2 sport=6443 dport=37554 [ASSURED] mark=0 use=1
tcp 6 431992 ESTABLISHED src=172.30.3.192 dst=172.30.3.222 sport=54920 dport=443 src=192.168.201.107 dst=172.30.3.192 sport=59443 dport=54920 [ASSURED] mark=0 use=1
tcp 6 431955 ESTABLISHED src=192.168.201.108 dst=182.75.23.156 sport=33186 dport=443 src=182.75.23.156 dst=172.30.3.222 sport=443 dport=33186 [ASSURED] mark=0 use=1
tcp 6 431999 ESTABLISHED src=192.168.201.109 dst=182.75.23.156 sport=16756 dport=443 src=182.75.23.156 dst=172.30.3.222 sport=443 dport=16756 [ASSURED] mark=0 use=1
tcp 6 431647 ESTABLISHED src=172.22.3.199 dst=172.30.3.222 sport=60828 dport=22 src=172.30.3.222 dst=172.22.3.199 sport=22 dport=60828 [ASSURED] mark=0 use=1
tcp 6 431998 ESTABLISHED src=192.168.201.108 dst=182.75.23.156 sport=22507 dport=443 src=182.75.23.156 dst=172.30.3.222 sport=443 dport=22507 [ASSURED] mark=0 use=1
tcp 6 431999 ESTABLISHED src=192.168.201.108 dst=182.75.23.156 sport=17720 dport=443 src=182.75.23.156 dst=172.30.3.222 sport=443 dport=17720 [ASSURED] mark=0 use=1
tcp 6 431999 ESTABLISHED src=192.168.201.111 dst=182.75.23.156 sport=21704 dport=443 src=182.75.23.156 dst=172.30.3.222 sport=443 dport=21704 [ASSURED] mark=0 use=1
root@linux-study:~#
删除所有处于TIME_WAIT状态的连接条目:conntrack -D --state TIME_WAIT
显示所有来自特定IP地址的连接条目:conntrack -L -s 192.168.201.111
root@linux-study:~# conntrack -L -s 192.168.201.111|head
tcp 6 5 TIME_WAIT src=192.168.201.111 dst=182.75.23.156 sport=33961 dport=443 src=182.75.23.156 dst=172.30.3.222 sport=443 dport=33961 [ASSURED] mark=0 use=1
tcp 6 43 TIME_WAIT src=192.168.201.111 dst=182.75.23.156 sport=11361 dport=443 src=182.75.23.156 dst=172.30.3.222 sport=443 dport=11361 [ASSURED] mark=0 use=1
tcp 6 75 TIME_WAIT src=192.168.201.111 dst=182.75.23.156 sport=21555 dport=443 src=182.75.23.156 dst=172.30.3.222 sport=443 dport=21555 [ASSURED] mark=0 use=1
tcp 6 101 TIME_WAIT src=192.168.201.111 dst=182.75.23.156 sport=1895 dport=443 src=182.75.23.156 dst=172.30.3.222 sport=443 dport=1895 [ASSURED] mark=0 use=2
tcp 6 85 TIME_WAIT src=192.168.201.111 dst=182.75.23.156 sport=19260 dport=443 src=182.75.23.156 dst=172.30.3.222 sport=443 dport=19260 [ASSURED] mark=0 use=1
tcp 6 15 TIME_WAIT src=192.168.201.111 dst=182.75.23.156 sport=5298 dport=443 src=182.75.23.156 dst=172.30.3.222 sport=443 dport=5298 [ASSURED] mark=0 use=1
tcp 6 2 TIME_WAIT src=192.168.201.111 dst=182.75.23.156 sport=2753 dport=443 src=182.75.23.156 dst=172.30.3.222 sport=443 dport=2753 [ASSURED] mark=0 use=1
tcp 6 1 TIME_WAIT src=192.168.201.111 dst=182.75.23.156 sport=53251 dport=443 src=182.75.23.156 dst=172.30.3.222 sport=443 dport=53251 [ASSURED] mark=0 use=1
tcp 6 114 TIME_WAIT src=192.168.201.111 dst=182.75.23.156 sport=31972 dport=443 src=182.75.23.156 dst=172.30.3.222 sport=443 dport=31972 [ASSURED] mark=0 use=1
tcp 6 114 TIME_WAIT src=192.168.201.111 dst=182.75.23.156 sport=47645 dport=443 src=182.75.23.156 dst=172.30.3.222 sport=443 dport=47645 [ASSURED] mark=0 use=1
root@linux-study:~#
显示所有发送到特定IP地址的连接条目:conntrack -L -d 182.75.23.156
oot@linux-study:~# conntrack -L -d 182.75.23.156|head
tcp 6 35 TIME_WAIT src=192.168.201.109 dst=182.75.23.156 sport=50720 dport=443 src=182.75.23.156 dst=172.30.3.222 sport=443 dport=50720 [ASSURED] mark=0 use=1
tcp 6 111 TIME_WAIT src=192.168.201.109 dst=182.75.23.156 sport=38312 dport=443 src=182.75.23.156 dst=172.30.3.222 sport=443 dport=38312 [ASSURED] mark=0 use=1
tcp 6 21 TIME_WAIT src=192.168.201.106 dst=182.75.23.156 sport=8688 dport=443 src=182.75.23.156 dst=172.30.3.222 sport=443 dport=8688 [ASSURED] mark=0 use=1
tcp 6 113 TIME_WAIT src=192.168.201.109 dst=182.75.23.156 sport=54751 dport=443 src=182.75.23.156 dst=172.30.3.222 sport=443 dport=54751 [ASSURED] mark=0 use=1
tcp 6 103 TIME_WAIT src=192.168.201.109 dst=182.75.23.156 sport=22584 dport=443 src=182.75.23.156 dst=172.30.3.222 sport=443 dport=22584 [ASSURED] mark=0 use=1
tcp 6 6 CLOSE src=192.168.201.110 dst=182.75.23.156 sport=40445 dport=443 src=182.75.23.156 dst=172.30.3.222 sport=443 dport=40445 [ASSURED] mark=0 use=1
tcp 6 85 TIME_WAIT src=192.168.201.109 dst=182.75.23.156 sport=46340 dport=443 src=182.75.23.156 dst=172.30.3.222 sport=443 dport=46340 [ASSURED] mark=0 use=1
tcp 6 66 TIME_WAIT src=192.168.201.109 dst=182.75.23.156 sport=12944 dport=443 src=182.75.23.156 dst=172.30.3.222 sport=443 dport=12944 [ASSURED] mark=0 use=1
tcp 6 59 TIME_WAIT src=192.168.201.109 dst=182.75.23.156 sport=5575 dport=443 src=182.75.23.156 dst=172.30.3.222 sport=443 dport=5575 [ASSURED] mark=0 use=1
tcp 6 34 TIME_WAIT src=192.168.201.109 dst=182.75.23.156 sport=47638 dport=443 src=182.75.23.156 dst=172.30.3.222 sport=443 dport=47638 [ASSURED] mark=0 use=1
root@linux-study:~#
显示所有使用特定源端口的连接条目:conntrack -L -p tcp --sport 22
root@linux-study:~#
root@linux-study:~# conntrack -L -p tcp --sport 57112
tcp 6 61 TIME_WAIT src=192.168.201.108 dst=182.75.23.156 sport=57112 dport=443 src=182.75.23.156 dst=172.30.3.222 sport=443 dport=57112 [ASSURED] mark=0 use=1
conntrack v1.4.5 (conntrack-tools): 1 flow entries have been shown.
root@linux-study:~#
root@linux-study:~#
显示所有使用特定目的端口的连接条目:conntrack -L -p tcp --dport 443
root@linux-study:~# conntrack -L -p tcp --dport 443|head
tcp 6 102 TIME_WAIT src=192.168.201.110 dst=182.75.23.156 sport=9099 dport=443 src=182.75.23.156 dst=172.30.3.222 sport=443 dport=9099 [ASSURED] mark=0 use=1
tcp 6 37 TIME_WAIT src=192.168.201.109 dst=182.75.23.156 sport=3253 dport=443 src=182.75.23.156 dst=172.30.3.222 sport=443 dport=3253 [ASSURED] mark=0 use=1
tcp 6 8 CLOSE src=192.168.201.110 dst=182.75.23.156 sport=21568 dport=443 src=182.75.23.156 dst=172.30.3.222 sport=443 dport=21568 [ASSURED] mark=0 use=1
tcp 6 39 TIME_WAIT src=192.168.201.109 dst=182.75.23.156 sport=19778 dport=443 src=182.75.23.156 dst=172.30.3.222 sport=443 dport=19778 [ASSURED] mark=0 use=1
tcp 6 85 TIME_WAIT src=192.168.201.109 dst=182.75.23.156 sport=16067 dport=443 src=182.75.23.156 dst=172.30.3.222 sport=443 dport=16067 [ASSURED] mark=0 use=1
tcp 6 58 TIME_WAIT src=192.168.201.111 dst=182.75.23.156 sport=43727 dport=443 src=182.75.23.156 dst=172.30.3.222 sport=443 dport=43727 [ASSURED] mark=0 use=1
tcp 6 54 TIME_WAIT src=192.168.201.109 dst=182.75.23.156 sport=3599 dport=443 src=182.75.23.156 dst=172.30.3.222 sport=443 dport=3599 [ASSURED] mark=0 use=1
tcp 6 0 TIME_WAIT src=192.168.201.109 dst=182.75.23.156 sport=47476 dport=443 src=182.75.23.156 dst=172.30.3.222 sport=443 dport=47476 [ASSURED] mark=0 use=1
tcp 6 55 TIME_WAIT src=192.168.201.109 dst=182.75.23.156 sport=62983 dport=443 src=182.75.23.156 dst=172.30.3.222 sport=443 dport=62983 [ASSURED] mark=0 use=1
tcp 6 119 TIME_WAIT src=192.168.201.109 dst=182.75.23.156 sport=56724 dport=443 src=182.75.23.156 dst=172.30.3.222 sport=443 dport=56724 [ASSURED] mark=0 use=1
root@linux-study:~#
实时显示连接跟踪事件,如新建连接、更新连接或销毁连接:conntrack -E
root@linux-study:~#
root@linux-study:~# conntrack -E|head
[UPDATE] tcp 6 60 SYN_RECV src=192.168.201.109 dst=182.75.23.156 sport=61456 dport=443 src=182.75.23.156 dst=172.30.3.222 sport=443 dport=61456
[UPDATE] tcp 6 432000 ESTABLISHED src=192.168.201.109 dst=182.75.23.156 sport=61456 dport=443 src=182.75.23.156 dst=172.30.3.222 sport=443 dport=61456 [ASSURED]
[UPDATE] tcp 6 120 FIN_WAIT src=192.168.201.109 dst=182.75.23.156 sport=61456 dport=443 src=182.75.23.156 dst=172.30.3.222 sport=443 dport=61456 [ASSURED]
[UPDATE] tcp 6 30 LAST_ACK src=192.168.201.109 dst=182.75.23.156 sport=61456 dport=443 src=182.75.23.156 dst=172.30.3.222 sport=443 dport=61456 [ASSURED]
[UPDATE] tcp 6 10 CLOSE src=192.168.201.109 dst=182.75.23.156 sport=61456 dport=443 src=182.75.23.156 dst=172.30.3.222 sport=443 dport=61456 [ASSURED]
[NEW] tcp 6 120 SYN_SENT src=192.168.201.111 dst=182.75.23.156 sport=64550 dport=443 [UNREPLIED] src=182.75.23.156 dst=172.30.3.222 sport=443 dport=64550
[UPDATE] tcp 6 60 SYN_RECV src=192.168.201.111 dst=182.75.23.156 sport=64550 dport=443 src=182.75.23.156 dst=172.30.3.222 sport=443 dport=64550
[UPDATE] tcp 6 432000 ESTABLISHED src=192.168.201.111 dst=182.75.23.156 sport=64550 dport=443 src=182.75.23.156 dst=172.30.3.222 sport=443 dport=64550 [ASSURED]
[DESTROY] tcp 6 src=192.168.201.109 dst=182.75.23.156 sport=26138 dport=443 src=182.75.23.156 dst=172.30.3.222 sport=443 dport=26138 [ASSURED]
[NEW] tcp 6 120 SYN_SENT src=192.168.201.109 dst=182.75.23.156 sport=1302 dport=443 [UNREPLIED] src=182.75.23.156 dst=172.30.3.222 sport=443 dport=1302
root@linux-study:~#
删除与特定源IP地址相关的所有连接条目:conntrack -D -s 192.168.1.100
删除与特定目的IP地址相关的所有连接条目:conntrack -D -d 192.168.1.100
删除所有源NAT(SNAT)到特定IP的连接:conntrack -D --src-nat 192.168.1.100
删除所有目的NAT(DNAT)到特定IP的连接:conntrack -D --dst-nat 192.168.1.100
当然,让我们在适当的地方加入emoji来让回答更加生动:
服务器突然无法响应,初步怀疑是由于网络连接数过多导致。需要快速诊断是否有大量的网络连接占用了服务器资源。
conntrack -L
命令🔍列出当前所有的网络连接状态,这能帮助我们快速获取当前的连接概览。conntrack -C
命令直接获取当前跟踪的连接总数,这可以更快地帮助我们判断是否连接数过多。conntrack -L | grep "状态" | wc -l
(状态比如ESTABLISHED)来具体分析哪种类型的连接占比较高。conntrack -D --dst <服务器IP>
来删除向特定目标IP地址的所有连接,或者根据需要删除特定状态的连接,如conntrack -D --state ESTABLISHED
来减少活跃连接🚨。服务器遭受疑似DDoS攻击,导致正常服务受到影响。需要快速定位并减轻攻击。
conntrack -L
命令查看当前的网络连接,特别是那些状态为NEW
但未得到回应的连接(即UNREPLIED标记)。conntrack -L | grep [UNREPLIED] | awk '{print $NF}' | sort | uniq -c | sort -nr
来识别发送大量连接请求的源IP。conntrack -D --src <攻击源IP>
来直接从连接跟踪表中删除这些恶意连接⛔。用户报告特定服务响应缓慢。初步怀疑是网络层面的问题,需要确认是否为网络连接问题。
conntrack -L | grep "特定服务的端口"
来查看与服务相关的网络连接状态,从而判断是否有异常的连接状态。SYN_RECV
状态,可能表明SYN Flood攻击,或者服务端口无法及时处理入站连接请求。conntrack -D
命令删除一些长时间未建立的连接,以减轻服务器负担🔧。conntrack
命令需要管理员权限(root)来执行。🛠️conntrack -E
监听连接事件时,输出可能会非常快速和大量,可以考虑将其重定向到文件中或通过管道传递给grep
进行过滤。🔍原创声明:本文系作者授权腾讯云开发者社区发表,未经许可,不得转载。
如有侵权,请联系 cloudcommunity@tencent.com 删除。
原创声明:本文系作者授权腾讯云开发者社区发表,未经许可,不得转载。
如有侵权,请联系 cloudcommunity@tencent.com 删除。