Networking – How to Know What Program is Listening on a Given Port

networkingsocketswebserver

I suspect a program is listening on port 8000 on my machine.

When I run the following command, I get this error:

> python -m SimpleHTTPServer
# Lots of python error
socket.error: [Errno 98] Address already in use

If I use another port (8000 is the default), the web server runs fine.

If I run wget localhost:8000 from the command line, it returns 404 Not Found.

What can I do (or what tools are available) to find what program is listening on port 8000, and from there where that program is configured?

Best Answer

Open your terminal and type as

lsof -i :8000

that command will list you the application used by that port with PID. (If no results run via sudo since your might have no permission to certain processes.)

For example, with port 8000 (python3 -m http.server):

$ lsof -i :8000
COMMAND  PID USER   FD   TYPE  DEVICE SIZE/OFF NODE NAME
python3 3269 user    3u  IPv4 1783216      0t0  TCP *:8000 (LISTEN)

And port 22 (SSH):

$ sudo lsof -i :22
COMMAND  PID USER   FD   TYPE  DEVICE SIZE/OFF NODE NAME
sshd     998 root    3u  IPv4 1442116      0t0  TCP *:ssh (LISTEN)
sshd     998 root    4u  IPv6 1442118      0t0  TCP *:ssh (LISTEN)

Hope that helps.

Related Question