netstat常见用法
netstat是rhel6和rhel7的默认网络工具
rhel8的默认工具是ss,rhel8要安装netstat,需要安装net-tools
netstat常用参数
usage: netstat [-vWeenNcCF] [<Af>] -r netstat {-V|--version|-h|--help}
netstat [-vWnNcaeol] [<Socket> ...]
netstat { [-vWeenNac] -I[<Iface>] | [-veenNac] -i | [-cnNe] -M | -s [-6tuw] } [delay]
-r, --route display routing table
-I, --interfaces=<Iface> display interface table for <Iface>
-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 -tunlp
[rhel8 root ~]# netstat -tunlp
Active Internet connections (only servers)
Proto Recv-Q Send-Q Local Address Foreign Address State PID/Program name
tcp 0 0 0.0.0.0:22 0.0.0.0:* LISTEN 950/sshd
udp 0 0 127.0.0.1:323 0.0.0.0:* 747/chronyd
udp6 0 0 ::1:323 :::* 747/chronyd
# 查看当前建立的连接
netstat -tunp
[rhel8 root ~]# netstat -tunp
Active Internet connections (w/o servers)
Proto Recv-Q Send-Q Local Address Foreign Address State PID/Program name
tcp 0 216 192.168.1.178:22 101.204.28.121:27072 ESTABLISHED 10924/sshd: root [p
tcp 0 0 192.168.1.178:22 101.204.28.121:25041 ESTABLISHED 10692/sshd: root [p
udp 0 0 192.168.1.178:68 192.168.1.253:67 ESTABLISHED 837/NetworkManager
查看tcp和udp端口
-t tcp
u udp
n 不解析
l 监听
p 显示pid
netstat -tunlp|grep 端口号
# 查看22端口
[rhel8 root ~]# netstat -tunlp|grep 22
tcp 0 0 0.0.0.0:22 0.0.0.0:* LISTEN 1010/sshd
列出所有端口(包括监听和未监听的)
# 列出所有tcp端口
netstat -atn
[rhel8 root ~]# netstat -atn
Active Internet connections (servers and established)
Proto Recv-Q Send-Q Local Address Foreign Address State
tcp 0 0 0.0.0.0:22 0.0.0.0:* LISTEN
tcp 0 208 192.168.1.178:22 101.204.28.121:41965 ESTABLISHED
tcp 0 0 192.168.1.178:53202 100.100.30.25:80 ESTABLISHED
# 列出所有udp端口
netstat -aun
[rhel8 root ~]# netstat -aun
Active Internet connections (servers and established)
Proto Recv-Q Send-Q Local Address Foreign Address State
udp 0 0 192.168.1.178:68 192.168.1.253:67 ESTABLISHED
udp 0 0 127.0.0.1:323 0.0.0.0:*
udp6 0 0 ::1:323 :::*
在netstat输出中显示pid和进程名称
netstat -ptn
[rhel8 root ~]# netstat -ptn
Active Internet connections (w/o servers)
Proto Recv-Q Send-Q Local Address Foreign Address State PID/Program name
tcp 0 216 192.168.1.178:22 101.204.28.121:41965 ESTABLISHED 26768/sshd: root [p
tcp 0 0 192.168.1.178:53202 100.100.30.25:80 ESTABLISHED 4751/AliYunDun
找出程序运行的端口
netstat -ap|grep ssh
通过端口查进程id
netstat -anp|grep 22|grep LISTEN|awk '{print $7}'|cut -d/ -f1
# 实例
[rhel8 root ~]# netstat -anp|grep 22|grep LISTEN|awk '{print $7}'|cut -d/ -f1
950
LISTENING
查看核心路由信息
[rhel8 root ~]# netstat -rn
Kernel IP routing table
Destination Gateway Genmask Flags MSS Window irtt Iface
0.0.0.0 192.168.1.253 0.0.0.0 UG 0 0 0 eth0
192.168.1.0 0.0.0.0 255.255.255.0 U 0 0 0 eth0
IP和TCP分析
查看连接某服务端口最多的ip地址
netstat -ntu|grep :22|awk '{print $5}'|cut -d: -f1|awk '{++ip[$1]} END {for(i in ip) print ip[i],"\t",i}'|sort -nr
## 打印样式
1 101.204.28.121
TCP各种状态列表
netstat -nt|grep -e 127.0.0.1 -e 0.0.0.0 -e ::: -v|awk '/^tcp/ {++state[$NF]} END {for(i in state) print i ,"\t",state[i]}'
# 打印结果
ESTABLISHED 1
查看php-cgi进程数
netstat -anpo|grep "php-cgi"|wc -l
篇二
netstat命令是一个监控TCP/IP网络的非常有用的工具,它可以显示路由表、实际的网络连接以及每一个网络接口设备的状态信息。
语法:
netstat [选项]
参数:
-a或--all:显示所有连线中的Socket; -A<网络类型>或--<网络类型>:列出该网络类型连线中的相关地址; -c或--continuous:持续列出网络状态; -C或--cache:显示路由器配置的快取信息; -e或--extend:显示网络其他相关信息; -F或--fib:显示FIB; -g或--groups:显示多重广播功能群组组员名单; -h或--help:在线帮助; -i或--interfaces:显示网络界面信息表单; -l或--listening:显示监控中的服务器的Socket; -M或--masquerade:显示伪装的网络连线; -n或--numeric:直接使用ip地址,而不通过域名服务器; -N或--netlink或--symbolic:显示网络硬件外围设备的符号连接名称; -o或--timers:显示计时器; -p或--programs:显示正在使用Socket的程序识别码和程序名称; -r或--route:显示Routing Table; -s或--statistice:显示网络工作信息统计表; -t或--tcp:显示TCP传输协议的连线状况; -u或--udp:显示UDP传输协议的连线状况; -v或--verbose:显示指令执行过程; -V或--version:显示版本信息; -w或--raw:显示RAW传输协议的连线状况; -x或--unix:此参数的效果和指定"-A unix"参数相同; --ip或--inet:此参数的效果和指定"-A inet"参数相同。
使用实例:
实例1:列出所有端口
命令:
netstat -a # 列出所有端口
netstat -at # 列出所有TCP端口
netstat -au # 列出所有UDP端口
netstat -ax # 列出所有unix端口
netstat -atnlp # 直接使用ip地址列出所有处理监听状态的TCP端口,且加上程序名
输出:
说明:
下面分析每一项的含义
Proto
协议名(tcp协议还是udp协议);
recv-Q
网络接收队列
表示收到的数据已经在本地接收缓冲,但是还有多少没有被进程取走,recv()如果接收队列Recv-Q一直处于阻塞状态,可能是遭受了拒绝服务 denial-of-service 攻击;
send-Q
网路发送队列
对方没有收到的数据或者说没有Ack的,还是本地缓冲区.
如果发送队列Send-Q不能很快的清零,可能是有应用向外发送数据包过快,或者是对方接收数据包不够快;
这两个值通常应该为0,如果不为0可能是有问题的。packets在两个队列里都不应该有堆积状态。可接受短暂的非0情况。
Local Address
- Local Address 部分的0.0.0.0:873表示监听服务器上所有ip地址的所有(0.0.0.0表示本地所有ip),比如你的服务器是有172.172.230.210和172.172.230.11两个ip地址,那么0.0.0.0:873此时表示监听172.172.230.210,172.172.230.211,127.0.0.1三个地址的873端口
- 127.0.0.1:25这个表示监听本机的loopback地址的25端口(如果某个服务只监听了回环地址,那么只能在本机进行访问,无法通过tcp/ip 协议进行远程访问)
- 192.168.1.81:2288这是因为我们在启动的时候指定了192.168.1.81:2288参数,如果不指定的话,会监听0.0.0.0:2288
Foreign Address解释
与本机端口通信的外部socket。显示规则与Local Address相同
State解释
链路状态,共有11种
state列共有12中可能的状态,前面11种是按照TCP连接建立的三次握手和TCP连接断开的四次挥手过程来描述的。
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的连接中断请求,或先前的连接中断请求的确认 */
CLOSE_WAIT:被动关闭(passiveclose)端TCP接到FIN后,就发出ACK以回应FIN请求(它的接收也作为文件结束符传递给上层应用程序),并进入CLOSE_WAIT./* The remote end has shut down, waiting for the socketto close. 等待从本地用户发来的连接中断请求 */
FIN_WAIT2:主动关闭端接到ACK后,就进入了FIN-WAIT-2./* Connection is closed, and the socket is waiting for a shutdownfrom the remote end. 从远程TCP等待连接中断请求 */
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. */
备注
SYN: (同步序列编号,SynchronizeSequence Numbers)该标志仅在三次握手建立TCP连接时有效。表示一个新的TCP连接请求。
ACK: (确认编号,AcknowledgementNumber)是对TCP请求的确认标志,同时提示对端系统已经成功接收所有数据。
FIN: (结束标志,FINish)用来结束一个TCP回话.但对应端口仍处于开放状态,准备接收后续数据。
实例2:显示每个协议的统计信息
命令:
netstat -s # 显示所有端口的统计信息
netstat -st # 显示所有TCP的统计信息
netstat -su # 显示所有UDP的统计信息
实例3:显示核心路由信息
命令:
netstat -r # 显示所有端口的统计信息
netstat -rn # 显示所有TCP的统计信息
输出:
说明:
Destination:目标网络或者主机。
Gateway:网关地址,如果没有设置则为*。
Genmask:目标网络掩码;如果默认路由则用"0.0.0.0"。
Flags标志说明:
U Up表示此路由当前为启动状态
H Host,表示此网关为一主机
G Gateway,表示此网关为一路由器
R Reinstate Route,使用动态路由重新初始化的路由
D Dynamically,此路由是动态性地写入
M Modified,此路由是由路由守护程序或导向器动态修改
! 表示此路由当前为关闭状态
Iface:对于这个路由,数据包将要发送到那个接口(网卡)。
篇三
以下摘自man netstat
NETSTAT(8) Linux System Administrator's Manual NETSTAT(8)
NAME
netstat - Print network connections, routing tables, interface statistics, masquerade connections, and multicast memberships
SYNOPSIS
netstat [address_family_options] [--tcp|-t] [--udp|-u] [--udplite|-U] [--sctp|-S] [--raw|-w] [--l2cap|-2] [--rfcomm|-f] [--listening|-l]
[--all|-a] [--numeric|-n] [--numeric-hosts] [--numeric-ports] [--numeric-users] [--symbolic|-N] [--extend|-e[--extend|-e]] [--timers|-o] [--pro‐
gram|-p] [--verbose|-v] [--continuous|-c] [--wide|-W] [delay]
netstat {--route|-r} [address_family_options] [--extend|-e[--extend|-e]] [--verbose|-v] [--numeric|-n] [--numeric-hosts] [--numeric-ports]
[--numeric-users] [--continuous|-c] [delay]
netstat {--interfaces|-I|-i} [--all|-a] [--extend|-e] [--verbose|-v] [--program|-p] [--numeric|-n] [--numeric-hosts] [--numeric-ports] [--numeric-
users] [--continuous|-c] [delay]
netstat {--groups|-g} [--numeric|-n] [--numeric-hosts] [--numeric-ports] [--numeric-users] [--continuous|-c] [delay]
netstat {--masquerade|-M} [--extend|-e] [--numeric|-n] [--numeric-hosts] [--numeric-ports] [--numeric-users] [--continuous|-c] [delay]
netstat {--statistics|-s} [--tcp|-t] [--udp|-u] [--udplite|-U] [--sctp|-S] [--raw|-w] [delay]
netstat {--version|-V}
netstat {--help|-h}
address_family_options:
[-4|--inet] [-6|--inet6] [--protocol={inet,inet6,unix,ipx,ax25,netrom,ddp,bluetooth, ... } ] [--unix|-x] [--inet|--ip|--tcpip] [--ax25] [--x25]
[--rose] [--ash] [--bluetooth] [--ipx] [--netrom] [--ddp|--appletalk] [--econet|--ec]
NOTES
This program is mostly obsolete. Replacement for netstat is ss. Replacement for netstat -r is ip route. Replacement for netstat -i is ip -s
link. Replacement for netstat -g is ip maddr.
DESCRIPTION
Netstat prints information about the Linux networking subsystem. The type of information printed is controlled by the first argument, as follows:
(none)
By default, netstat displays a list of open sockets. If you don't specify any address families, then the active sockets of all configured address
families will be printed.
--route, -r
Display the kernel routing tables. See the description in route(8) for details. netstat -r and route -e produce the same output.
--groups, -g
Display multicast group membership information for IPv4 and IPv6.
--interfaces=iface , -I=iface , -i
Display a table of all network interfaces, or the specified iface.
--masquerade, -M
Display a list of masqueraded connections.
--statistics, -s
Display summary statistics for each protocol.
OPTIONS
--verbose, -v
Tell the user what is going on by being verbose. Especially print some useful information about unconfigured address families.
--wide, -W
Do not truncate IP addresses by using output as wide as needed. This is optional for now to not break existing scripts.
--numeric, -n
Show numerical addresses instead of trying to determine symbolic host, port or user names.
--numeric-hosts
shows numerical host addresses but does not affect the resolution of port or user names.
--numeric-ports
shows numerical port numbers but does not affect the resolution of host or user names.
--numeric-users
shows numerical user IDs but does not affect the resolution of host or port names.
--protocol=family, -A
Specifies the address families (perhaps better described as low level protocols) for which connections are to be shown. family is a comma (',')
separated list of address family keywords like inet, inet6, unix, ipx, ax25, netrom, econet, ddp, and bluetooth. This has the same effect as
using the --inet|-4, --inet6|-6, --unix|-x, --ipx, --ax25, --netrom, --ddp, and --bluetooth options.
The address family inet (Iv4) includes raw, udp, udplite and tcp protocol sockets.
The address family bluetooth (Iv4) includes l2cap and rfcomm protocol sockets.
-c, --continuous
This will cause netstat to print the selected information every second continuously.
-e, --extend
Display additional information. Use this option twice for maximum detail.
-o, --timers
Include information related to networking timers.
-p, --program
Show the PID and name of the program to which each socket belongs.
-l, --listening
Show only listening sockets. (These are omitted by default.)
-a, --all
Show both listening and non-listening (for TCP this means established connections) sockets. With the --interfaces option, show interfaces that
are not up
-F
Print routing information from the FIB. (This is the default.)
-C
Print routing information from the route cache.
delay
Netstat will cycle printing through statistics every delay seconds.
OUTPUT
Active Internet connections (TCP, UDP, UDPLite, raw)
Proto
The protocol (tcp, udp, udpl, raw) used by the socket.
Recv-Q
Established: The count of bytes not copied by the user program connected to this socket. Listening: Since Kernel 2.6.18 this column contains the
current syn backlog.
Send-Q
Established: The count of bytes not acknowledged by the remote host. Listening: Since Kernel 2.6.18 this column contains the maximum size of the
syn backlog.
Local Address
Address and port number of the local end of the socket. Unless the --numeric (-n) option is specified, the socket address is resolved to its
canonical host name (FQDN), and the port number is translated into the corresponding service name.
Foreign Address
Address and port number of the remote end of the socket. Analogous to "Local Address".
State
The state of the socket. Since there are no states in raw mode and usually no states used in UDP and UDPLite, this column may be left blank. Nor‐
mally this can be one of several values:
ESTABLISHED
The socket has an established connection.
SYN_SENT
The socket is actively attempting to establish a connection.
SYN_RECV
A connection request has been received from the network.
FIN_WAIT1
The socket is closed, and the connection is shutting down.
FIN_WAIT2
Connection is closed, and the socket is waiting for a shutdown from the remote end.
TIME_WAIT
The socket is waiting after close to handle packets still in the network.
CLOSE The socket is not being used.
CLOSE_WAIT
The remote end has shut down, waiting for the socket to close.
LAST_ACK
The remote end has shut down, and the socket is closed. Waiting for acknowledgement.
LISTEN The socket is listening for incoming connections. Such sockets are not included in the output unless you specify the --listening (-l) or
--all (-a) option.
CLOSING
Both sockets are shut down but we still don't have all our data sent.
UNKNOWN
The state of the socket is unknown.
User
The username or the user id (UID) of the owner of the socket.
PID/Program name
Slash-separated pair of the process id (PID) and process name of the process that owns the socket. --program causes this column to be included.
You will also need superuser privileges to see this information on sockets you don't own. This identification information is not yet available
for IPX sockets.
Timer
TCP timer associated with this socket. The format is timer(a/b/c). The timer is one of the following values:
off There is no timer set for this socket.
on The retransmission timer is active for the socket.
keepalive
The keepalive timer is active for the socket.
timewait
The connection is closing and the timewait timer is active for the socket.
The values in the brackets:
a Timer value.
b Number of retransmissions sent.
c Number of keepalives sent.
Active UNIX domain Sockets
Proto
The protocol (usually unix) used by the socket.
RefCnt
The reference count (i.e. attached processes via this socket).
Flags
The flags displayed is SO_ACCEPTON (displayed as ACC), SO_WAITDATA (W) or SO_NOSPACE (N). SO_ACCECPTON is used on unconnected sockets if their
corresponding processes are waiting for a connect request. The other flags are not of normal interest.
Type
There are several types of socket access:
SOCK_DGRAM
The socket is used in Datagram (connectionless) mode.
SOCK_STREAM
This is a stream (connection) socket.
SOCK_RAW
The socket is used as a raw socket.
SOCK_RDM
This one serves reliably-delivered messages.
SOCK_SEQPACKET
This is a sequential packet socket.
SOCK_PACKET
Raw interface access socket.
UNKNOWN
Who ever knows what the future will bring us - just fill in here :-)
State
This field will contain one of the following Keywords:
FREE The socket is not allocated
LISTENING
The socket is listening for a connection request. Such sockets are only included in the output if you specify the --listening (-l) or
--all (-a) option.
CONNECTING
The socket is about to establish a connection.
CONNECTED
The socket is connected.
DISCONNECTING
The socket is disconnecting.
(empty)
The socket is not connected to another one.
UNKNOWN
This state should never happen.
PID/Program name
Process ID (PID) and process name of the process that has the socket open. More info available in Active Internet connections section written
above.
Path
This is the path name as which the corresponding processes attached to the socket.
Active IPX sockets
(this needs to be done by somebody who knows it)
Active NET/ROM sockets
(this needs to be done by somebody who knows it)
Active AX.25 sockets
(this needs to be done by somebody who knows it)
FILES
/etc/services -- The services translation file
/proc -- Mount point for the proc filesystem, which gives access to kernel status information via the following files.
/proc/net/dev -- device information
/proc/net/raw -- raw socket information
/proc/net/tcp -- TCP socket information
/proc/net/udp -- UDP socket information
/proc/net/udplite -- UDPLite socket information
/proc/net/igmp -- IGMP multicast information
/proc/net/unix -- Unix domain socket information
/proc/net/ipx -- IPX socket information
/proc/net/ax25 -- AX25 socket information
/proc/net/appletalk -- DDP (appletalk) socket information
/proc/net/nr -- NET/ROM socket information
/proc/net/route -- IP routing information
/proc/net/ax25_route -- AX25 routing information
/proc/net/ipx_route -- IPX routing information
/proc/net/nr_nodes -- NET/ROM nodelist
/proc/net/nr_neigh -- NET/ROM neighbours
/proc/net/ip_masquerade -- masqueraded connections
/sys/kernel/debug/bluetooth/l2cap -- Bluetooth L2CAP information
/sys/kernel/debug/bluetooth/rfcomm -- Bluetooth serial connections
/proc/net/snmp -- statistics
SEE ALSO
route(8), ifconfig(8), iptables(8), proc(5) ss(8) ip(8)
BUGS
Occasionally strange information may appear if a socket changes as it is viewed. This is unlikely to occur.
AUTHORS
The netstat user interface was written by Fred Baumgarten <dc6iq@insu1.etec.uni-karlsruhe.de>, the man page basically by Matt Welsh <mdw@tc.cor‐
nell.edu>. It was updated by Alan Cox <Alan.Cox@linux.org>, updated again by Tuan Hoang <tqhoang@bigfoot.com>. The man page and the command
included in the net-tools package is totally rewritten by Bernd Eckenfels <ecki@linux.de>. UDPLite options were added by Brian Micek
<bmicek@gmail.com>
net-tools 2014-10-07 NETSTAT(8)