zoukankan      html  css  js  c++  java
  • 【Linux常见命令】netstat命令

    netstat - Print network connections, routing tables, interface statistics, masquerade connections, and multicast memberships

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

    netstat命令用于显示与IP、TCP、UDP和ICMP协议相关的统计数据,一般用于检验本机各端口的网络连接情况。

    netstat是在内核中访问网络及相关信息的程序,它能提供TCP连接,TCP和UDP监听,进程内存管理的相关报告。

    语法

    • netstat [address_family_options] [--tcp|-t] [--udp|-u] [--raw|-w] [--listening|-l] [--all|-a] [--numeric|-n] [--numeric-hosts][--numeric-ports][--numeric-ports] [--symbolic|-N] [--extend|-e[--extend|-e]] [--timers|-o] [--program|-p] [--verbose|-v] [--continuous|-c] [delay]
    • netstat {--route|-r} [address_family_options] [--extend|-e[--extend|-e]] [--verbose|-v] [--numeric|-n] [--numeric-hosts][--numeric-ports][--numeric-ports] [--continuous|-c] [delay]
    • netstat {--interfaces|-I|-i} [iface] [--all|-a] [--extend|-e] [--verbose|-v] [--program|-p] [--numeric|-n] [--numeric-hosts][--numeric-ports][--numeric-ports] [--continuous|-c] [delay]
    • netstat {--groups|-g} [--numeric|-n] [--numeric-hosts][--numeric-ports][--numeric-ports] [--continuous|-c] [delay]
    • netstat {--masquerade|-M} [--extend|-e] [--numeric|-n] [--numeric-hosts][--numeric-ports][--numeric-ports] [--continuous|-c] [delay]
    • netstat {--statistics|-s} [--tcp|-t] [--udp|-u] [--raw|-w] [delay]
    • netstat {--version|-V}
    • netstat {--help|-h}

    选项:address_family_options:

      [--protocol={inet,inet6,unix,ipx,ax25,netrom,ddp, ... } ] [--unix|-x]
      [--inet|--ip] [--ax25] [--ipx] [--netrom] [--ddp]

     

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

    参数:

    • -a, --all, --listening     display all sockets (default: connected) 显示所有连线中的socket
    • -A<网络类型>或-<网络类型> 列出该网络类型连线中的相关地址
    • -c或--continuous持续列出网络状态。
    • -C, --cache             display routing cache instead of FIB;显示路由器配置的快取信息。
    • -e, --extend    显示网络其它相关信息
    • -F或--fib  显示FIB  display Forwarding Information Base (default)
    • -g或--groups显示多重广播功能分组组成员列表。
    • -h或--help在线帮助。
    • -i或--i​​nterfaces  显示网络界面信息表单。
    • -l或--listening  显示监视中的服务器的套接字。
    • -M或--masquerade  显示伪装的网络连线。
    • -n或--numeric 直接使用IP地址,而不通过域名服务器。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或--netlink或--symbolic  显示网络硬件外围设备的符号连接名称。resolve hardware names
    • -o或--timers显示计时器。
    • -p或--programs显示正在使用套接字的程序识别码和程序名。
    • -r或--route显示路由表。
    • -s或--statistice显示网络工作信息统计表。
    • -t或--tcp显示TCP传输协议的连线状况。
    • -u或--udp显示UDP传输协议的连线状况。
    • -v或--verbose显示指令执行过程。
    • -V或--version显示版本信息。
    • -w或--raw显示RAW传输协议的连线状况。
    • -x或--unix此参数的效果和指定“ -A unix”参数相同。
    • --ip或--inet此参数的效果和指定“ -A inet”参数相同。
     

    netstat输出结果分析

    [root@oldboy ~]# netstat
    Active Internet connections (w/o servers)
    Proto Recv-Q Send-Q Local Address               Foreign Address             State      
    tcp        0      0 192.168.0.109:ssh           192.168.0.102:62939         ESTABLISHED 
    Active UNIX domain sockets (w/o servers)
    Proto RefCnt Flags       Type       State         I-Node Path
    unix  5      [ ]         DGRAM                    9304   /dev/log
    unix  2      [ ]         DGRAM                    7486   @/org/kernel/udev/udevd
    unix  2      [ ]         DGRAM                    12160  
    unix  2      [ ]         DGRAM                    10461  
    unix  2      [ ]         DGRAM                    9844   
    unix  3      [ ]         DGRAM                    7504   
    unix  3      [ ]         DGRAM                    7503  

    说明:

    从整体上看,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:没有任何连接状态


    常用的命令显示
    1. 显示网卡列表 netstat -i

    [root@oldboy ~]# netstat -i
    Kernel Interface table
    Iface       MTU Met    RX-OK RX-ERR RX-DRP RX-OVR    TX-OK TX-ERR TX-DRP TX-OVR Flg
    eth0       1500   0        1      0      0      0        9      0      0      0 BMRU
    eth3       1500   0     4418      0      0      0     2176      0      0      0 BMRU
    lo        65536   0        0      0      0      0        0      0      0      0 LRU
    

    2. 显示组播组的关系 netstat -g

    [root@oldboy ~]# netstat -g
    IPv6/IPv4 Group Memberships
    Interface       RefCnt Group
    --------------- ------ ---------------------
    lo              1      all-systems.mcast.net
    eth0            1      all-systems.mcast.net
    eth3            1      all-systems.mcast.net
    lo              1      ff02::1
    eth0            1      ff02::1:ff10:a556
    eth0            1      ff02::1
    eth3            1      ff02::1:ff4c:6fad
    eth3            1      ff02::1
    

    3. 显示网络统计  netstat -s

    [root@oldboy ~]# netstat -s
    Ip:
        1874 total packets received
        5 with invalid addresses
        0 forwarded
        0 incoming packets discarded
        1867 incoming packets delivered
        1629 requests sent out
    Icmp:
        0 ICMP messages received
        0 input ICMP message failed.
        ICMP input histogram:
        1 ICMP messages sent
        0 ICMP messages failed
        ICMP output histogram:
            destination unreachable: 1
    IcmpMsg:
            OutType3: 1
    Tcp:
        0 active connections openings
        1 passive connection openings
        0 failed connection attempts
        0 connection resets received
        1 connections established
        351 segments received
        195 segments send out
        0 segments retransmited
        0 bad segments received.
        1 resets sent
    Udp:
        461 packets received
        1 packets to unknown port received.
        0 packet receive errors
        1433 packets sent
    UdpLite:
    TcpExt:
        2 packets header predicted
        179 acknowledgments not containing data received
        9 predicted acknowledgments
        0 TCP data loss events
    IpExt:
        InBcastPkts: 1062
        InOctets: 242307
        OutOctets: 392233
        InBcastOctets: 155961

    4. 常用组合:netstat -lntup

    说明:-l,listening;-n, num; -t, tcp, -u, udp, -p, process

    [root@oldboy ~]# netstat -lntup
    Active Internet connections (only servers)
    Proto Recv-Q Send-Q Local Address               Foreign Address             State       PID/Program name   
    tcp        0      0 0.0.0.0:3306                0.0.0.0:*                   LISTEN      1400/mysqld         
    tcp        0      0 0.0.0.0:22                  0.0.0.0:*                   LISTEN      1261/sshd           
    tcp        0      0 :::22                       :::*                        LISTEN      1261/sshd           
    udp        0      0 0.0.0.0:68                  0.0.0.0:*                               1185/dhclient       
    udp        0      0 0.0.0.0:68                  0.0.0.0:*                               965/dhclient   

    5. 显示关于以太网的统计数据netstat -e

    说明:

    用于显示关于以太网的统计数据。它列出的项目包括传送的数据报的总字节数、错误数、删除数、数据报的数量和广播的数量。这些统计数据既有发送的数据报数量,也有接收的数据报数量。这个选项可以用来统计一些基本的网络流量)
    [root@oldboy ~]# netstat -e
    Active Internet connections (w/o servers)
    Proto Recv-Q Send-Q Local Address               Foreign Address             State       User       Inode     
    tcp        0      0 192.168.0.109:ssh           192.168.0.102:62939         ESTABLISHED root       10414      
    Active UNIX domain sockets (w/o servers)
    Proto RefCnt Flags       Type       State         I-Node Path
    unix  5      [ ]         DGRAM                    9304   /dev/log
    unix  2      [ ]         DGRAM                    7486   @/org/kernel/udev/udevd
    unix  2      [ ]         DGRAM                    12160  
    unix  2      [ ]         DGRAM                    10461  
    unix  2      [ ]         DGRAM                    9844   
    unix  3      [ ]         DGRAM                    7504   
    unix  3      [ ]         DGRAM                    7503 

    6. 显示路由信息 netstat -r

    等价命令route -n 显示路由信息

    [root@oldboy ~]# netstat -r
    Kernel IP routing table
    Destination     Gateway         Genmask         Flags   MSS Window  irtt Iface
    10.0.2.0        *               255.255.255.0   U         0 0          0 eth0
    192.168.0.0     *               255.255.255.0   U         0 0          0 eth3
    link-local      *               255.255.0.0     U         0 0          0 eth0
    link-local      *               255.255.0.0     U         0 0          0 eth3
    default         192.168.0.1     0.0.0.0         UG        0 0          0 eth3

    7. 统计机器中网络连接各个状态个数

    [root@oldboy ~]# netstat -an|grep tcp
    tcp        0      0 0.0.0.0:3306                0.0.0.0:*                   LISTEN      
    tcp        0      0 0.0.0.0:22                  0.0.0.0:*                   LISTEN      
    tcp        0      0 192.168.0.109:22            192.168.0.102:62939         ESTABLISHED 
    tcp        0      0 :::22                       :::*                        LISTEN      
    [root@oldboy ~]# netstat -an | awk '/^tcp/ {++S[$NF]}  END {for (a in S) print a,S[a]} '
    ESTABLISHED 1
    LISTEN 3

    把状态全都取出来后使用uniq -c统计后再进行排序

    [root@oldboy ~]# netstat -ant
    Active Internet connections (servers and established)
    Proto Recv-Q Send-Q Local Address               Foreign Address             State      
    tcp        0      0 0.0.0.0:3306                0.0.0.0:*                   LISTEN      
    tcp        0      0 0.0.0.0:22                  0.0.0.0:*                   LISTEN      
    tcp        0      0 192.168.0.109:22            192.168.0.102:62939         ESTABLISHED 
    tcp        0      0 :::22                       :::*                        LISTEN      
    [root@oldboy ~]# netstat -ant|awk '{print $6}' 
    established)
    Foreign
    LISTEN
    LISTEN
    ESTABLISHED
    LISTEN
    [root@oldboy ~]# netstat -ant|awk '{print $6}'|sort|uniq -c
          1 ESTABLISHED
          1 Foreign
          3 LISTEN
          1 established)
     
  • 相关阅读:
    Mybatis多表查询
    (转)Java安全通信:HTTPS与SSL
    (转)RSA加密解密及数字签名Java实现
    (转)大型企业电话会议视频会议备份解决方案
    (转)虚拟IP原理
    虚拟IP---Linux下一个网卡配置多个IP
    C++ 点
    算法(8)Maximum Product Subarray
    算法(7)Majority Element II
    算法(6)3Sum Closest
  • 原文地址:https://www.cnblogs.com/zoe233/p/11907344.html
Copyright © 2011-2022 走看看