zoukankan      html  css  js  c++  java
  • Linux netstat命令详解(检验本机各端口的网络连接情况)

     

       netstat命令用于显示与IP、TCP、UDP和ICMP协议相关的统计数据,一般用于检验本机各端口的网络连接情况。netstat是在内核中访问网络及相关信息的程序,它能提供TCP连接,TCP和UDP监听,进程内存管理的相关报告。

    netstat常用命令参数

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    usage: netstat [-veenNcCF] [<Af>] -r         netstat {-V|--version|-h|--help}
           netstat [-vnNcaeol] [<Socket> ...]
           netstat { [-veenNac] -I[<Iface>] | [-veenNac] -i | [-cnNe] -M | -s } [delay]
     
            -r, --route                display routing table
            -I, --interfaces=<Iface>   display interface table for <Iface>
            -i, --interfaces           display interface table
            -g, --groups               display multicast group memberships
            -s, --statistics           display networking statistics (like SNMP)
            -M, --masquerade           display masqueraded connections
     
            -v, --verbose              be verbose
            -n, --numeric              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, --symbolic             resolve hardware names
            -e, --extend               display other/more information
            -p, --programs             display PID/Program name for sockets
            -c, --continuous           continuous listing
     
            -l, --listening            display listening server sockets
            -a, --all, --listening     display all sockets (default: connected)
            -o, --timers               display timers
            -F, --fib                  display Forwarding Information Base (default)
            -C, --cache                display routing cache instead of FIB
            -T, --notrim               stop trimming long addresses
            -Z, --context              display SELinux security context for sockets
    1
    2
    3
    4
    5
    <Iface>: Name of interface to monitor/list.
    <Socket>={-t|--tcp} {-u|--udp} {-S|--sctp} {-w|--raw} {-x|--unix} --ax25 --ipx --netrom
    <AF>=Use '-A <af>' or '--<af>'; default: inet
    List of possible address families (which support routing):
    inet (DARPA Internet) inet6 (IPv6) ax25 (AMPR AX.25)

    netstat输出结果分析

    image

    说明:

    从整体上看,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

    image

    显示组播组的关系

    1
    netstat –g

    image

    显示网络统计

    1
    netstat -s

    image

    常用组合:

    1
    2
    netstat -lntup 
    说明: l:listening   n:num   t:tcp  u:udp  p:process

    image

    显示关于以太网的统计数据

    1
    2
    3
    4
    netstat –e
     
    说明:
    用于显示关于以太网的统计数据。它列出的项目包括传送的数据报的总字节数、错误数、删除数、数据报的数量和广播的数量。这些统计数据既有发送的数据报数量,也有接收的数据报数量。这个选项可以用来统计一些基本的网络流量)

    image

    显示路由信息

    1
    netstat –r
    1
    route –n   【也可以显示路由信息】

    image

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

    1
    netstat -an | awk '/^tcp/ {++S[$NF]}  END {for (a in S) print a,S[a]} '

    image

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

    1
    netstat -ant|awk '{print $6}'|sort|uniq –c

    image

    查看连接某服务端口最多的的IP地址

    1
    netstat -ant|grep "192.168.25.*"|awk '{print $5}'|awk -F: '{print $1}'|sort -nr|uniq –c

    image

    找出程序运行的端口

    1
    netstat -ap | grep ssh

    image

    在 netstat 输出中显示 TCP连接信息

    netstat -pt

    image

    说明:netstat -p 可以与其它开关一起使用,就可以添加 “PID/进程名称”

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

    其他注意事项:

    image

    小技巧: netstat -lnt | grep ssh | wc -l -->只要大于2个,ssh服务就是好的

  • 相关阅读:
    springboot 全局异常拦截器,友好异常提示
    java Word 转 PDF格式
    SpringCloud GateWay 使用 网关路由
    spring boot 读写参数到session
    springboot2.5.2 整合 flowable6.6.0 流程引擎
    java 自定义表单 动态表单 表单设计器 工作流引擎 flowable 项目源码
    springboot 运行 jar 包读取外部配置文件
    Spring Boot mybatisconfig 和 log4j 输出sql 日志
    SQL 数据查询语句之字符串拆分
    Enable Script debugging in IE8 at Window 7 operating system
  • 原文地址:https://www.cnblogs.com/liuys635/p/11340661.html
Copyright © 2011-2022 走看看