Linux根据端口号查看进程信息

前言

有时候服务器上部署了N多的服务,只知道端口号,你想知道这个服务是谁,路径在哪,这时候就需要一些命令来查询了

方法

1、命令lsof, 用法

1
2
3
4
[root@localhost]# lsof -i:80
[root@localhost]#

# 以上是80端口没有被占用
1
2
3
4
5
6
7
[root@localhost sbin]# lsof -i:80
COMMAND PID USER FD TYPE DEVICE SIZE/OFF NODE NAME
nginx 8246 root 6u IPv4 64233 0t0 TCP *:http (LISTEN)
nginx 8247 nobody 6u IPv4 64233 0t0 TCP *:http (LISTEN)
[root@localhost sbin]#

#这个是80端口被占用了

2、但是又的机器上没有这个losf这个命令,又无法安装,还有一种方式netstat命令

1
2
3
[root@localhost sbin]# netstat -nlp|grep :80
tcp 0 0 0.0.0.0:80 0.0.0.0:* LISTEN 8246/nginx
[root@localhost sbin]#

8246就是进程号

3、下面我们再根据进程号查询具体的进程信息ps命令

1
2
3
4
5
6
7
8
9
[root@localhost sbin]# ps -ef | grep 8246
root 8246 1 0 14:56 ? 00:00:00 nginx: master process ./nginx
nobody 8247 8246 0 14:56 ? 00:00:00 nginx: worker process
root 8461 2679 0 15:26 pts/1 00:00:00 grep 8246
[root@localhost sbin]# ps -x | grep 8246
Warning: bad syntax, perhaps a bogus '-'? See /usr/share/doc/procps-3.2.8/FAQ
8246 ? Ss 0:00 nginx: master process ./nginx
8463 pts/1 S+ 0:00 grep 8246
[root@localhost sbin]#
结语

以上就是两种查询方式,非常有用,有时候你不知道机器上部署了什么服务,但是知道端口号,这就非常有用了