天天看點

用netstat檢視網絡狀态詳解

一、Linux伺服器上11種網絡連接配接狀态: 

用netstat檢視網絡狀态詳解

                                          圖:TCP的狀态機

通常情況下:一個正常的TCP連接配接,都會有三個階段:1、TCP三向交握;2、資料傳送;3、TCP四次揮手

注:以下說明最好能結合”圖:TCP的狀态機”來了解。

SYN: (同步序列編号,Synchronize Sequence Numbers)該标志僅在三次握手建立TCP連接配接時有效。表示一個新的TCP連接配接請求。

ACK: (确認編号,Acknowledgement Number)是對TCP請求的确認标志,同時提示對端系統已經成功接收所有資料。

FIN: (結束标志,FINish)用來結束一個TCP回話.但對應端口仍處于開放狀态,準備接收後續資料。

1)、LISTEN:首先服務端需要打開一個socket進行監聽,狀态為LISTEN. /* The socket is listening for incoming connections. 偵聽來自遠方TCP端口的連接配接請求 */ 

2)、SYN_SENT:用戶端通過應用程式調用connect進行active open.于是用戶端tcp發送一個SYN以請求建立一個連接配接.之後狀态置為SYN_SENT. /*The socket is actively attempting to establish a connection. 在發送連接配接請求後等待比對的連接配接請求 */

3)、SYN_RECV:服務端應發出ACK确認用戶端的SYN,同時自己向用戶端發送一個SYN. 之後狀态置為SYN_RECV /* A connection request has been received from the network. 在收到和發送一個連接配接請求後等待對連接配接請求的确認 */

4)、ESTABLISHED: 代表一個打開的連接配接,雙方可以進行或已經在資料互動了。/* The socket has an established connection. 代表一個打開的連接配接,資料可以傳送給使用者 */

5)、FIN_WAIT1:主動關閉(active close)端應用程式調用close,于是其TCP發出FIN請求主動關閉連接配接,之後進入FIN_WAIT1狀态./* The socket is closed, and the connection is shutting down. 等待遠端TCP的連接配接中斷請求,或先前的連接配接中斷請求的确認 */

6)、CLOSE_WAIT:被動關閉(passive close)端TCP接到FIN後,就發出ACK以回應FIN請求(它的接收也作為檔案結束符傳遞給上層應用程式),并進入CLOSE_WAIT. /* The remote end has shut down, waiting for the socket to close. 等待從本地使用者發來的連接配接中斷請求 */

7)、FIN_WAIT2:主動關閉端接到ACK後,就進入了FIN-WAIT-2 ./* Connection is closed, and the socket is waiting for a shutdown from the remote end. 從遠端TCP等待連接配接中斷請求 */

8)、LAST_ACK:被動關閉端一段時間後,接收到檔案結束符的應用程式将調用CLOSE關閉連接配接。這導緻它的TCP也發送一個 FIN,等待對方的ACK.就進入了LAST-ACK . /* The remote end has shut down, and the socket is closed. Waiting for acknowledgement. 等待原來發向遠端TCP的連接配接中斷請求的确認 */

9)、TIME_WAIT:在主動關閉端接收到FIN後,TCP就發送ACK包,并進入TIME-WAIT狀态。/* The socket is waiting after close to handle packets still in the network.等待足夠的時間以確定遠端TCP接收到連接配接中斷請求的确認 */

10)、CLOSING: 比較少見./* Both sockets are shut down but we still don’t have all our data sent. 等待遠端TCP對連接配接中斷的确認 */

11)、CLOSED: 被動關閉端在接受到ACK包後,就進入了closed的狀态。連接配接結束./* The socket is not being used. 沒有任何連接配接狀态 */ 

TIME_WAIT狀态的形成隻發生在主動關閉連接配接的一方。 

主動關閉方在接收到被動關閉方的FIN請求後,發送成功給對方一個ACK後,将自己的狀态由FIN_WAIT2修改為TIME_WAIT,而必須再等2倍 的MSL(Maximum Segment Lifetime,MSL是一個資料報在internetwork中能存在的時間)時間之後雙方才能把狀态 都改為CLOSED以關閉連接配接。目前RHEL裡保持TIME_WAIT狀态的時間為60秒。

當然上述很多TCP狀态在系統裡都有對應的解釋或設定,可見man tcp

二、關于長連接配接和短連接配接:

通俗點講:短連接配接就是一次TCP請求得到結果後,連接配接馬上結束.而長連接配接并不馬上斷開,而一直保持着,直到長連接配接TIMEOUT(具體程式都有相關參數說明).長連接配接可以避免不斷的進行TCP三向交握和四次揮手. 

長連接配接(keepalive)是需要靠雙方不斷的發送探測包來維持的,keepalive期間服務端和用戶端的TCP連接配接狀态是ESTABLISHED.目前http 1.1版本裡預設都是keepalive(1.0版本預設是不keepalive的),ie6/7/8和firefox都預設用的是http 1.1版本了(如何檢視目前浏覽器用的是哪個版本,這裡不再贅述)。Apache,java

一個應用至于到底是該使用短連接配接還是長連接配接,應該視具體情況而定。一般的應用應該使用長連接配接。

1、Linux的相關keepalive參數

a、 tcp_keepalive_time – INTEGER 

How often TCP sends out keepalive messages when keepalive is enabled. 

Default: 2hours. 

b、 tcp_keepalive_probes – INTEGER 

How many keepalive probes TCP sends out, until it decides that the 

connection is broken. Default value: 9. 

c、 tcp_keepalive_intvl – INTEGER 

How frequently the probes are send out. Multiplied by 

tcp_keepalive_probes it is time to kill not responding connection, 

after probes started. Default value: 75sec i.e. connection 

will be aborted after ~11 minutes of retries.

2、F5負載均衡上的相關參數說明

a、Keep Alive Interval 

Specifies, when enabled, how frequently the system sends data over an idle TCP connection, to determine whether the connection is still valid. 

Specify: Specifies the interval at which the system sends data over an idle connection, to determine whether the connection is still valid. The defaultis 1800 milliseconds. 

b、Time Wait 

Specifies the length of time that a TCP connection remains in the TIME-WAIT state before entering the CLOSED state. 

Specify: Specifies the number of milliseconds that a TCP connection can remain in the TIME-WAIT state. The default is 2000.

c、Idle Timeout 

Specifies the length of time that a connection is idle (has no traffic) before the connection is eligible for deletion. 

Specify: Specifies a number of seconds that the TCP connection can remain idle before the system deletes it. The default is 300 seconds.

3、Apache的相關參數說明 

以下是Apache/2.0.61版本的預設參數和說明

a、KeepAlive: 

default On.Whether or not to allow persistent connections (more than 

one request per connection). Set to “Off” to deactivate. 

b、MaxKeepAliveRequests: 

default 100.The maximum number of requests to allow 

during a persistent connection. Set to 0 to allow an unlimited amount. 

We recommend you leave this number high, for maximum performance. 

c、KeepAliveTimeout: 

default 15. Number of seconds to wait for the next request from the 

same client on the same connection.