linux下查询端口是否被占用,杀掉进程关闭端口占用
文章目录
概述
首先你必须知道,端口不是独立存在的,它是依附于进程的;
某个进程开启,那么它对应的端口就开启了,进程关闭,则该端口也就关闭了;
下次若某个进程再次开启,则相应的端口也再次开启;
而不要纯粹的理解为关闭掉某个端口,不过可以禁用某个端口.
查询端口
比如我们要查询 6379 端口
netstat -anp | grep 6379
[root@centos76 ~]# netstat -anp | grep 6379
tcp 0 0 172.16.60.111:6379 0.0.0.0:* LISTEN 2195/redis-server 1
tcp 0 0 127.0.0.1:6379 0.0.0.0:* LISTEN 2195/redis-server 1
tcp 0 0 127.0.0.1:45016 127.0.0.1:6379 ESTABLISHED 16498/node /data/ww
tcp 0 0 127.0.0.1:52014 127.0.0.1:6379 ESTABLISHED 29727/node /data/ww
tcp 0 0 127.0.0.1:6379 127.0.0.1:33198 ESTABLISHED 2195/redis-server 1
tcp 0 0 127.0.0.1:45100 127.0.0.1:6379 ESTABLISHED 1145/node /data/www
tcp 0 0 127.0.0.1:6379 127.0.0.1:47468 ESTABLISHED 2195/redis-server 1
tcp 0 0 127.0.0.1:6379 127.0.0.1:45016 ESTABLISHED 2195/redis-server 1
tcp 0 0 127.0.0.1:6379 127.0.0.1:54700 ESTABLISHED 2195/redis-server 1
tcp 0 0 127.0.0.1:6379 127.0.0.1:52014 ESTABLISHED 2195/redis-server 1
tcp 0 0 127.0.0.1:6379 127.0.0.1:47114 ESTABLISHED 2195/redis-server 1
tcp 0 0 127.0.0.1:47114 127.0.0.1:6379 ESTABLISHED 1925/node /data/www
tcp 0 0 127.0.0.1:6379 127.0.0.1:54702 ESTABLISHED 2195/redis-server 1
tcp 0 0 127.0.0.1:54702 127.0.0.1:6379 ESTABLISHED 27700/node /data/ww
tcp 0 0 127.0.0.1:45102 127.0.0.1:6379 ESTABLISHED 1145/node /data/www
tcp 0 0 127.0.0.1:33198 127.0.0.1:6379 ESTABLISHED 26873/node /data/ww
tcp 0 0 127.0.0.1:45014 127.0.0.1:6379 ESTABLISHED 16498/node /data/ww
tcp 0 0 127.0.0.1:47468 127.0.0.1:6379 ESTABLISHED 19832/node /data/ww
tcp 0 0 127.0.0.1:6379 127.0.0.1:45100 ESTABLISHED 2195/redis-server 1
tcp 0 0 127.0.0.1:6379 127.0.0.1:45014 ESTABLISHED 2195/redis-server 1
tcp 0 0 127.0.0.1:54700 127.0.0.1:6379 ESTABLISHED 27700/node /data/ww
tcp 0 0 127.0.0.1:6379 127.0.0.1:45102 ESTABLISHED 2195/redis-server 1
显示出来所有连接 6379 端口的进程
杀掉进程,释放端口
tcp 0 0 172.16.60.111:6379 0.0.0.0:* LISTEN 2195/redis-server 1
上面这一条说明 6379这个端口被进程ID为 2195 占用
我们只需要杀掉 2195 进程即可.
kill -9 进程ID