Finding out what process is listening on a port under Linux

Category: tips Tags: linux admin networking

Ever needed to find out what process has a port open? Or easily check all listening ports and see what process has them open?

If so, it's very easy to do. There are in fact, multiple ways to solve these problems.

The main way that I use is netstat. It can show many useful things but for this example, the syntax is:

netstat -tulpn

Which will show something like:

Active Internet connections (only servers)
Proto Recv-Q Send-Q Local Address        Foreign Address   State       PID/Program name
tcp        0      0 127.0.0.1:3306       0.0.0.0:*         LISTEN      1374/mysqld
tcp        0      0 0.0.0.0:139          0.0.0.0:*         LISTEN      1132/smbd
tcp        0      0 0.0.0.0:80           0.0.0.0:*         LISTEN      12171/apache2
tcp        0      0 0.0.0.0:50000        0.0.0.0:*         LISTEN      2247/mediatomb
tcp        0      0 0.0.0.0:4949         0.0.0.0:*         LISTEN      1413/munin-node
tcp        0      0 0.0.0.0:53           0.0.0.0:*         LISTEN      1371/dnsmasq
tcp        0      0 0.0.0.0:22           0.0.0.0:*         LISTEN      1093/sshd

You can also use grep to limit your results if you have a lot of open ports:

netstat -tulpn | grep :80

for example will show you port 80 (http)

tcp        0      0 0.0.0.0:80              0.0.0.0:*               LISTEN      12171/apache2

Another method that works but I find isn't as handy at times is fuser:

fuser 80/tcp

which will show something like:

80/tcp:              12171 12174 12175 12176 12177 12178

but doesn't tell you easily the name of the process like netstat will, which for example means another step like:

ls -l /proc/12171/exe

to give you

lrwxrwxrwx 1 root root 0 2011-02-14 12:55 /proc/12171/exe -> /usr/lib/apache2/mpm-prefork/apache2

which then tells you that port 80 is opened by process 12171 which is apache2.

I prefer the netstat option as that shows everything you need to know with just one command generally.