Mr Dk.'s BlogMr Dk.'s Blog
  • 🦆 About Me
  • ⛏️ Technology Stack
  • 🔗 Links
  • 🗒️ About Blog
  • Algorithm
  • C++
  • Compiler
  • Cryptography
  • DevOps
  • Docker
  • Git
  • Java
  • Linux
  • MS Office
  • MySQL
  • Network
  • Operating System
  • Performance
  • PostgreSQL
  • Productivity
  • Solidity
  • Vue.js
  • Web
  • Wireless
  • 🐧 How Linux Works (notes)
  • 🐧 Linux Kernel Comments (notes)
  • 🐧 Linux Kernel Development (notes)
  • 🐤 μc/OS-II Source Code (notes)
  • ☕ Understanding the JVM (notes)
  • ⛸️ Redis Implementation (notes)
  • 🗜️ Understanding Nginx (notes)
  • ⚙️ Netty in Action (notes)
  • ☁️ Spring Microservices (notes)
  • ⚒️ The Annotated STL Sources (notes)
  • ☕ Java Development Kit 8
GitHub
  • 🦆 About Me
  • ⛏️ Technology Stack
  • 🔗 Links
  • 🗒️ About Blog
  • Algorithm
  • C++
  • Compiler
  • Cryptography
  • DevOps
  • Docker
  • Git
  • Java
  • Linux
  • MS Office
  • MySQL
  • Network
  • Operating System
  • Performance
  • PostgreSQL
  • Productivity
  • Solidity
  • Vue.js
  • Web
  • Wireless
  • 🐧 How Linux Works (notes)
  • 🐧 Linux Kernel Comments (notes)
  • 🐧 Linux Kernel Development (notes)
  • 🐤 μc/OS-II Source Code (notes)
  • ☕ Understanding the JVM (notes)
  • ⛸️ Redis Implementation (notes)
  • 🗜️ Understanding Nginx (notes)
  • ⚙️ Netty in Action (notes)
  • ☁️ Spring Microservices (notes)
  • ⚒️ The Annotated STL Sources (notes)
  • ☕ Java Development Kit 8
GitHub
  • 📝 Notes
    • Algorithm
      • Algorithm - Bloom Filter
      • Algorithm - Disjoint Set
      • Algorithm - Fast Power
      • Algorithm - KMP
      • Algorithm - Monotonic Stack
      • Algorithm - RB-Tree
      • Algorithm - Regular Expression
      • Algorithm - Sliding Window
      • Online Judge - I/O
    • C++
      • C++ - Const
      • C++ File I/O
      • C++ - Object Layout
      • C++ - Operator Overload
      • C++ - Polymorphism
      • C++ STL algorithm
      • C++ STL map
      • C++ STL multimap
      • C++ STL priority_queue
      • C++ STL set
      • C++ STL string
      • C++ STL unordered_map
      • C++ STL vector
      • C++ - Smart Pointer
      • C++ - Template & Genericity
    • Compiler
      • ANTLR - Basic
      • Compiler - LLVM Architecture
      • Compiler - Multi-version GCC
    • Cryptography
      • Cryptography - Certbot
      • Cryptography - Digital Signature & PKCS #7
      • Cryptography - GPG
      • Cryptography - JWT
      • Cryptography - Keystore & Certificates
      • Cryptography - OAuth 2.0
      • Cryptography - Java 实现对称与非对称加密算法
      • Cryptography - TLS
    • DevOps
      • DevOps - Travis CI
    • Docker
      • Docker - Image & Storage Management
      • Docker - Image
      • Docker - Libcontainer
      • Docker - Multi-Arch Image
      • Docker - Multi-Stage Build
      • Docker - Network
      • Docker - Orchestration & Deployment
      • Docker - Overview
      • Docker - Service Building
      • Docker - Volume & Network Usage
      • Docker - Volume
      • Linux - Control Group
      • Linux - Namespace
    • Git
      • Git - Branch & Merge
      • Git - Cached
      • Git - Cherry Pick
      • Git - Commit
      • Git - Patch
      • Git - Proxy
      • Git - Rebase
      • Git - Reset
      • Git - Stash
      • Git - Theme for Git-Bash
    • Java
      • JVM - Synchronized
      • JVM - Volatile
      • Java - Annotation 注解
      • Java - BIO & NIO
      • Java - Class Path
      • Java - Condition and LockSupport
      • Java - Current Timestamp
      • Java - Deep Copy
      • Java - 运行环境配置
      • Java - Equals
      • Java - Exporting JAR
      • Java - Javadoc
      • Java - Lock
      • Java - Maven 项目构建工具
      • Java - References
      • Java - Reflection Mechanism
      • Java - String Split
      • Java - Thread Pool
      • Java - Thread
      • Tomcat - Class Loader
      • Tomcat - Container
    • Linux
      • addr2line
      • cut
      • df
      • du
      • fallocate
      • find
      • fio
      • grep
      • groupadd
      • gzip
      • head / tail
      • hexdump
      • iostat
      • iotop
      • kill
      • ldd
      • lsof
      • ltrace / strace
      • mpstat
      • netstat
      • nm
      • pidstat
      • pmap
      • readlink
      • readlink
      • rpm2cpio / rpm2archive
      • sort
      • tee
      • uniq
      • useradd
      • usermod
      • watch
      • wc
      • which
      • xargs
    • MS Office
      • MS Office - Add-in Dev
      • MS Office - Application
    • MySQL
      • InnoDB - Architecture
      • InnoDB - Backup
      • InnoDB - Checkpoint
      • InnoDB - Critical Features
      • InnoDB - Files
      • InnoDB - Index
      • InnoDB - Insert Buffer
      • InnoDB - Lock
      • InnoDB - Partition Table
      • InnoDB - Table Storage
      • MySQL - Server Configuration
      • MySQL - Storage Engine
    • Network
      • Network - ARP
      • Network - FTP
      • Network - GitHub Accelerating
      • HTTP - Message Format
      • HTTP - POST 提交表单的两种方式
      • Network - Proxy Server
      • Network - SCP
      • Network - SSH
      • Network - TCP Congestion Control
      • Network - TCP Connection Management
      • Network - TCP Flow Control
      • Network - TCP Retransmission
      • Network - Traceroute
      • Network - V2Ray
      • Network - WebSocket
      • Network - Windows 10 Mail APP
      • Network - frp
    • Operating System
      • Linux - Kernel Compilation
      • Linux - Multi-OS
      • Linux - Mutex & Condition
      • Linux - Operations
      • Linux: Package Manager
      • Linux - Process Manipulation
      • Linux - User ID
      • Linux - Execve
      • OS - Compile and Link
      • OS - Dynamic Linking
      • OS - ELF
      • Linux - Image
      • OS - Loading
      • OS - Shared Library Organization
      • OS - Static Linking
      • Syzkaller - Architecture
      • Syzkaller - Description Syntax
      • Syzkaller - Usage
      • Ubuntu - Desktop Recover (Python)
      • WSL: CentOS 8
    • Performance
      • Linux Performance - Perf Event
      • Linux Performance - Perf Record
      • Linux Performance - Perf Report
      • Linux Performance - Flame Graphs
      • Linux Performance - Off CPU Analyze
    • PostgreSQL
      • PostgreSQL - ANALYZE
      • PostgreSQL - Atomics
      • PostgreSQL - CREATE INDEX CONCURRENTLY
      • PostgreSQL - COPY FROM
      • PostgreSQL - COPY TO
      • PostgreSQL - Executor: Append
      • PostgreSQL - Executor: Group
      • PostgreSQL - Executor: Limit
      • PostgreSQL - Executor: Material
      • PostgreSQL - Executor: Nest Loop Join
      • PostgreSQL - Executor: Result
      • PostgreSQL - Executor: Sequential Scan
      • PostgreSQL - Executor: Sort
      • PostgreSQL - Executor: Unique
      • PostgreSQL - FDW Asynchronous Execution
      • PostgreSQL - GUC
      • PostgreSQL - Locking
      • PostgreSQL - LWLock
      • PostgreSQL - Multi Insert
      • PostgreSQL - Plan Hint GUC
      • PostgreSQL - Process Activity
      • PostgreSQL - Query Execution
      • PostgreSQL - Spinlock
      • PostgreSQL - Storage Management
      • PostgreSQL - VFD
      • PostgreSQL - WAL Insert
      • PostgreSQL - WAL Prefetch
    • Productivity
      • LaTeX
      • Venn Diagram
      • VuePress
    • Solidity
      • Solidity - ABI Specification
      • Solidity - Contracts
      • Solidity - Expressions and Control Structures
      • Solidity - Layout and Structure
      • Solidity - Remix IDE
      • Solidity - Slither
      • Solidity - Types
      • Solidity - Units and Globally Available Variables
    • Vue.js
      • Vue.js - Environment Variable
    • Web
      • Web - CORS
      • Web - OpenAPI Specification
    • Wireless
      • Wireless - WEP Cracking by Aircrack-ng
      • Wireless - WPS Cracking by Reaver
      • Wireless - wifiphisher

netstat

Created by : Mr Dk.

2022 / 12 / 17 16:49

Hangzhou, Zhejiang, China


Background

netstat 用于显示网络连接信息。

Usage

其参数选项主要用于决定在输出中是否展示某些信息。所以输出信息的解读就比较重要。

输出分为两部分:

  • Active Internet connections
  • Active UNIX domain Sockets

Internet Connections

其中,对于活跃的互联网连接,输出的信息包含:

  • Socket 协议:TCP / UDP / ...
  • 接收队列:已经接收但未被拷贝到用户程序的字节数
  • 发送队列:远程未确认的字节数
  • 本地地址:本地 IP 和端口号(如果不加 -n,那么会被翻译为常见域名和应用程序名,比如 localhost:mysql)
  • 远程地址
  • 状态:主要是给 TCP Socket 使用
  • 用户:Socket 所属的用户名或用户 ID
  • 程序:使用 --program 引入这一列,显示这个 Socket 的进程名或进程号
  • 定时器:与这个 Socket 相关的 TCP 定时器,需要使用 --timers 引入
$ netstat -a --program --timers
(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     Timer
tcp        0      0 localhost:8005          0.0.0.0:*               LISTEN      -                    off (0.00/0/0)
tcp        0      0 localhost:8002          0.0.0.0:*               LISTEN      -                    off (0.00/0/0)
tcp        0      0 localhost:8000          0.0.0.0:*               LISTEN      -                    off (0.00/0/0)
tcp        0      0 0.0.0.0:ssh             0.0.0.0:*               LISTEN      -                    off (0.00/0/0)
tcp        0      0 localhost:domain        0.0.0.0:*               LISTEN      -                    off (0.00/0/0)
tcp        0      0 localhost:36147         0.0.0.0:*               LISTEN      2853/node            off (0.00/0/0)
tcp        0      0 localhost:mysql         0.0.0.0:*               LISTEN      -                    off (0.00/0/0)
tcp        0      0 localhost:11211         0.0.0.0:*               LISTEN      -                    off (0.00/0/0)
tcp        0      0 localhost:33060         0.0.0.0:*               LISTEN      -                    off (0.00/0/0)
tcp        0      0 localhost:ipp           0.0.0.0:*               LISTEN      -                    off (0.00/0/0)
tcp        0      0 localhost:36147         localhost:53890         ESTABLISHED 2853/node            off (0.00/0/0)
tcp        0      0 192.168.0.105:ssh       192.168.0.103:61072     ESTABLISHED -                    keepalive (5804.14/0/0)
tcp        0      0 192.168.0.105:59186     143.244.210.202:https   TIME_WAIT   -                    timewait (47.68/0/0)
tcp        0      0 localhost:53890         localhost:36147         ESTABLISHED -                    off (0.00/0/0)
tcp        0      0 192.168.0.105:50736     124.221.154.83:12700    ESTABLISHED -                    keepalive (2.24/0/0)
tcp        0    324 192.168.0.105:ssh       192.168.0.103:57526     ESTABLISHED -                    on (0.20/0/0)
tcp        0      0 localhost:53892         localhost:36147         ESTABLISHED -                    off (0.00/0/0)
tcp        0      0 192.168.0.105:ssh       192.168.0.103:62188     ESTABLISHED -                    keepalive (6399.66/0/0)
tcp        0      0 localhost:36147         localhost:53892         ESTABLISHED 4285/node            off (0.00/0/0)
tcp6       0      0 [::]:ms-wbt-server      [::]:*                  LISTEN      -                    off (0.00/0/0)
tcp6       0      0 ip6-localhost:3350      [::]:*                  LISTEN      -                    off (0.00/0/0)
tcp6       0      0 [::]:http               [::]:*                  LISTEN      -                    off (0.00/0/0)
tcp6       0      0 [::]:ssh                [::]:*                  LISTEN      -                    off (0.00/0/0)
tcp6       0      0 ip6-localhost:ipp       [::]:*                  LISTEN      -                    off (0.00/0/0)
udp        0      0 0.0.0.0:mdns            0.0.0.0:*                           -                    off (0.00/0/0)
udp        0      0 0.0.0.0:47784           0.0.0.0:*                           -                    off (0.00/0/0)
udp        0      0 localhost:domain        0.0.0.0:*                           -                    off (0.00/0/0)
udp        0      0 192.168.0.105:bootpc    192.168.0.1:bootps      ESTABLISHED -                    off (0.00/0/0)
udp        0      0 0.0.0.0:631             0.0.0.0:*                           -                    off (0.00/0/0)
udp        0      0 192.168.0.105:43301     192.168.0.1:domain      ESTABLISHED -                    off (0.00/0/0)
udp6       0      0 [::]:36812              [::]:*                              -                    off (0.00/0/0)
udp6       0      0 [::]:mdns               [::]:*                              -                    off (0.00/0/0)
raw6       0      0 [::]:ipv6-icmp          [::]:*                  7           -                    off (0.00/0/0)

UNIX Domain Sockets

对于活跃的 UNIX Domain Sockets,打印的信息包含:

  • 协议
  • 附着到这个 Socket 上的进程数(引用计数)
  • 标志位
  • Socket 类型
  • 状态
  • 进程名 / 进程编号
  • 路径
$ netstat -a --program

Active UNIX domain sockets (servers and established)
Proto RefCnt Flags       Type       State         I-Node   PID/Program name     Path
unix  2      [ ACC ]     STREAM     LISTENING     30243    -                    /var/run/docker/libnetwork/c3ceef6d7bab.sock
unix  2      [ ACC ]     STREAM     LISTENING     27523    -                    @/tmp/.ICE-unix/1617
unix  2      [ ACC ]     STREAM     LISTENING     35116    -                    @/tmp/.X11-unix/X0
unix  2      [ ACC ]     STREAM     LISTENING     32384    -                    @/var/lib/gdm3/.cache/ibus/dbus-LepPuSBi
unix  2      [ ]         DGRAM                    35506    2135/systemd         /run/user/1000/systemd/notify
unix  2      [ ]         DGRAM                    30078    -                    /run/user/125/systemd/notify
unix  2      [ ACC ]     STREAM     LISTENING     35509    2135/systemd         /run/user/1000/systemd/private
unix  2      [ ACC ]     STREAM     LISTENING     30081    -                    /run/user/125/systemd/private
unix  2      [ ACC ]     STREAM     LISTENING     35515    2135/systemd         /run/user/1000/bus
unix  2      [ ACC ]     STREAM     LISTENING     32059    -                    /run/user/125/bus
unix  2      [ ACC ]     STREAM     LISTENING     35517    2135/systemd         /run/user/1000/gnupg/S.dirmngr
unix  2      [ ACC ]     STREAM     LISTENING     32061    -                    /run/user/125/gnupg/S.dirmngr
unix  2      [ ACC ]     STREAM     LISTENING     35519    2135/systemd         /run/user/1000/gnupg/S.gpg-agent.browser

网卡列表

使用 -i 及 -ie(基本等价于 ifconfig)选项,可以看到所有网卡信息:

$ netstat -i
Kernel Interface table
Iface      MTU    RX-OK RX-ERR RX-DRP RX-OVR    TX-OK TX-ERR TX-DRP TX-OVR Flg
docker0   1500        0      0      0 0             0      0      0      0 BMU
enp4s0    1500   340806      0      0 0        134521      0      0      0 BMRU
lo       65536    41882      0      0 0         41882      0      0      0 LRU

$ netstat -ie
Kernel Interface table
docker0: flags=4099<UP,BROADCAST,MULTICAST>  mtu 1500
        inet 172.17.0.1  netmask 255.255.0.0  broadcast 172.17.255.255
        ether 02:42:06:aa:44:da  txqueuelen 0  (Ethernet)
        RX packets 0  bytes 0 (0.0 B)
        RX errors 0  dropped 0  overruns 0  frame 0
        TX packets 0  bytes 0 (0.0 B)
        TX errors 0  dropped 0 overruns 0  carrier 0  collisions 0

enp4s0: flags=4163<UP,BROADCAST,RUNNING,MULTICAST>  mtu 1500
        inet 192.168.0.105  netmask 255.255.255.0  broadcast 192.168.0.255
        inet6 fe80::fdb5:c5e3:982b:21d1  prefixlen 64  scopeid 0x20<link>
        ether c8:5b:76:e4:d6:cf  txqueuelen 1000  (Ethernet)
        RX packets 341106  bytes 461278024 (461.2 MB)
        RX errors 0  dropped 0  overruns 0  frame 0
        TX packets 134869  bytes 17595636 (17.5 MB)
        TX errors 0  dropped 0 overruns 0  carrier 0  collisions 0

lo: flags=73<UP,LOOPBACK,RUNNING>  mtu 65536
        inet 127.0.0.1  netmask 255.0.0.0
        inet6 ::1  prefixlen 128  scopeid 0x10<host>
        loop  txqueuelen 1000  (Local Loopback)
        RX packets 42420  bytes 9788790 (9.7 MB)
        RX errors 0  dropped 0  overruns 0  frame 0
        TX packets 42420  bytes 9788790 (9.7 MB)
        TX errors 0  dropped 0 overruns 0  carrier 0  collisions 0

路由信息

使用 -r 选项可以看到内核路由表:

$ netstat -r
Kernel IP routing table
Destination     Gateway         Genmask         Flags   MSS Window  irtt Iface
default         192.168.0.1     0.0.0.0         UG        0 0          0 enp4s0
link-local      0.0.0.0         255.255.0.0     U         0 0          0 enp4s0
172.17.0.0      0.0.0.0         255.255.0.0     U         0 0          0 docker0
192.168.0.0     0.0.0.0         255.255.255.0   U         0 0          0 enp4s0

监控

-c 参数等价于使用 watch 命令持续刷新输出。

References

20 Netstat Commands for Linux Network Management

netstat(8) — Linux manual page

Edit this page on GitHub
Prev
mpstat
Next
nm