How to View Processes and Their Respective Ports on Linux and OSX

If you want to see what processes are listening on what ports these you can use the linux command lsof and netstat. Below are a few good examples. You need to be root or use sudo to see processes that you don’t own.

Show what processes are using port 80

Output
COMMAND   PID     USER   FD   TYPE DEVICE SIZE/OFF NODE NAME
apache2  3369 www-data    4u  IPv6 410048      0t0  TCP *:http (LISTEN)
apache2  3370 www-data    4u  IPv6 410048      0t0  TCP *:http (LISTEN)
apache2  3372 www-data    4u  IPv6 410048      0t0  TCP *:http (LISTEN)
apache2 12140     root    4u  IPv6 410048      0t0  TCP *:http (LISTEN)
apache2 27614 www-data    4u  IPv6 410048      0t0  TCP *:http (LISTEN)
apache2 27615 www-data    4u  IPv6 410048      0t0  TCP *:http (LISTEN)
apache2 27654 www-data    4u  IPv6 410048      0t0  TCP *:http (LISTEN)
apache2 27792 www-data    4u  IPv6 410048      0t0  TCP *:http (LISTEN)
apache2 30098 www-data    4u  IPv6 410048      0t0  TCP *:http (LISTEN)
apache2 30099 www-data    4u  IPv6 410048      0t0  TCP *:http (LISTEN)
apache2 30100 www-data    4u  IPv6 410048      0t0  TCP *:http (LISTEN)
root@util:~#

 

Listing processes on a MAC (port 22)

Output
COMMAND     PID      USER   FD   TYPE             DEVICE SIZE/OFF NODE NAME
Cyberduck  3887 mike  132u  IPv6 0x9c24bd554cfdc3e3      0t0  TCP 10.10.0.14:53962->dev.com:ssh (ESTABLISHED)
Cyberduck  3887 mike  144u  IPv6 0x9c24bd555473fea3      0t0  TCP 10.11.0.10:53770->dev.com:ssh (ESTABLISHED)
Cyberduck  3887 mike  145u  IPv6 0x9c24bd5549389923      0t0  TCP 10.10.0.14:53977->dev.com:ssh (ESTABLISHED)
Cyberduck  3887 mike  146u  IPv6 0x9c24bd5559a803e3      0t0  TCP 10.10.0.14:58802->10.3.8.1:ssh (ESTABLISHED)
ssh       16962 mike    3u  IPv4 0x9c24bd553fc5a82b      0t0  TCP 10.10.0.14:55981->dev:ssh (ESTABLISHED)
ssh       17375 mike    3u  IPv4 0x9c24bd553a95363b      0t0  TCP 10.10.0.14:60429->dev:ssh (ESTABLISHED)

 

list of port numbers that are listening and what application it is

Output
Active Internet connections (only servers)
Proto Recv-Q Send-Q Local Address           Foreign Address         State       PID/Program name
tcp        0      0 0.0.0.0:3306            0.0.0.0:*               LISTEN      2840/mysqld     
tcp        0      0 0.0.0.0:54321           0.0.0.0:*               LISTEN      1409/sshd       
tcp        0      0 0.0.0.0:22              0.0.0.0:*               LISTEN      1409/sshd       
tcp        0      0 0.0.0.0:25              0.0.0.0:*               LISTEN      2146/master     
tcp6       0      0 :::80                   :::*                    LISTEN      3369/apache2    
tcp6       0      0 :::54321                :::*                    LISTEN      1409/sshd       
tcp6       0      0 :::22                   :::*                    LISTEN      1409/sshd