netstat命令是一个监控TCP/IP网络的非常有用的工具,它可以显示路由表、实际的网络连接以及每一个网络接口设备的状态信息,以及与IP、TCP、UDP和ICMP协议相关的统计数据,一般用于检验本机各端口的网络连接情况,用于分析网络问题,非常方便。
#netstat -h usage: netstat [-vWeenNcCF] [<Af>] -r netstat {-V|--version|-h|--help} netstat [-vWnNcaeol] [<Socket> ...] netstat { [-vWeenNac] -i | [-cnNe] -M | -s [-6tuw] } -r, --route display routing table -i, --interfaces display interface table -g, --groups display multicast group memberships -s, --statistics display networking statistics (like SNMP) -M, --masquerade display masqueraded connections -v, --verbose be verbose -W, --wide don't truncate IP addresses -n, --numeric don't resolve names --numeric-hosts don't resolve host names --numeric-ports don't resolve port names --numeric-users don't resolve user names -N, --symbolic resolve hardware names -e, --extend display other/more information -p, --programs display PID/Program name for sockets -o, --timers display timers -c, --continuous continuous listing -l, --listening display listening server sockets -a, --all display all sockets (default: connected) -F, --fib display Forwarding Information Base (default) -C, --cache display routing cache instead of FIB -Z, --context display SELinux security context for sockets <Socket>={-t|--tcp} {-u|--udp} {-U|--udplite} {-S|--sctp} {-w|--raw} {-x|--unix} --ax25 --ipx --netrom <AF>=Use '-6|-4' or '-A <af>' or '--<af>'; default: inet List of possible address families (which support routing): inet (DARPA Internet) inet6 (IPv6) ax25 (AMPR AX.25) netrom (AMPR NET/ROM) ipx (Novell IPX) ddp (Appletalk DDP) x25 (CCITT X.25)
netstat -anpt
-a 显示所有连线中的Socket
-n 直接使用ip地址,而不通过域名服务器
-p 示正在使用Socket的程序识别码和程序名称
-t 显示TCP传输协议的连线状况
#netstat -anpt (Not all processes could be identified, non-owned process info will not be shown, you would have to be root to see it all.) Active Internet connections (servers and established) Proto Recv-Q Send-Q Local Address Foreign Address State PID/Program name tcp 0 0 0.0.0.0:10050 0.0.0.0:* LISTEN - tcp 0 0 0.0.0.0:139 0.0.0.0:* LISTEN - tcp 0 0 127.0.0.53:53 0.0.0.0:* LISTEN - tcp 0 0 0.0.0.0:21 0.0.0.0:* LISTEN - tcp 0 0 0.0.0.0:22 0.0.0.0:* LISTEN - tcp 0 0 127.0.0.1:6010 0.0.0.0:* LISTEN - tcp 0 0 127.0.0.1:6011 0.0.0.0:* LISTEN - tcp 0 0 127.0.0.1:6012 0.0.0.0:* LISTEN - tcp 0 0 0.0.0.0:445 0.0.0.0:* LISTEN -
显示结果解析
Proto:协议名(tcp协议还是udp协议)
Recv-Q:网络接收队列
Send-Q:网路发送队列
Local Address:本地IP地址和端口
Foreign Address:对端IP地址和端口
State:状态
PID/Program name:进程和进程名
通过Recv-Q和Send-Q可以分析网络包的接收发送情况,正常情况应该都是零。
配合grep命令可以查看对应的进程的网络情况,如netstat -anpt | grep test。
state的种类和描述摘抄如下。
LISTEN :首先服务端需要打开一个socket进行监听,状态为LISTEN./*The socket is listening for incoming connections. 侦听来自远方TCP端口的连接请求 */
SYN_SENT:客户端通过应用程序调用connect进行activeopen.于是客户端tcp发送一个SYN以请求建立一个连接.之后状态SYN_SENT。/*The socket is actively attempting to establish aconnection. 在发送连接请求后等待匹配的连接请求 */
SYN_RECV:服务端应发出ACK确认客户端的 SYN,同时自己向客户端发送一个SYN.之后状态置为SYN_RECV/* A connection request has been received from the network. 在收到和发送一个连接请求后等待对连接请求的确认 */
ESTABLISHED:代表一个打开的连接,双方可以进行或已经在数据交互了。/* The socket has an established connection. 代表一个打开的连接,数据可以传送给用户 */
FIN_WAIT1:主动关闭(activeclose)端应用程序调用close,于是其TCP发出FIN请求主动关闭连接,之后进入FIN_WAIT1状态./* The socket is closed, and the connection is shutting down. 等待远程TCP的连接中断请求,或先前的连接中断请求的确认 */
FIN_WAIT2:主动关闭端接到ACK后,就进入了FIN-WAIT-2./* Connection is closed, and the socket is waiting for a shutdownfrom the remote end. 从远程TCP等待连接中断请求 */
CLOSE_WAIT:被动关闭(passiveclose)端TCP接到FIN后,就发出ACK以回应FIN请求(它的接收也作为文件结束符传递给上层应用程序),并进入CLOSE_WAIT./* The remote end has shut down, waiting for the socketto close. 等待从本地用户发来的连接中断请求 */
LAST_ACK:被动关闭端一段时间后,接收到文件结束符的应用程 序将调用CLOSE关闭连接。这导致它的TCP也发送一个 FIN,等待对方的ACK.就进入了LAST-ACK./* The remote end has shut down, and the socket is closed. Waiting foracknowledgement. 等待原来发向远程TCP的连接中断请求的确认 */
TIME_WAIT:在主动关闭端接收到FIN后,TCP 就发送ACK包,并进入TIME-WAIT状态。/* Thesocket is waiting after close to handle packets still in the network.等待足够的时间以确保远程TCP接收到连接中断请求的确认*/
CLOSING:比较少见./* Bothsockets are shut down but we still don’t have all our datasent. 等待远程TCP对连接中断的确认 */
CLOSED:被动关闭端在接受到ACK包后,就进入了closed的状态。连接结束./*The socket is not being used. 没有任何连接状态 */
UNKNOWN:未知的Socket状态。/* Thestate of the socket is unknown. */