• Linux系统使用命令来查看本地端口的使用情况


    Linux系统使用命令来查看本地端口的使用情况

    1. netstat 命令:

      netstat 是一个显示网络连接、路由表、接口统计信息等的工具。要查看端口使用情况,可以使用以下命令:

      netstat -tunlp
      
      • 1

      这里,选项的含义如下:

      • -t 表示显示TCP端口;

      • -u 表示显示UDP端口;

      • -n 表示直接使用数字地址和端口;

      • -l 表示显示监听中的服务;

      • -p 表示显示进程ID和进程名称。
        输出结果中,你会看到如下列信息:

      • Proto:协议(TCP或UDP);

      • Recv-Q:接收队列,表示收到的但还未被应用进程读取的字节数;

      • Send-Q:发送队列,表示发送但未收到确认的字节数;

      • Local Address:本地地址和端口;

      • Foreign Address:远程地址和端口;

      • State:连接状态;

      • PID/Program name:进程ID和程序名称。

        (sdk) (python) -bash-5.1# netstat -tunlp
        Active Internet connections (only servers)
        Proto Recv-Q Send-Q Local Address           Foreign Address         State       PID/Program name
        tcp        0      0 127.0.0.1:9875          0.0.0.0:*               LISTEN      2068851/xxxserver
        tcp        0      0 127.0.0.1:9876          0.0.0.0:*               LISTEN      2068851/xxxserver
        tcp        0      0 0.0.0.0:9877            0.0.0.0:*               LISTEN      35455/nginx: master
        tcp        0      0 127.0.0.1:9879          0.0.0.0:*               LISTEN      2068851/xxxserver
        tcp        0      0 127.0.0.1:9880          0.0.0.0:*               LISTEN      2068851/xxxserver
        tcp6       0      0 :::9878                 :::*                    LISTEN      2068851/xxxserver
        tcp6       0      0 10.38.174.68:6438       :::*                    LISTEN      3446840/xxxAnal
        
        • 1
        • 2
        • 3
        • 4
        • 5
        • 6
        • 7
        • 8
        • 9
        • 10
    2. ss 命令:

      ss 是一个工具,用于获取socket统计信息,它可以显示类似 netstat 的信息,但通常比 netstat 更快。查看端口使用情况的命令如下:

      ss -tunlp
      
      • 1

      输出结果中,你会看到如下列信息:

      • State:连接状态;

      • Recv-Q:接收队列;

      • Send-Q:发送队列;

      • Local Address:Port:本地地址和端口;

      • Peer Address:Port:对端地址和端口;

      • PID/Program name:进程ID和程序名称。

        (sdk) (python) -bash-5.1# ss -tunlp
        Netid       State        Recv-Q       Send-Q                     Local Address:Port              Peer Address:Port       Process
        tcp         LISTEN       0            128                            127.0.0.1:9875                   0.0.0.0:*           users:(("xxxserver",pid=2068851,fd=20))
        tcp         LISTEN       0            128                            127.0.0.1:9876                   0.0.0.0:*           users:(("xxxserver",pid=2068851,fd=11))
        tcp         LISTEN       0            511                              0.0.0.0:9877                   0.0.0.0:*           users:(("nginx",pid=35456,fd=8),("nginx",pid=35455,fd=8))
        tcp         LISTEN       0            128                            127.0.0.1:9879                   0.0.0.0:*           users:(("xxxserver",pid=2068851,fd=12))
        tcp         LISTEN       0            128                            127.0.0.1:9880                   0.0.0.0:*           users:(("xxxserver",pid=2068851,fd=13))
        tcp         LISTEN       0            4096                                   *:9878                         *:*           users:(("xxxserver",pid=2068851,fd=21))
        tcp         LISTEN       0            4096               [::ffff:10.38.174.68]:6438                         *:*           users:(("aaaserver",pid=3446840,fd=13))
        
        • 1
        • 2
        • 3
        • 4
        • 5
        • 6
        • 7
        • 8
        • 9
    3. lsof 命令:

      lsof 是一个列出当前系统打开文件的工具,但也可以用来查看端口使用情况。使用如下命令:

      lsof -i
      
      • 1

      或者,如果你想要查看特定端口的详细信息,可以使用:

      lsof -i :port
      
      • 1

      输出结果中,你会看到如下列信息:

      • COMMAND:进程名称;

      • PID:进程ID;

      • USER:进程所有者;

      • FD:文件描述符;

      • TYPE:文件类型(如IPv4、IPv6);

      • DEVICE:设备;

      • SIZE/OFF:大小/偏移量;

      • NODE:节点(通常是文件的inode号);

      • NAME:文件名称,这里会显示端口号和状态。

        (sdk) (python) -bash-5.1# lsof -i
        COMMAND       PID         USER   FD   TYPE    DEVICE SIZE/OFF NODE NAME
        xxxserver 2068851         root   12u  IPv4  62524614      0t0  TCP localhost:9879 (LISTEN)
        xxxserver 2068851         root   13u  IPv4  62524616      0t0  TCP localhost:9880 (LISTEN)
        xxxserver 2068851         root   21u  IPv6  62507639      0t0  TCP *:9878 (LISTEN)
        aaaaaaaAn 3446840 intelligence   13u  IPv6  31669590      0t0  TCP localhost.localdomain:6438 (LISTEN)
        
        • 1
        • 2
        • 3
        • 4
        • 5
        • 6
  • 相关阅读:
    MVSNet服务器环境配置及测试
    C++:CRTP(Curiously Recurring Template Pattern 奇异递归模板)
    C++提高编程
    TailwindCss Functions & Directives
    CAS源码工程搭建记录
    【linux】物理磁盘挂载目录——(分区、格式化、重启自动挂载)
    ubuntu永久修改mac地址
    教你如何使用API接口获取数据
    【解决】自定义conda环境安装位置,三种解决方法
    空间地理数据可视化之 tmap 包及其拓展
  • 原文地址:https://blog.csdn.net/Jackie_Ryan/article/details/138171903