Tag Archives: linux

Enumerate applications listening to a computer port

How to figure which application/process is already using a port?

For example, below apache is failing to start because port 80 is already taken by some other application.

Apr 11 21:08:13 ip-172-26-0-194 systemd[1]: Starting The Apache HTTP Server...
Apr 11 21:08:13 ip-172-26-0-194 apachectl[17997]: (98)Address already in use: AH00072: make_sock: could not bind to address [::]:80
Apr 11 21:08:13 ip-172-26-0-194 apachectl[17997]: (98)Address already in use: AH00072: make_sock: could not bind to address 0.0.0.0:80

Here is a script which can help you find this info.

application_port_list.sh
# list out the processes using a port.
# This script does not give the exact answer but potential solutions. It uses
# three tools one after another.
#
# - lsof
# - netstat
# - ss
set -e
PORT="$1"
echo "Looking for application using port $PORT"
if command -v lsof &> /dev/null
then
sudo lsof -i :$PORT
fi
if command -v netstat &> /dev/null
then
netstat -tulpn | grep ":$PORT"
exit;
fi
if command -v ss &> /dev/null
then
sudo ss -tulp | grep ":$PORT"
exit;
fi