Netstat 命令

netstat命令显示网络连接的状态.

显示当前活动的网络连接

  1. netstat -an
  2. Active Internet connections (servers and established)
  3. Proto Recv-Q Send-Q Local Address Foreign Address State
  4. tcp 0 0 127.0.0.1:44244 0.0.0.0:* LISTEN
  5. tcp 0 0 127.0.1.1:53 0.0.0.0:* LISTEN
  6. tcp 0 0 127.0.0.1:25 0.0.0.0:* LISTEN
  7. tcp 0 0 127.0.0.1:3306 0.0.0.0:* LISTEN
  8. ... ... ...
  9. Active UNIX domain sockets (servers and established)
  10. Proto RefCnt Flags Type State I-Node Path
  11. unix 2 [ ] DGRAM 22370 /run/user/1000/systemd/notify
  12. unix 2 [ ACC ] STREAM LISTENING 23426 @/tmp/.ICE-unix/2548
  13. unix 2 [ ACC ] STREAM LISTENING 22371 /run/user/1000/systemd/private
  14. unix 2 [ ACC ] SEQPACKET LISTENING 8934 /run/udev/control
  15. unix 2 [ ACC ] STREAM LISTENING 20436 /run/user/1000/keyring/control
  16. unix 2 [ ACC ] STREAM LISTENING 24609 /tmp/fcitx-socket-:0
  17. unix 2 [ ACC ] STREAM LISTENING 27013 /tmp/sogou-qimpanel-cell

显示运行程序的进程号/程序名

  1. netstat -tapn
  2. (Not all processes could be identified, non-owned process info
  3. will not be shown, you would have to be root to see it all.)
  4. Active Internet connections (servers and established)
  5. Proto Recv-Q Send-Q Local Address Foreign Address State PID/Program name
  6. tcp 0 0 127.0.0.1:44244 0.0.0.0:* LISTEN 9720/1452662683780-
  7. tcp 0 0 127.0.1.1:53 0.0.0.0:* LISTEN -
  8. tcp 0 0 127.0.0.1:25 0.0.0.0:* LISTEN -
  9. tcp 0 0 127.0.0.1:3306 0.0.0.0:* LISTEN -
  10. tcp 0 0 127.0.0.1:587 0.0.0.0:* LISTEN -
  11. tcp 0 0 127.0.0.1:47034 127.0.0.1:44244 ESTABLISHED 9680/editor
  12. tcp 0 0 127.0.0.1:44244 127.0.0.1:47034 ESTABLISHED 9720/1452662683780-
  13. tcp 0 0 127.0.0.1:47038 127.0.0.1:44244 ESTABLISHED 9680/editor
  14. tcp 0 0 127.0.0.1:44244 127.0.0.1:47038 ESTABLISHED 9720/1452662683780-
  15. tcp6 0 0 :::1080 :::* LISTEN 31588/shadowsocks
  16. tcp6 0 0 :::1088 :::* LISTEN 31589/shadowsocks

在最后一列有运行的进程号/程序名.

显示路由表

  1. netstat --route
  2. Kernel IP routing table
  3. Destination Gateway Genmask Flags MSS Window irtt Iface
  4. link-local * 255.255.0.0 U 0 0 0 docker0
  5. 172.17.0.0 * 255.255.0.0 U 0 0 0 docker0

显示RAW网络统计

  1. netstat --statistics --raw
  2. Ip:
  3. 3046724 total packets received
  4. 20 forwarded
  5. 0 incoming packets discarded
  6. 3045898 incoming packets delivered
  7. 1985453 requests sent out
  8. 48 outgoing packets dropped
  9. 1611 dropped because of missing route
  10. Icmp:
  11. 474 ICMP messages received
  12. 0 input ICMP message failed.
  13. ICMP input histogram:
  14. destination unreachable: 471
  15. echo requests: 3
  16. 1999 ICMP messages sent
  17. 0 ICMP messages failed
  18. ICMP output histogram:
  19. destination unreachable: 1996
  20. echo replies: 3
  21. IcmpMsg:
  22. InType3: 471
  23. InType8: 3
  24. OutType0: 3
  25. OutType3: 1996
  26. UdpLite:
  27. IpExt:
  28. InNoRoutes: 38
  29. InMcastPkts: 143
  30. OutMcastPkts: 164
  31. InBcastPkts: 310
  32. OutBcastPkts: 6
  33. InOctets: 5251249872
  34. OutOctets: 149575262
  35. InMcastOctets: 26980
  36. OutMcastOctets: 27820
  37. InBcastOctets: 32799
  38. OutBcastOctets: 284
  39. InNoECTPkts: 3750022

(显示太占篇幅了…)

其他

  • netstat –-tcp –-numeric 列出本机的TCP连接
  • netstat —tcp —listening –-programs 显示系统正在监听的端口以及程序
  1. netstat --tcp --listening –-programs -n
  2. Active Internet connections (only servers)
  3. Proto Recv-Q Send-Q Local Address Foreign Address State
  4. tcp 0 0 127.0.0.1:44244 0.0.0.0:* LISTEN
  5. tcp 0 0 127.0.1.1:53 0.0.0.0:* LISTEN
  6. tcp 0 0 127.0.0.1:25 0.0.0.0:* LISTEN
  7. tcp 0 0 127.0.0.1:3306 0.0.0.0:* LISTEN
  8. tcp 0 0 127.0.0.1:587 0.0.0.0:* LISTEN
  9. tcp6 0 0 :::1080 :::* LISTEN
  10. tcp6 0 0 :::1088 :::* LISTEN
  • netstat –rnC 显示路由缓存