<?xml version="1.0" encoding="UTF-8"?><rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>Linux &#8211; 知识箱</title>
	<atom:link href="https://www.kmsbox.com/tag/linux/feed" rel="self" type="application/rss+xml" />
	<link>https://www.kmsbox.com</link>
	<description>IT知识收集箱</description>
	<lastBuildDate>Fri, 27 Dec 2024 04:32:12 +0000</lastBuildDate>
	<language>zh-Hans</language>
	<sy:updatePeriod>
	hourly	</sy:updatePeriod>
	<sy:updateFrequency>
	1	</sy:updateFrequency>
	<generator>https://wordpress.org/?v=6.9</generator>
	<item>
		<title>netstat命令 查看TCP连接数信息_</title>
		<link>https://www.kmsbox.com/os/linux/155.html</link>
		
		<dc:creator><![CDATA[歡哥]]></dc:creator>
		<pubDate>Fri, 02 Sep 2022 09:39:30 +0000</pubDate>
				<category><![CDATA[Linux]]></category>
		<guid isPermaLink="false">https://www.kmsbox.com/?p=155</guid>

					<description><![CDATA[来源 网络 侵删 netstat命令的功能是显示网络连接、路由表和网络接口的信息，可以让用户得知有哪些网络连接 [&#8230;]]]></description>
										<content:encoded><![CDATA[
<blockquote class="wp-block-quote is-layout-flow wp-block-quote-is-layout-flow">
<p>来源 网络 侵删</p>
</blockquote>



<p>netstat命令的功能是显示网络连接、路由表和网络接口的信息，可以让用户得知有哪些网络连接正在运作。在日常工作中，我们最常用的也就两个参数，即netstat –an，如下所示：</p>



<figure class="wp-block-image size-large"><img decoding="async" src="https://oss.kmsbox.com/images/2022/09/1662111392-image.png" alt=""/></figure>



<p>etstat &nbsp;-an参数中stat（状态）的含义如下：</p>



<p><br><strong>LISTEN</strong>：侦听来自远方的TCP端口的连接请求；<br><strong>SYN-SENT</strong>：在发送连接请求后等待匹配的连接请求；<br><strong>SYN-RECEIVED</strong>：在收到和发送一个连接请求后等待对方对连接请求的确认；<br><strong>ESTABLISHED</strong>：代表一个打开的连接，我们常用此作为并发连接数；<br><strong>FIN-WAIT-1</strong>：等待远程TCP连接中断请求，或先前的连接中断请求的确认；<br><strong>FIN-WAIT-2</strong>：从远程TCP等待连接中断请求；<br><strong>CLOSE-WAIT</strong>：等待从本地用户发来的连接中断请求；<br><strong>CLOSING</strong>：等待远程TCP对连接中断的确认；<br><strong>LAST-ACK</strong>：等待原来发向远程TCP的连接中断的确认；<br><strong>TIME-WAIT</strong>：等待足够的时间以确保远程TCP连接收到中断请求的确认；<br><strong>CLOSED</strong>：没有任何连接状态；</p>



<p>二、在日常工作中，我们可以用shell组合命令来查看服务器的TCP连接状态并汇总，命令如下：</p>



<pre class="wp-block-code"><code>netstat -an|awk '/^tcp/{++S&#91;$NF]}END{for (a in S)print a,S&#91;a]}'  </code></pre>



<p>参数说明：<br><strong>CLOSED</strong>：没有连接活动或正在进行的；<br><strong>LISTEN</strong>：服务器正在等待的进入呼叫；<br><strong>SYN_RECV</strong>：一个连接请求已经到达，等待确认；<br><strong>SYN_SENT</strong>：应用已经开始，打开一个连接；<br><strong>ESTABLISHED</strong>：正常数据传输状态，也可以近似的理解为当前服务器的并发数；<br><strong>FIN_WAIT1</strong>：应用已经完成；<br><strong>FIN_WAIT2</strong>：另一边同意释放；<br><strong>ITMED_WAIT</strong>：等待所有分组死掉；<br><strong>CLOSING</strong>：两边同时尝试关闭；<br><strong>TIME_WAIT</strong>：另一边已初始化一个释放；<br><strong>LAST_ACK</strong>：等待所有分组死掉；</p>



<p>三、查看某个端口的连接数：</p>



<p>比如查看 18180 端口</p>



<p>方法1，可以显示详细的端口连接信息：</p>



<p>lsof -i:18180</p>



<p>方法2，直接显示连接的数量：</p>



<p><code>netstat -an |grep 'ESTABLISHED' |grep -i '18180' |wc -l</code></p>



<p>四、统计 TCP连接数 命令：</p>



<p><code>netstat&nbsp;-an&nbsp;|grep&nbsp;'ESTABLISHED'&nbsp;|grep&nbsp;'tcp'&nbsp;|wc&nbsp;-l &nbsp;</code></p>
]]></content:encoded>
					
		
		
			</item>
	</channel>
</rss>
