zoukankan      html  css  js  c++  java
  • netstat

    一:netstat

    netstat -tunlp 用于显示 tcp,udp 的端口和进程等相关情况。

    netstat 查看端口占用语法格式:

    netstat -tunlp | grep 端口号
    • -t (tcp) 仅显示tcp相关选项
    • -u (udp)仅显示udp相关选项
    • -n 拒绝显示别名,能显示数字的全部转化为数字
    • -l 仅列出在Listen(监听)的服务状态
    • -p 显示建立相关链接的程序名
    • -a (all)显示所有选项,默认不显示LISTEN相关
    • r 显示路由信息,路由表
    • e 显示扩展信息,例如uid等
    • s 按各个协议进行统计
    • c 每隔一个固定时间,执行该netstat命令

    提示:LISTEN和LISTENING的状态只有用-a或者-l才能看到

    二:列出所有端口 (包括监听和未监听的)

    1:列出所有端口 netstat -a

    1.  
      # netstat -a | more
    2.  
      Active Internet connections (servers and established)
    3.  
      Proto Recv-Q Send-Q Local Address Foreign Address State
    4.  
      tcp 0 0 localhost:30037 *:* LISTEN
    5.  
      udp 0 0 *:bootpc *:*
    6.  
       
    7.  
      Active UNIX domain sockets (servers and established)
    8.  
      Proto RefCnt Flags Type State I-Node Path
    9.  
      unix 2 [ ACC ] STREAM LISTENING 6135 /tmp/.X11-unix/X0
    10.  
      unix 2 [ ACC ] STREAM LISTENING 5140 /var/run/acpid.socket

    2:列出所有 tcp 端口 netstat -at 

    1.  
      # netstat -at
    2.  
      Active Internet connections (servers and established)
    3.  
      Proto Recv-Q Send-Q Local Address Foreign Address State
    4.  
      tcp 0 0 localhost:30037 *:* LISTEN
    5.  
      tcp 0 0 localhost:ipp *:* LISTEN
    6.  
      tcp 0 0 *:smtp *:* LISTEN
    7.  
      tcp6 0 0 localhost:ipp [::]:* LISTEN

    3: 列出所有 udp 端口 netstat -au

    1.  
      # netstat -au
    2.  
      Active Internet connections (servers and established)
    3.  
      Proto Recv-Q Send-Q Local Address Foreign Address State
    4.  
      udp 0 0 *:bootpc *:*
    5.  
      udp 0 0 *:49119 *:*
    6.  
      udp 0 0 *:mdns *:*

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

    1:只显示监听端口 netstat -l

    1.  
      # netstat -l
    2.  
      Active Internet connections (only servers)
    3.  
      Proto Recv-Q Send-Q Local Address Foreign Address State
    4.  
      tcp 0 0 localhost:ipp *:* LISTEN
    5.  
      tcp6 0 0 localhost:ipp [::]:* LISTEN
    6.  
      udp 0 0 *:49119 *:*

    2:只列出所有监听 tcp 端口 netstat -lt

    1.  
      # netstat -lt
    2.  
      Active Internet connections (only servers)
    3.  
      Proto Recv-Q Send-Q Local Address Foreign Address State
    4.  
      tcp 0 0 localhost:30037 *:* LISTEN
    5.  
      tcp 0 0 *:smtp *:* LISTEN
    6.  
      tcp6 0 0 localhost:ipp [::]:* LISTEN

    3:只列出所有监听 udp 端口 netstat -lu

    1.  
      # netstat -lu
    2.  
      Active Internet connections (only servers)
    3.  
      Proto Recv-Q Send-Q Local Address Foreign Address State
    4.  
      udp 0 0 *:49119 *:*
    5.  
      udp 0 0 *:mdns *:*

    4:只列出所有监听 UNIX 端口 netstat -lx

    1.  
      # netstat -lx
    2.  
      Active UNIX domain sockets (only servers)
    3.  
      Proto RefCnt Flags Type State I-Node Path
    4.  
      unix 2 [ ACC ] STREAM LISTENING 6294 private/maildrop
    5.  
      unix 2 [ ACC ] STREAM LISTENING 6203 public/cleanup
    6.  
      unix 2 [ ACC ] STREAM LISTENING 6302 private/ifmail
    7.  
      unix 2 [ ACC ] STREAM LISTENING 6306 private/bsmtp

    5:在 netstat 输出中显示 PID 和进程名称 netstat -p

    netstat -p 可以与其它开关一起使用,就可以添加 “PID/进程名称” 到 netstat 输出中,这样 debugging 的时候可以很方便的发现特定端口运行的程序。

    1.  
      # netstat -pt
    2.  
      Active Internet connections (w/o servers)
    3.  
      Proto Recv-Q Send-Q Local Address Foreign Address State PID/Program name
    4.  
      tcp 1 0 ramesh-laptop.loc:47212 192.168.185.75:www CLOSE_WAIT 2109/firefox
    5.  
      tcp 0 0 ramesh-laptop.loc:52750 lax:www ESTABLISHED 2109/firefox

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

    当你不想让主机,端口和用户名显示,使用 netstat -n。将会使用数字代替那些名称。

    同样可以加速输出,因为不用进行比对查询。

    # netstat -an

    7:持续输出 netstat 信息

    netstat 将每隔一秒输出网络信息。

    1.  
      # netstat -c
    2.  
      Active Internet connections (w/o servers)
    3.  
      Proto Recv-Q Send-Q Local Address Foreign Address State
    4.  
      tcp 0 0 ramesh-laptop.loc:36130 101-101-181-225.ama:www ESTABLISHED
    5.  
      tcp 1 1 ramesh-laptop.loc:52564 101.11.169.230:www CLOSING
    6.  
      tcp 0 0 ramesh-laptop.loc:43758 server-101-101-43-2:www ESTABLISHED
    7.  
      tcp 1 1 ramesh-laptop.loc:42367 101.101.34.101:www CLOSING
    8.  
      ^C

    8:显示系统不支持的地址族 (Address Families)

    1.  
      netstat --verbose
    2.  
       
    3.  
      netstat: no support for `AF IPX' on this system.
    4.  
      netstat: no support for `AF AX25' on this system.
    5.  
      netstat: no support for `AF X25' on this system.
    6.  
      netstat: no support for `AF NETROM' on this system.

    9:显示核心路由信息 netstat -r

    1.  
      # netstat -r
    2.  
      Kernel IP routing table
    3.  
      Destination Gateway Genmask Flags MSS Window irtt Iface
    4.  
      192.168.1.0 * 255.255.255.0 U 0 0 0 eth2
    5.  
      link-local * 255.255.0.0 U 0 0 0 eth2
    6.  
      default 192.168.1.1 0.0.0.0 UG 0 0 0 eth2

    注意: 使用 netstat -rn 显示数字格式,不查询主机名称。

    10:找出程序运行的端口

    并不是所有的进程都能找到,没有权限的会不显示,使用 root 权限查看所有的信息。

    1.  
      # netstat -ap | grep ssh
    2.  
      tcp 1 0 dev-db:ssh 101.174.100.22:39213 CLOSE_WAIT -
    3.  
      tcp 1 0 dev-db:ssh 101.174.100.22:57643 CLOSE_WAIT -

    11:显示网络接口列表

    1.  
      # netstat -i
    2.  
      Kernel Interface table
    3.  
      Iface MTU Met RX-OK RX-ERR RX-DRP RX-OVR TX-OK TX-ERR TX-DRP TX-OVR Flg
    4.  
      eth0 1500 0 0 0 0 0 0 0 0 0 BMU
    5.  
      eth2 1500 0 26196 0 0 0 26883 6 0 0 BMRU
    6.  
      lo 16436 0 4 0 0 0 4 0 0 0 LRU

    显示详细信息,像是 ifconfig 使用 netstat -ie:

    1.  
      # netstat -ie
    2.  
      Kernel Interface table
    3.  
      eth0 Link encap:Ethernet HWaddr 00:10:40:11:11:11
    4.  
      UP BROADCAST MULTICAST MTU:1500 Metric:1
    5.  
      RX packets:0 errors:0 dropped:0 overruns:0 frame:0
    6.  
      TX packets:0 errors:0 dropped:0 overruns:0 carrier:0
    7.  
      collisions:0 txqueuelen:1000
    8.  
      RX bytes:0 (0.0 B) TX bytes:0 (0.0 B)
    9.  
      Memory:f6ae0000-f6b00000

    四:IP和TCP实际应用案例分析

    第一、查看连接某服务最多的的IP地址

    1.  
      [root@izwz99z5o9dc90keftqhlrz ~]# netstat -nat |awk '{print $5}'|awk -F: '{print $1}'|sort|uniq -c|sort -nr|head -20
    2.  
       
    3.  
      11 120.227.29.121
    4.  
      7
    5.  
      4 0.0.0.0
    6.  
      2 47.106.203.79
    7.  
      1 and
    8.  
      1 Address
    9.  
      1 163.177.83.164
    10.  
      1 100.100.30.25

    第二、查看连接指定服务器端口6379最多的的IP地址

    1.  
      [root@izwz99z5o9dc90keftqhlrz ~]# netstat -nat | grep "47.106.203.79:6379" |awk '{print $5}'|awk -F: '{print $1}'|sort|uniq -c|sort -nr|head -20
    2.  
       
    3.  
      1 47.106.203.79

    第三、查看80端口的连接数

    1.  
      [root@izwz99z5o9dc90keftqhlrz ~]# netstat -nat|grep -i "80"|wc -l
    2.  
      7

    第四、连接的IP数量进行排序

    1.  
      [root@izwz99z5o9dc90keftqhlrz ~]# netstat -ntu | awk '{print $5}' | cut -d: -f1 | sort | uniq -c | sort -n
    2.  
       
    3.  
      1 100.100.30.25
    4.  
      1 120.227.29.121
    5.  
      1 163.177.83.164
    6.  
      1 Address
    7.  
      1 servers)
    8.  
      2 120.41.45.100
    9.  
      2 47.106.203.79

    第五、TCP连接状况

    1.  
      netstat -nat |awk '{print $6}'|sort|uniq -c|sort -rn
    2.  
      netstat -n | awk '/^tcp/ {++S[$NF]};END {for(a in S) print a, S[a]}'
    3.  
      netstat -n | awk '/^tcp/ {++state[$NF]}; END {for(key in state) print key," ",state[key]}'
    4.  
      netstat -n | awk '/^tcp/ {++arr[$NF]};END {for(k in arr) print k," ",arr[k]}'
    5.  
      netstat -n |awk '/^tcp/ {print $NF}'|sort|uniq -c|sort -rn
    6.  
      netstat -ant | awk '{print $NF}' | grep -v '[a-z]' | sort | uniq -c
    7.  
       
    8.  
      如:
    9.  
      [root@izwz99z5o9dc90keftqhlrz ~]# netstat -nat |awk '{print $6}'|sort|uniq -c|sort -rn
    10.  
      11 LISTEN
    11.  
      4 ESTABLISHED
    12.  
      1 TIME_WAIT
    13.  
      1 Foreign
    14.  
      1 established)
    15.  
      1 CLOSE_WAIT

    第六、占用80端口最多的20个IP

    1.  
      [root@izwz99z5o9dc90keftqhlrz ~]# netstat -anlp|grep 80|grep tcp|awk '{print $5}'|awk -F: '{print $1}'|sort|uniq -c|sort -nr|head -n20
    2.  
      3 120.41.45.100
    3.  
      3
    4.  
      1 163.177.83.164
    5.  
      1 100.100.30.25
    6.  
      1 0.0.0.0

    第七、查看time_wait较多的连接

    1.  
      [root@izwz99z5o9dc90keftqhlrz ~]# netstat -n|grep TIME_WAIT|awk '{print $5}'|sort|uniq -c|sort -rn|head -n20
    2.  
      1 120.41.45.100:80

    第八、查看SYN连接数

    1.  
      [root@izwz99z5o9dc90keftqhlrz ~]# netstat -an | grep SYN | awk '{print $5}' | awk -F: '{print $1}' | sort | uniq -c | sort -nr | more
    2.  
  • 相关阅读:
    Lua大整数的实现
    std::allocator在stl容器中使用问题
    深度学习框架安装
    Tensorflow安装使用一段时间后,import时出现错误:ImportError: DLL load failed
    论文解读:SIFA
    多位微软MVP推荐,第一本ASP.NET Core 3.1的书来了
    ASP.NET Core 进程内与进程外的性能对比
    基于Netty的程序主动发送消息
    dbroot文件结构解析(一)
    qtree文件结构解析(二)
  • 原文地址:https://www.cnblogs.com/igoodful/p/13743230.html
Copyright © 2011-2022 走看看