Finding out which process is using a port on Linux
📄 Wiki page | 🕑 Last updated: Mar 31, 2023The simplest way to find out which process is using a port on Linux is to use the lsof
command:
lsof -i :80
(-i tells lsof
to list only files matched by the specified Internet address)
Example output (with nginx running on port 80):
COMMAND PID USER FD TYPE DEVICE SIZE/OFF NODE NAME
nginx 1074 root 6u IPv4 12011 0t0 TCP *:http (LISTEN)
nginx 1074 root 7u IPv6 12012 0t0 TCP *:http (LISTEN)
nginx 1075 www-data 6u IPv4 12011 0t0 TCP *:http (LISTEN)
nginx 1075 www-data 7u IPv6 12012 0t0 TCP *:http (LISTEN)
nginx 1076 www-data 6u IPv4 12011 0t0 TCP *:http (LISTEN)
nginx 1076 www-data 7u IPv6 12012 0t0 TCP *:http (LISTEN)
nginx 1077 www-data 6u IPv4 12011 0t0 TCP *:http (LISTEN)
nginx 1077 www-data 7u IPv6 12012 0t0 TCP *:http (LISTEN)
nginx 1078 www-data 6u IPv4 12011 0t0 TCP *:http (LISTEN)
nginx 1078 www-data 7u IPv6 12012 0t0 TCP *:http (LISTEN)
If no matches are found, you'll get empty output with return code 1 (useful for scripting):
lsof -i :81 || echo "Nothing found."
Alternatively, you could filter the output from lsof -P
, but this would require way more resources.
If for some reason you don't have losf already installed, you can install it with:
# deb-based (Debian, Ubuntu, Mint, Raspbian, Kali, etc.)
apt install lsof
# rpm-based (Fedora, CentOS, etc.)
dnf install lsof
# Arch
pacman -S lsof
# Alpine
apk add lsof
Alternatives
Alternatives you could use instead of lsof
are fuser
and netstat
(I'll add more examples on this later).