Linux netstat命令

发布时间 2023-04-12 22:02:48作者: 红尘过客2022

Linux netstat命令

非常重要的命令,尤其是面对openresty、nginx等代理服务器,解决网络阻塞的关键命令

root@jdit:~# netstat -a | awk '/^tcp/ {++S[$NF]} END {for(a in S) print a, S[a]}'
LISTEN 5
ESTABLISHED 3
TIME_WAIT 1

Linux netstat 命令用于显示网络状态。

利用 netstat 指令可让你得知整个 Linux 系统的网络情况。

语法

netstat [-acCeFghilMnNoprstuvVwx][-A<网络类型>][--ip]

参数说明

  • -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 -a

显示当前户籍UDP连接状况

# netstat -nu

显示UDP端口号的使用情况

root@jdit:~# netstat -apu
Active Internet connections (servers and established)
Proto Recv-Q Send-Q Local Address           Foreign Address         State       PID/Program name
udp        0      0 localhost:domain        0.0.0.0:*                           71701/systemd-resol
udp        0      0 iZ2ze1w1u469cd5g:bootpc 0.0.0.0:*                           4828/systemd-networ
udp        0      0 localhost:323           0.0.0.0:*                           71734/chronyd
udp6       0      0 ip6-localhost:323       [::]:*                              71734/chronyd

显示网卡列表

root@jdit:~# 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  1526526      0      0 0       1190444      0      0      0 BMRU
lo       65536   183355      0      0 0        183355      0      0      0 LRU

显示组播组的关系

root@jdit:~# netstat -g
IPv6/IPv4 Group Memberships
Interface       RefCnt Group
--------------- ------ ---------------------
lo              1      all-systems.mcast.net
eth0            1      all-systems.mcast.net
lo              1      ip6-allnodes
lo              1      ff01::1
eth0            1      ff02::1:ff12:126f
eth0            2      ip6-allnodes
eth0            1      ff01::1
root@jdit:~#

显示网络统计信息

root@jdit:~# netstat -s
Ip:
    Forwarding: 2
    1014665 total packets received
    9 with invalid addresses
    0 forwarded
    0 incoming packets discarded
    1014656 incoming packets delivered
    975856 requests sent out
Icmp:
    59888 ICMP messages received
    40 input ICMP message failed
    InCsumErrors: 13
    ICMP input histogram:
        destination unreachable: 133
        echo requests: 59738
        echo replies: 2
        timestamp request: 2
    59829 ICMP messages sent
    0 ICMP messages failed
    ICMP output histogram:
        destination unreachable: 89
        echo replies: 59738
        timestamp replies: 2
IcmpMsg:
        InType0: 2
        InType3: 133
        InType8: 59738
        InType13: 2
        OutType0: 59738
        OutType3: 89
        OutType14: 2
Tcp:
    43576 active connection openings
    1348 passive connection openings
    23788 failed connection attempts
    425 connection resets received
    3 connections established
    696441 segments received
    683395 segments sent out
    2873 segments retransmitted
    431 bad segments received
    46575 resets sent
    InCsumErrors: 395
Udp:
    258229 packets received
    89 packets to unknown port received
    0 packet receive errors
    271715 packets sent
    0 receive buffer errors
    0 send buffer errors
UdpLite:
TcpExt:
    23716 resets received for embryonic SYN_RECV sockets
    7 ICMP packets dropped because they were out-of-window
    38835 TCP sockets finished time wait in fast timer
    1428 delayed acks sent
    Quick ack mode was activated 275 times
    105641 packet headers predicted
    66845 acknowledgments not containing data payload received
    154479 predicted acknowledgments
    1 times recovered from packet loss due to fast retransmit
    TCPSackRecovery: 156
    Detected reordering 56 times using SACK
    Detected reordering 8 times using reno fast retransmit
    1 congestion windows fully recovered without slow start
    TCPDSACKUndo: 75
    7 congestion windows recovered without slow start after partial ack
    TCPLostRetransmit: 218
    TCPSackFailures: 4
    2 timeouts in loss state
    331 fast retransmits
    1 retransmits in slow start
    TCPTimeouts: 2934
    TCPLossProbes: 289
    TCPLossProbeRecovery: 46
    TCPSackRecoveryFail: 4
    TCPBacklogCoalesce: 746
    TCPDSACKOldSent: 278
    TCPDSACKRecv: 169
    TCPDSACKOfoRecv: 47
    71 connections reset due to unexpected data
    253 connections reset due to early user close
    40 connections aborted due to timeout
    TCPDSACKIgnoredNoUndo: 51
    TCPSackShifted: 1710
    TCPSackMerged: 244
    TCPSackShiftFallback: 257
    TCPRcvCoalesce: 43172
    TCPOFOQueue: 13350
    TCPOFOMerge: 27
    TCPChallengeACK: 41
    TCPSYNChallenge: 38
    TCPAutoCorking: 1451
    TCPFromZeroWindowAdv: 1
    TCPToZeroWindowAdv: 1
    TCPWantZeroWindowAdv: 200
    TCPSynRetrans: 2051
    TCPOrigDataSent: 378682
    TCPHystartTrainDetect: 1
    TCPHystartTrainCwnd: 17
    TCPHystartDelayDetect: 2
    TCPHystartDelayCwnd: 2617
    TCPACKSkippedSynRecv: 1
    TCPKeepAlive: 603
    TCPDelivered: 421633
    TCPAckCompressed: 6924
    TcpTimeoutRehash: 439
    TcpDuplicateDataRehash: 34
    TCPDSACKRecvSegs: 216
IpExt:
    InOctets: 637501815
    OutOctets: 200810317
    InNoECTPkts: 1312537
MPTcpExt:

显示监听的套接口

root@jdit:~# netstat -l
Active Internet connections (only servers)
Proto Recv-Q Send-Q Local Address           Foreign Address         State
tcp        0      0 localhost:6010          0.0.0.0:*               LISTEN
tcp        0      0 localhost:domain        0.0.0.0:*               LISTEN
tcp        0      0 0.0.0.0:redis           0.0.0.0:*               LISTEN
tcp        0      0 0.0.0.0:ssh             0.0.0.0:*               LISTEN
tcp6       0      0 [::]:redis              [::]:*                  LISTEN
udp        0      0 localhost:domain        0.0.0.0:*
udp        0      0 iZ2ze1w1u469cd5g:bootpc 0.0.0.0:*
udp        0      0 localhost:323           0.0.0.0:*
udp6       0      0 ip6-localhost:323       [::]:*
raw6       0      0 [::]:ipv6-icmp          [::]:*                  7
Active UNIX domain sockets (only servers)
Proto RefCnt Flags       Type       State         I-Node   Path
unix  2      [ ACC ]     STREAM     LISTENING     110292   /run/user/0/systemd/private
unix  2      [ ACC ]     STREAM     LISTENING     110861   /run/user/0/bus
unix  2      [ ACC ]     STREAM     LISTENING     17485    @/org/kernel/linux/storage/multipathd
unix  2      [ ACC ]     STREAM     LISTENING     110863   /run/user/0/gnupg/S.dirmngr
unix  2      [ ACC ]     STREAM     LISTENING     110865   /run/user/0/gnupg/S.gpg-agent.browser
unix  2      [ ACC ]     STREAM     LISTENING     110867   /run/user/0/gnupg/S.gpg-agent.extra
unix  2      [ ACC ]     STREAM     LISTENING     110869   /run/user/0/gnupg/S.gpg-agent.ssh
unix  2      [ ACC ]     STREAM     LISTENING     110871   /run/user/0/gnupg/S.gpg-agent
unix  2      [ ACC ]     STREAM     LISTENING     110873   /run/user/0/pk-debconf-socket
unix  2      [ ACC ]     STREAM     LISTENING     110875   /run/user/0/snapd-session-agent.socket
unix  2      [ ACC ]     STREAM     LISTENING     20184    /var/snap/lxd/common/lxd/unix.socket
unix  2      [ ACC ]     STREAM     LISTENING     57267    /run/systemd/private
unix  2      [ ACC ]     STREAM     LISTENING     56315    /run/systemd/userdb/io.systemd.DynamicUser
unix  2      [ ACC ]     STREAM     LISTENING     56316    /run/systemd/io.system.ManagedOOM
unix  2      [ ACC ]     STREAM     LISTENING     225419   /usr/local/aegis/Aegis-<guid(5a2c30a2-a87d-490a-9281-6765edad7cba)>
unix  2      [ ACC ]     STREAM     LISTENING     57515    /run/systemd/journal/io.systemd.journal
unix  2      [ ACC ]     STREAM     LISTENING     24904    /tmp/aliyun_assist_service.sock
unix  2      [ ACC ]     STREAM     LISTENING     20181    /run/dbus/system_bus_socket
unix  2      [ ACC ]     STREAM     LISTENING     20188    /run/snapd.socket
unix  2      [ ACC ]     STREAM     LISTENING     20190    /run/snapd-snap.socket
unix  2      [ ACC ]     STREAM     LISTENING     20779    /run/uuidd/request
unix  2      [ ACC ]     STREAM     LISTENING     20186    /var/snap/lxd/common/lxd-user/unix.socket
unix  2      [ ACC ]     STREAM     LISTENING     20183    @ISCSIADM_ABSTRACT_NAMESPACE
unix  2      [ ACC ]     STREAM     LISTENING     17387    /run/lvm/lvmpolld.socket
unix  2      [ ACC ]     STREAM     LISTENING     17488    /run/systemd/fsck.progress
unix  2      [ ACC ]     STREAM     LISTENING     17393    /run/systemd/journal/stdout
unix  2      [ ACC ]     SEQPACKET  LISTENING     17396    /run/udev/control
unix  2      [ ACC ]     STREAM     LISTENING     573784   /run/systemd/resolve/io.systemd.Resolve

</guid(5a2c30a2-a87d-490a-9281-6765edad7cba)>