netstat 查看端口占用、杀死进程

发布于:2025-04-12 ⋅ 阅读:(42) ⋅ 点赞:(0)


查看端口占用

netstat -tanlp

强制杀死

在安全的情况下,用下面命令杀死端口占用的进程

sudo kill $(sudo lsof -t -i:3128)

报错信息

部署程序时,会出现这样的报错:

Error response from daemon: driver failed programming external connectivity on endpoint docker-redis-1 (ad25e19a912e30946f61b1bd5d5d77cfc57ecc06e381de34abda4f39ebd65229): failed to bind port 0.0.0.0:6379/tcp: Error starting userland proxy: listen tcp4 0.0.0.0:6379: bind: address already in use

使用 netstat 看不到直接的端口占用

$ netstat -tanlp
(Not all processes could be identified, non-owned process info
 will not be shown, you would have to be root to see it all.)
Active Internet connections (servers and established)
Proto Recv-Q Send-Q Local Address           Foreign Address         State       PID/Program name    
tcp        0      0 0.0.0.0:8080            0.0.0.0:*               LISTEN      -                   
tcp        0      0 127.0.0.53:53           0.0.0.0:*               LISTEN      -                   
tcp        0      0 0.0.0.0:22              0.0.0.0:*               LISTEN      -                   
tcp        0      0 0.0.0.0:5432            0.0.0.0:*               LISTEN      -                   
tcp        0      0 0.0.0.0:3128            0.0.0.0:*               LISTEN      -                   
tcp        0      0 0.0.0.0:25              0.0.0.0:*               LISTEN      -                   
tcp        0      0 0.0.0.0:13921           0.0.0.0:*               LISTEN      -                   
tcp        0      0 0.0.0.0:8194            0.0.0.0:*               LISTEN      -     

使用 lsof

这样也无法看到

lsof -i:3128

需要加上sudo

$ sudo lsof  -i:3128
COMMAND     PID USER   FD   TYPE DEVICE SIZE/OFF NODE NAME
docker-pr 24571 root    4u  IPv4  88178      0t0  TCP *:3128 (LISTEN)

加上-t 就是只返回进程 id

$ sudo lsof -t -i:3128
24571


2025-04-06(六)