Finding the PID of the process using a specific port?
On Linux, you must be root or the owner of the process to get the information you desire. As such, for processes running as another user, prepending sudo
is most of what you need. In addition to that, on modern Linux systems, ss
is tool to use to do this:
$ sudo ss -lptn 'sport = :80'
State Local Address:Port Peer Address:Port
LISTEN 127.0.0.1:80 *:* users:(("nginx",pid=125004,fd=12))
LISTEN ::1:80 :::* users:(("nginx",pid=125004,fd=11))
You can also use the same invocation you're currently using, but remember to sudo
:
$ sudo netstat -nlp | grep :80
tcp 0 0 0.0.0.0:80 0.0.0.0:* LISTEN 125004/nginx
You can also use lsof:
$ sudo lsof -n -i :80 | grep LISTEN
nginx 125004 nginx 3u IPv4 6645 0t0 TCP 0.0.0.0:80 (LISTEN)
Also you can use lsof
utility. Need to be root.
# lsof -i :25
COMMAND PID USER FD TYPE DEVICE SIZE/OFF NODE NAME
exim4 2799 Debian-exim 3u IPv4 6645 0t0 TCP localhost:smtp (LISTEN)
exim4 2799 Debian-exim 4u IPv6 6646 0t0 TCP localhost:smtp (LISTEN)
I am using "CentOS 7 minimal" which has nor netstat
neither lsof
. But a lot of linux distributions have the socket statistics command (i.e. ss
).
Here is an example of execution:
# ss -tanp | grep 6379
LISTEN 0 128 127.0.0.1:6379 *:* users:(("redis-server",pid=2531,fd=4))