linux查看程序占用的本地端口
ss是Socket Statistics的缩写,用来替代旧的netstat工具,功能更强大,执行更快。它用于查看系统的网络连接情况,包括TCP、UDP等协议的信息。
一. 命令解析:
sudo ss -tulwnp
ss (Socket Statistics):替代 netstat 的工具,用于查看网络连接、路由表、接口统计等。
 -t:显示 TCP 套接字。
 -u:显示 UDP 套接字。
 -l:仅显示监听(LISTEN)状态的套接字。
 -w:显示 RAW 套接字(较少使用)。
 -n:禁用域名解析,直接显示 IP 和端口号。
 -p:显示关联的进程信息(需 sudo 权限)。
 sudo:提升权限以查看所有进程的网络信息。
二. 输出结果
Netid State  Recv-Q Send-Q                                  Local Address:Port         Peer Address:Port      Process                                           
icmp6 UNCONN 0      0                                                   *:58                      *:*          users:(("NetworkManager",pid=1230,fd=21))        
udp   UNCONN 0      0                                       127.0.0.53%lo:53                0.0.0.0:*          users:(("systemd-resolve",pid=1161,fd=12))       
udp   UNCONN 0      0                                10.68.133.100%wlp2s0:33001             0.0.0.0:*          users:(("RayLinkService",pid=5084,fd=41))        
udp   UNCONN 0      0                          172.19.0.1%br-67610ac5d589:33001             0.0.0.0:*          users:(("RayLinkService",pid=5084,fd=40))        
udp   UNCONN 0      0                          172.18.0.1%br-7f68d848895d:33001             0.0.0.0:*          users:(("RayLinkService",pid=5084,fd=39))        
udp   UNCONN 0      0                                  172.17.0.1%docker0:33001             0.0.0.0:*          users:(("RayLinkService",pid=5084,fd=38))        
udp   UNCONN 0      0                                           127.0.0.1:33001             0.0.0.0:*          users:(("RayLinkService",pid=5084,fd=53))        
udp   UNCONN 0      0                                             0.0.0.0:33785             0.0.0.0:*          users:(("avahi-daemon",pid=1220,fd=14))          
udp   UNCONN 0      0                                         224.0.0.251:5353              0.0.0.0:*          users:(("chrome",pid=6862,fd=85))                
udp   UNCONN 0      0                                         224.0.0.251:5353              0.0.0.0:*          users:(("chrome",pid=6913,fd=53))                
udp   UNCONN 0      0                                             0.0.0.0:5353              0.0.0.0:*          users:(("avahi-daemon",pid=1220,fd=12))          
udp   UNCONN 0      0                                10.68.133.100%wlp2s0:38828             0.0.0.0:*          users:(("RayLinkService",pid=5084,fd=37))        
udp   UNCONN 0      0                          172.19.0.1%br-67610ac5d589:38828             0.0.0.0:*          users:(("RayLinkService",pid=5084,fd=36))        
udp   UNCONN 0      0                          172.18.0.1%br-7f68d848895d:38828             0.0.0.0:*          users:(("RayLinkService",pid=5084,fd=35))        
udp   UNCONN 0      0                                  172.17.0.1%docker0:38828             0.0.0.0:*          users:(("RayLinkService",pid=5084,fd=34))        
udp   UNCONN 0      0                                10.68.133.100%wlp2s0:44616             0.0.0.0:*          users:(("RayLinkService",pid=5084,fd=33))        
udp   UNCONN 0      0                          172.19.0.1%br-67610ac5d589:44616             0.0.0.0:*          users:(("RayLinkService",pid=5084,fd=32))        
udp   UNCONN 0      0                          172.18.0.1%br-7f68d848895d:44616             0.0.0.0:*          users:(("RayLinkService",pid=5084,fd=31))        
udp   UNCONN 0      0                                  172.17.0.1%docker0:44616             0.0.0.0:*          users:(("RayLinkService",pid=5084,fd=26))        
udp   UNCONN 0      0                                           127.0.0.1:19081             0.0.0.0:*          users:(("nautilus",pid=1943297,fd=25))           
udp   UNCONN 0      0                                                [::]:60031                [::]:*          users:(("avahi-daemon",pid=1220,fd=15))          
udp   UNCONN 0      0       [2001:da8:b801:4c:275f:65a0:4a02:cf69]%wlp2s0:33001                [::]:*          users:(("RayLinkService",pid=5084,fd=61))        
udp   UNCONN 0      0                  [fe80::2774:a098:7839:13c2]%wlp2s0:546                  [::]:*          users:(("NetworkManager",pid=1230,fd=24))        
udp   UNCONN 0      0                                                [::]:5353                 [::]:*          users:(("avahi-daemon",pid=1220,fd=13))          
udp   UNCONN 0      0       [2001:da8:b801:4c:275f:65a0:4a02:cf69]%wlp2s0:38828                [::]:*          users:(("RayLinkService",pid=5084,fd=43))        
udp   UNCONN 0      0       [2001:da8:b801:4c:275f:65a0:4a02:cf69]%wlp2s0:44616                [::]:*          users:(("RayLinkService",pid=5084,fd=42))        
tcp   LISTEN 0      128                                         127.0.0.1:55831             0.0.0.0:*          users:(("RayLinkService",pid=5095,fd=8))         
tcp   LISTEN 0      5                                           127.0.0.1:23119             0.0.0.0:*          users:(("zotero-bin",pid=2927689,fd=103))        
tcp   LISTEN 0      16                                          127.0.0.1:19645             0.0.0.0:*          users:(("python3",pid=2110,fd=11))               
tcp   LISTEN 0      128                                         127.0.0.1:51321             0.0.0.0:*          users:(("RayLinkService",pid=5084,fd=6))         
tcp   LISTEN 0      4096                                    127.0.0.53%lo:53                0.0.0.0:*          users:(("systemd-resolve",pid=1161,fd=13))       
tcp   LISTEN 0      100                                         127.0.0.1:5151              0.0.0.0:*          users:(("python",pid=1935176,fd=7))              
tcp   LISTEN 0      128                                         127.0.0.1:36899             0.0.0.0:*          users:(("RayLinkCapturer",pid=5093,fd=9))        
tcp   LISTEN 0      10                                          127.0.0.1:4301              0.0.0.0:*          users:(("qq",pid=2521049,fd=160))                
tcp   LISTEN 0      10                                          127.0.0.1:4310              0.0.0.0:*          users:(("qq",pid=2521049,fd=170))                
tcp   LISTEN 0      128                                         127.0.0.1:33691             0.0.0.0:*          users:(("RayLinkCapturer",pid=5091,fd=9))        
tcp   LISTEN 0      5                                           127.0.0.1:631               0.0.0.0:*          users:(("cupsd",pid=15795,fd=7))                 
tcp   LISTEN 0      10                                          127.0.0.1:4001              0.0.0.0:*          users:(("qq",pid=2521049,fd=198))                
tcp   LISTEN 0      10                                          127.0.0.1:35600             0.0.0.0:*          users:(("ToDesk_Service",pid=3429,fd=6))         
tcp   LISTEN 0      5                                           127.0.0.1:48267             0.0.0.0:*          users:(("python",pid=1568095,fd=4))              
tcp   LISTEN 0      4096                                        127.0.0.1:42959             0.0.0.0:*          users:(("mongod",pid=1568181,fd=11))             
tcp   LISTEN 0      100                                         127.0.0.1:9000              0.0.0.0:*          users:(("python",pid=1567887,fd=36))             
tcp   LISTEN 0      100                                         127.0.0.1:9001              0.0.0.0:*          users:(("python",pid=1567887,fd=14))             
tcp   LISTEN 0      100                                         127.0.0.1:9002              0.0.0.0:*          users:(("python",pid=1567887,fd=10))             
tcp   LISTEN 0      100                                         127.0.0.1:9003              0.0.0.0:*          users:(("python",pid=1567887,fd=12))             
tcp   LISTEN 0      100                                         127.0.0.1:9004              0.0.0.0:*          users:(("python",pid=1567887,fd=23))             
tcp   LISTEN 0      511                                         127.0.0.1:9210              0.0.0.0:*          users:(("qq",pid=2521049,fd=114))                
tcp   LISTEN 0      100                                         127.0.0.1:41611             0.0.0.0:*          users:(("python",pid=1567887,fd=28))             
tcp   LISTEN 0      4096                                        127.0.0.1:11434             0.0.0.0:*          users:(("ollama",pid=3426,fd=3))                 
tcp   LISTEN 0      50                                 [::ffff:127.0.0.1]:19080                   *:*          users:(("nutstore",pid=4814,fd=41))              
tcp   LISTEN 0      64                                                  *:34642                   *:*          users:(("nutstore",pid=4814,fd=45))              
tcp   LISTEN 0      5                                               [::1]:631                  [::]:*          users:(("cupsd",pid=15795,fd=6)) 
2.1. 输出结果字段解释:
Netid:协议类型(如 tcp, udp, icmp6)。
 State:套接字状态(如 LISTEN 表示监听中,UNCONN 表示未连接)。
 Recv-Q/Send-Q:接收/发送队列大小(单位:字节)。
 Local Address:Port:本地地址和端口。
 Peer Address:Port:对端地址和端口(UDP 通常为 0.0.0.0:*)。
 Process:关联的进程名称、PID 和文件描述符(FD)。
2.2. 关键输出分析:
2.2.1. 系统服务:
- DNS 解析:systemd-resolved 在 127.0.0.53:53 处理 DNS 请求。udp UNCONN 0 0 127.0.0.53%lo:53 0.0.0.0:* users:(("systemd-resolve",pid=1161,fd=12)) tcp LISTEN 0 4096 127.0.0.53%lo:53 0.0.0.0:* users:(("systemd-resolve",pid=1161,fd=13))
- 打印服务:cupsd 在 631 端口提供打印服务。tcp LISTEN 0 5 127.0.0.1:631 0.0.0.0:* users:(("cupsd",pid=15795,fd=7))
- IPV6邻居发现:icmp6 UNCONN 0 0 *:58 *:* users:(("NetworkManager",pid=1230,fd=21))
- NetworkManager 处理 ICMPv6 邻居发现协议(端口 58)。
2.2.2. 用户应用程序:
- RayLinkService:RayLinkService 在多个接口(如无线网卡 wlp2s0、Docker 网桥docker0)监听 UDP33001端口,TCP51321端口可能用于控制连接。udp UNCONN 0 0 10.68.133.100%wlp2s0:33001 0.0.0.0:* users:(("RayLinkService",pid=5084,fd=41)) tcp LISTEN 0 128 127.0.0.1:51321 0.0.0.0:* users:(("RayLinkService",pid=5084,fd=6))
- 数据库服务:mongod在42959端口运行 MongoDB 数据库。tcp LISTEN 0 4096 127.0.0.1:42959 0.0.0.0:* users:(("mongod",pid=1568181,fd=11))
2.2.3. 其他服务:
- mDNS(局域网服务发现):Chrome 和 avahi-daemon使用5353端口支持 mDNS(如设备发现)。udp UNCONN 0 0 224.0.0.251:5353 0.0.0.0:* users:(("chrome",pid=6862,fd=85))
- 远程控制工具:tcp LISTEN 0 10 127.0.0.1:35600 0.0.0.0:* users:(("ToDesk_Service",pid=3429,fd=6))
- ToDesk 在 35600端口提供远程控制服务。
总结:
- 用途:快速定位系统中所有监听端口的进程,用于排查端口冲突、识别不明服务或监控网络活动。
- 重点关注: 
  - 监听在 0.0.0.0(所有接口)或公网 IP 的端口,可能存在安全风险。
- 未知进程或非预期端口,需进一步检查是否为恶意软件。
 
