Big Sur and later, use this command:
sudo lsof -i -P | grep LISTEN
or to just see just IPv4:
sudo lsof -nP -i4TCP:$PORT | grep LISTEN
On older versions, use one of the following forms:
sudo lsof -nP -iTCP:$PORT | grep LISTEN
sudo lsof -nP -i:$PORT | grep LISTEN
$PORT with the port number or a comma-separated list of port numbers.
sudo (followed by a space) if you need information on ports below #1024.
-n flag is for displaying IP addresses instead of host names. This makes the command execute much faster, because DNS lookups to get the host names can be slow (several seconds or a minute for many hosts).
-P flag is for displaying raw port numbers instead of resolved names like
ftp or more esoteric service names like
See the comments for more options.
For completeness, because frequently used together:
To kill the PID:
sudo kill -9 <PID>
# kill -9 60401
pingthe broadcast address (the broadcast address is printed as part of the output to
The hosts answering are on your local network. (You may also try
arp -abut that only keeps track of recently contacted hosts so you may want to run it after the broadcast.)