本文为joshua317原创文章,转载请注明:转载自joshua317博客 https://www.joshua317.com/article/203
一天一个 Linux 命令(43):netstat 命令
一、简介
Linux下的netstat命令用于显示各种网络相关信息,如网络连接,路由表,接口状态 (Interface Statistics),masquerade 连接,多播成员 (Multicast Memberships) 等等。netstat是在内核中访问网络及相关信息的程序,它能提供TCP连接,TCP和UDP监听,进程内存管理的相关报告。
从整体上看,netstat的输出结果可以分为两个部分:一个是Active Internet connections,称为有源TCP连接,其中”Recv-Q”和”Send-Q”指的是接收队列和发送队列。这些数字一般都应该是0。如果不是则表示软件包正在队列中堆积。这种情况只能在非常少的情况见到;另一个是Active UNIX domain sockets,称为有源Unix域套接口(和网络套接字一样,但是只能用于本机通信,性能可以提高一倍)。
二、格式说明
netstat [-acCeFghilMnNoprstuvVwx][-A<网络类型>][--ip]
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)
三、选项说明
-a或--all 显示所有连线中的Socket。
-A<网络类型>或--<网络类型> 列出该网络类型连线中的相关地址。
-c或--continuous 持续列出网络状态。
-C或--cache 显示路由器配置的快取信息。
-e或--extend 显示网络其他相关信息。
-F或--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或--statistics 显示网络工作信息统计表。
-t或--tcp 显示TCP传输协议的连线状况。
-u或--udp 显示UDP传输协议的连线状况。
-v或--verbose 显示指令执行过程。
-V或--version 显示版本信息。
-w或--raw 显示RAW传输协议的连线状况。
-x或--unix 此参数的效果和指定"-A unix"参数相同。
--ip或--inet 此参数的效果和指定"-A inet"参数相同。
四、命令功能
利用 netstat 指令可让你得知整个 Linux 系统的网络情况。
五、常见用法
5.1 显示详细的网络
# netstat -a
netstat的输出结果可以分为两个部分:
一个是Active Internet connections,称为有源TCP连接,其中"Recv-Q"和"Send-Q"指的是接收队列和发送队列。这些数字一般都应该是0。如果不是则表示软件包正在队列中堆积。这种情况只能在非常少的情况见到。
另一个是Active UNIX domain sockets,称为有源Unix域套接口(和网络套接字一样,但是只能用于本机通信,性能可以提高一倍)。
Proto显示连接使用的协议,RefCnt表示连接到本套接口上的进程号,Types显示套接口的类型,State显示套接口当前的状态,Path表示连接到套接口的其它进程使用的路径名。
套接口类型:
-t :TCP
-u :UDP
-raw :RAW类型
--unix :UNIX域类型
--ax25 :AX25类型
--ipx :ipx类型
--netrom :netrom类型
状态说明:
LISTEN:侦听来自远方的TCP端口的连接请求
SYN-SENT:再发送连接请求后等待匹配的连接请求(如果有大量这样的状态包,检查是否中招了)
SYN-RECEIVED:再收到和发送一个连接请求后等待对方对连接请求的确认(如有大量此状态,估计被flood攻击了)
ESTABLISHED:代表一个打开的连接
FIN-WAIT-1:等待远程TCP连接中断请求,或先前的连接中断请求的确认
FIN-WAIT-2:从远程TCP等待连接中断请求
CLOSE-WAIT:等待从本地用户发来的连接中断请求
CLOSING:等待远程TCP对连接中断的确认
LAST-ACK:等待原来的发向远程TCP的连接中断请求的确认(不是什么好东西,此项出现,检查是否被攻击)
TIME-WAIT:等待足够的时间以确保远程TCP接收到连接中断请求的确认
CLOSED:没有任何连接状态
5.2 显示当前UDP连接
# netstat -nu
5.3 显示当前TCP连接
netstat -nt
5.3 显示UDP端口号的使用
# netstat -apu
Active Internet connections (servers and established)
Proto Recv-Q Send-Q Local Address Foreign Address State PID/Program name
udp 0 0 0.0.0.0:bootpc 0.0.0.0:* 933/dhclient
udp 0 0 service-01:ntp 0.0.0.0:* 663/ntpd
udp 0 0 VM-0-15-centos:ntp 0.0.0.0:* 663/ntpd
udp6 0 0 service-01:ntp [::]:* 663/ntpd
udp6 0 0 VM-0-15-centos:ntp [::]:* 663/ntpd
5.4 显示网卡列表
# netstat -i
Kernel Interface table
Iface MTU RX-OK RX-ERR RX-DRP RX-OVR TX-OK TX-ERR TX-DRP TX-OVR Flg
eth0 1500 125094783 0 0 0 135609023 0 0 0 BMRU
lo 65536 21298782 0 0 0 21298782 0 0 0 LRU
5.5 显示组播组的关系
# netstat -g
IPv6/IPv4 Group Memberships
Interface RefCnt Group
--------------- ------ ---------------------
lo 1 all-systems.mcast.net
eth0 1 all-systems.mcast.net
lo 1 ff02::1
lo 1 ff01::1
eth0 1 ff02::1:ff3a:5f30
eth0 1 ff02::1
eth0 1 ff01::1
5.6 显示网络统计信息
# netstat -s
Ip:
141486693 total packets received
0 forwarded
0 incoming packets discarded
141486593 incoming packets delivered
154098495 requests sent out
48 dropped because of missing route
82 reassemblies required
14 packets reassembled ok
14 fragments received ok
82 fragments created
Icmp:
13813314 ICMP messages received
425 input ICMP message failed.
InCsumErrors: 1
ICMP input histogram:
destination unreachable: 3239
timeout in transit: 432
echo requests: 13809376
echo replies: 175
timestamp request: 91
13809624 ICMP messages sent
0 ICMP messages failed
ICMP output histogram:
destination unreachable: 143
echo request: 14
echo replies: 13809376
timestamp replies: 91
IcmpMsg:
InType0: 175
InType3: 3239
InType8: 13809376
InType11: 432
InType13: 91
OutType0: 13809376
OutType3: 143
OutType8: 14
OutType14: 91
Tcp:
11025992 active connections openings
2461256 passive connection openings
248809 failed connection attempts
39534 connection resets received
14 connections established
123115503 segments received
134149976 segments send out
5202375 segments retransmited
24422 bad segments received.
302984 resets sent
InCsumErrors: 23807
Udp:
4252325 packets received
143 packets to unknown port received.
0 packet receive errors
4252618 packets sent
0 receive buffer errors
0 send buffer errors
UdpLite:
TcpExt:
288 SYN cookies sent
1 SYN cookies received
157837 invalid SYN cookies received
228014 resets received for embryonic SYN_RECV sockets
994 packets pruned from receive queue because of socket buffer overrun
246 ICMP packets dropped because they were out-of-window
965882 TCP sockets finished time wait in fast timer
7253 packets rejects in established connections because of timestamp
3912126 delayed acks sent
522 delayed acks further delayed because of locked socket
Quick ack mode was activated 152550 times
3986 times the listen queue of a socket overflowed
5811 SYNs to LISTEN sockets dropped
984697 packets directly queued to recvmsg prequeue.
464913 bytes directly in process context from backlog
26707475 bytes directly received in process context from prequeue
27708536 packet headers predicted
5630 packets header predicted and directly queued to user
44344525 acknowledgments not containing data payload received
14968170 predicted acknowledgments
153 times recovered from packet loss due to fast retransmit
81069 times recovered from packet loss by selective acknowledgements
918 bad SACK blocks received
Detected reordering 1005 times using FACK
Detected reordering 1003 times using SACK
Detected reordering 32 times using reno fast retransmit
Detected reordering 4401 times using time stamp
1875 congestion windows fully recovered without slow start
4205 congestion windows partially recovered using Hoe heuristic
6088 congestion windows recovered without slow start by DSACK
55385 congestion windows recovered without slow start after partial ack
TCPLostRetransmit: 27834
31 timeouts after reno fast retransmit
11862 timeouts after SACK recovery
44654 timeouts in loss state
383435 fast retransmits
25395 forward retransmits
547771 retransmits in slow start
3856050 other TCP timeouts
TCPLossProbes: 589192
TCPLossProbeRecovery: 284584
30 classic Reno fast retransmits failed
35939 SACK retransmits failed
160352 DSACKs sent for old packets
1155 DSACKs sent for out of order packets
316513 DSACKs received
4149 DSACKs for out of order packets received
26395 connections reset due to unexpected data
1736 connections reset due to early user close
7726 connections aborted due to timeout
TCPSACKDiscard: 41
TCPDSACKIgnoredOld: 437
TCPDSACKIgnoredNoUndo: 217313
TCPSpuriousRTOs: 4212
TCPSackShiftFallback: 699121
TCPBacklogDrop: 1
TCPReqQFullDoCookies: 288
TCPRetransFail: 5
TCPRcvCoalesce: 11995363
TCPOFOQueue: 399736
TCPOFOMerge: 1519
TCPChallengeACK: 2475
TCPSYNChallenge: 622
TCPFastOpenCookieReqd: 11
TCPSpuriousRtxHostQueues: 39326
TCPAutoCorking: 533
TCPFromZeroWindowAdv: 449
TCPToZeroWindowAdv: 449
TCPWantZeroWindowAdv: 3253
TCPSynRetrans: 3411152
TCPOrigDataSent: 76114506
TCPHystartTrainDetect: 1896
TCPHystartTrainCwnd: 74460
TCPHystartDelayDetect: 6891
TCPHystartDelayCwnd: 423886
TCPACKSkippedSynRecv: 29912
TCPACKSkippedPAWS: 1152
TCPACKSkippedSeq: 971
TCPACKSkippedTimeWait: 12
TCPACKSkippedChallenge: 239
IpExt:
InNoRoutes: 1
InMcastPkts: 305307
InOctets: 68437168659
OutOctets: 92168136568
InMcastOctets: 10991052
InNoECTPkts: 143634491
InECT1Pkts: 3
InECT0Pkts: 8407
InCEPkts: 1217
按照各个协议分别显示其统计数据。如果我们的应用程序(如Web浏览器)运行速度比较慢,或者不能显示Web页之类的数据,那么我们就可以用本选项来查看一下所显示的信息。我们需要仔细查看统计数据的各行,找到出错的关键字,进而确定问题所在。
5.7 显示监听的套接口
# netstat -l
Active Internet connections (only servers)
Proto Recv-Q Send-Q Local Address Foreign Address State
tcp 0 0 VM-0-15-centos:6666 0.0.0.0:* LISTEN
tcp 0 0 0.0.0.0:http 0.0.0.0:* LISTEN
tcp 0 0 0.0.0.0:ssh 0.0.0.0:* LISTEN
tcp 0 0 0.0.0.0:https 0.0.0.0:* LISTEN
tcp 0 0 0.0.0.0:8000 0.0.0.0:* LISTEN
tcp 0 0 0.0.0.0:cslistener 0.0.0.0:* LISTEN
tcp6 0 0 [::]:mysql [::]:* LISTEN
udp 0 0 0.0.0.0:bootpc 0.0.0.0:*
udp 0 0 service-01:ntp 0.0.0.0:*
udp 0 0 VM-0-15-centos:ntp 0.0.0.0:*
udp6 0 0 service-01:ntp [::]:*
udp6 0 0 VM-0-15-centos:ntp [::]:*
Active UNIX domain sockets (only servers)
Proto RefCnt Flags Type State I-Node Path
unix 2 [ ACC ] STREAM LISTENING 13070 /var/run/lsm/ipc/simc
unix 2 [ ACC ] STREAM LISTENING 9486 /run/systemd/journal/stdout
unix 2 [ ACC ] STREAM LISTENING 12846 /run/dbus/system_bus_socket
unix 2 [ ACC ] STREAM LISTENING 532120 /tmp/mysql.sock
unix 2 [ ACC ] STREAM LISTENING 10552 /run/lvm/lvmpolld.socket
unix 2 [ ACC ] STREAM LISTENING 11852 /run/lvm/lvmetad.socket
unix 2 [ ACC ] STREAM LISTENING 13946 /var/run/lsm/ipc/sim
unix 2 [ ACC ] STREAM LISTENING 14462 /var/run/acpid.socket
unix 2 [ ACC ] STREAM LISTENING 291339499 /usr/local/qcloud/YunJing/conf/ydrpc_1
unix 2 [ ACC ] SEQPACKET LISTENING 11925 /run/udev/control
unix 2 [ ACC ] STREAM LISTENING 11719 /run/systemd/private
unix 2 [ ACC ] STREAM LISTENING 21457 /tmp/tmux-0/default
5.8 显示所有已建立的有效连接
# netstat -n
5.9 显示关于以太网的统计数据
# netstat -e
用于显示关于以太网的统计数据。它列出的项目包括传送的数据报的总字节数、错误数、删除数、数据报的数量和广播的数量。这些统计数据既有发送的数据报数量,也有接收的数据报数量。这个选项可以用来统计一些基本的网络流量
5.10 显示关于路由表的信息
# netstat -r
Destination Gateway Genmask Flags MSS Window irtt Iface
default gateway 0.0.0.0 UG 0 0 0 eth0
link-local 0.0.0.0 255.255.0.0 U 0 0 0 eth0
172.21.0.0 0.0.0.0 255.255.240.0 U 0 0 0 eth0
5.11 统计机器中网络连接各个状态个数
netstat -a | awk '/^tcp/ {++S[$NF]} END {for(a in S) print a, S[a]}'
LISTEN 7
CLOSE_WAIT 314
ESTABLISHED 7
TIME_WAIT 5
5.12 把状态全都取出来后使用uniq -c统计后再进行排序
netstat -nat |awk '{print $6}'|sort|uniq -c
314 CLOSE_WAIT
1 established)
12 ESTABLISHED
1 FIN_WAIT2
1 Foreign
7 LISTEN
2 SYN_RECV
3 TIME_WAIT
5.13 查看程序运行的端口
netstat -ap | grep ssh
5.14 在 netstat 输出中显示 PID 和进程名称
netstat -pt
5.15 找出运行在指定端口的进程,根据端口号查找进程名
netstat -anpt | grep '80'
运行在端口80的进程id为13548,再通过ps命令就可以找到具体的应用程序了。
ps -aux | grep 13548
本文为joshua317原创文章,转载请注明:转载自joshua317博客 https://www.joshua317.com/article/203