关于linux:Linux之netstat命令

6次阅读

共计 4499 个字符,预计需要花费 12 分钟才能阅读完成。

【Linux 常用命令速查手册】关注【入门小站】,后盾回复「1001」自取。

netstat 命令用于显示与 IP、TCP、UDP 和 ICMP 协定相干的统计数据,个别用于测验本机各端口的网络连接状况。netstat 是在内核中拜访网络及相干信息的程序,它能提供 TCP 连贯,TCP 和 UDP 监听,过程内存治理的相干报告。

TCP 连贯状态详解

  • LISTEN:侦听来自远方的 TCP 端口的连贯申请
  • SYN-SENT:再发送连贯申请后期待匹配的连贯申请
  • SYN-RECEIVED:再收到和发送一个连贯申请后期待对方对连贯申请的确认
  • ESTABLISHED:代表一个关上的连贯
  • FIN-WAIT-1:期待近程 TCP 连贯中断请求,或先前的连贯中断请求的确认
  • FIN-WAIT-2:从近程 TCP 期待连贯中断请求
  • CLOSE-WAIT:期待从本地用户发来的连贯中断请求
  • CLOSING:期待近程 TCP 对连贯中断的确认
  • LAST-ACK:期待原来的发向近程 TCP 的连贯中断请求的确认
  • TIME-WAIT:期待足够的工夫以确保近程 TCP 接管到连贯中断请求的确认
  • CLOSED:没有任何连贯状态

命令格局

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” 参数雷同。

列出所有端口 (蕴含 TCP 和 UDP)

> netstat -a

列出所有 TCP 端口

> netstat -at

列出所有 UDP 端口

> netstat -au

列出所有处于监听状态的 Sockets

> netstat -l

只列出所有监听 tcp 端口

> netstat -lt

只列出所有监听 udp 端口

> netstat -lu

只列出所有监听 UNIX 端口

> netstat -lx

显示所有端口的统计信息

> netstat -s
Ip:
    1007495197 total packets received
    0 forwarded
    582 with unknown protocol
    0 incoming packets discarded
    1007422115 incoming packets delivered
Icmp:
    66583265 ICMP messages received
    63899 input ICMP message failed.
    InCsumErrors: 384
    ICMP input histogram:
        destination unreachable: 115410
        timeout in transit: 12840
        source quenches: 11
        redirects: 563
        echo requests: 66453453
        echo replies: 152
        timestamp request: 438
        address mask request: 4
    66987000 ICMP messages sent
    0 ICMP messages failed
    ICMP output histogram:
        destination unreachable: 533106
        echo request: 4
        echo replies: 66453453
        timestamp replies: 437
IcmpMsg:
        InType0: 152
        InType3: 115410
        InType4: 11
        InType5: 563

IpExt:
    InNoRoutes: 15
    InMcastPkts: 1291307
    InOctets: 211446050816
    OutOctets: 481070069194

显示 TCP 端口的统计信息

> netstat -st
IcmpMsg:
    InType0: 152
    InType3: 115410
    InType4: 11
    InType5: 563
    InType8: 66453492
Tcp:
    55437641 active connections openings
    35899081 passive connection openings
    552243 failed connection attempts
    753118 connection resets received
    7 connections established
UdpLite:
TcpExt:
    162052 SYN cookies sent
    621 SYN cookies received
    4998179 invalid SYN cookies received
    551915 resets received for embryonic SYN_RECV sockets
IpExt:
    InNoRoutes: 15
    InMcastPkts: 1291308
    InOctets: 211446303015
    OutOctets: 481070459735
    InMcastOctets: 46487088

显示 UDP 端口的统计信息

> netstat -su
IcmpMsg:
    InType0: 152
    InType3: 115410
    InType4: 11
    InType5: 563
    InType8: 66453594
    InType11: 12840
    InType13: 438
    InType17: 4
    InType37: 3
    InType165: 7
    OutType0: 66453594
    OutType3: 533106
    OutType8: 4
    OutType14: 437
Udp:
    17941589 packets received
    637146 packets to unknown port received.
    1649 packet receive errors
    17977050 packets sent
    0 receive buffer errors
    0 send buffer errors
    InCsumErrors: 1640
UdpLite:
IpExt:
    InNoRoutes: 15
    InMcastPkts: 1291310
    InOctets: 211446802283
    OutOctets: 481071405083
    InMcastOctets: 46487160
    InNoECTPkts: 1009629627
    InECT1Pkts: 9955
    InECT0Pkts: 1987096
    InCEPkts: 94039

在 netstat 输入中显示 PID 和过程名称

> netstat -pt

在 netstat 输入中不显示主机,端口和用户名 (host, port or user)

> netstat -an

继续输入 netstat 信息

每隔一秒输入网络信息

> netstat -c

显示外围路由信息

> netstat -r

Kernel IP routing table
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.16.0.0      0.0.0.0         255.255.240.0   U         0 0          0 eth0

应用 netstat -rn 显示数字格局,不查问主机名称。

> netstat -rn

Kernel IP routing table
Destination     Gateway         Genmask         Flags   MSS Window  irtt Iface
0.0.0.0         172.16.0.1      0.0.0.0         UG        0 0          0 eth0
169.254.0.0     0.0.0.0         255.255.0.0     U         0 0          0 eth0
172.16.0.0      0.0.0.0         255.255.240.0   U         0 0          0 eth0

找出程序运行的端口

> netstat -tunlp | grep ssh

tcp        0      0 0.0.0.0:22              0.0.0.0:*               LISTEN      4400/sshd

找出运行在指定端口的过程

> netstat -an | grep ":80"
tcp        0      0 0.0.0.0:80              0.0.0.0:*               LISTEN
tcp        0      0 172.16.0.9:80           185.191.171.12:9380     TIME_WAIT
tcp        0      0 172.16.0.9:80           185.191.171.26:52418    TIME_WAIT
tcp        0   3450 172.16.0.9:80           185.191.171.37:25108    FIN_WAIT1
tcp        0      0 172.16.0.9:80           185.191.171.37:55096    TIME_WAIT
tcp        0      0 172.16.0.9:80           144.76.176.171:27832    TIME_WAIT
tcp        0      0 172.16.0.9:80           118.126.124.7:11127     TIME_WAIT
tcp        0      0 172.16.0.9:46628        169.254.0.55:8080       TIME_WAIT
tcp        0      0 172.16.0.9:80           144.76.176.171:23812    ESTABLISHED

通过端口找过程 ID

> netstat -tunlp | grep 80 | awk '{print $7}' | cut -d/ -f1
21323

显示网络接口列表

> 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
eth0             1450 462305568      0      0 0      276046945      0      0      0 BMRU
lo              65536  7204971      0      0 0       7204971      0      0      0 LRU
vpn_abi          1500 229612389      0 1023383 0      146640133      0    956      0 BMRU

统计 TCP 各种状态列表

> netstat -n | awk '/^tcp/{++S[$NF]}END{for(i in S) print i,S[i]}'
ESTABLISHED 7
FIN_WAIT2 13
TIME_WAIT 18

原文链接:https://rumenz.com/rumenbiji/…
微信公众号: 入门小站

【Linux 常用命令速查手册】关注【入门小站】,后盾回复「1001」自取。

正文完
 0