Skip to content

Instantly share code, notes, and snippets.

@tallesairan
Last active April 7, 2023 12:05
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save tallesairan/d4a68c72088b704467dcce959aa61f82 to your computer and use it in GitHub Desktop.
Save tallesairan/d4a68c72088b704467dcce959aa61f82 to your computer and use it in GitHub Desktop.
get pid by port on unix

On Linux, you must be root or the other user to get process information for processes running as other users, so prepending sudo is most of what you need. In addition to that, on modern Linux systems, ss is tool to use to do this:

$ sudo ss -lptn 'sport = :80'

State Local Address:Port Peer Address:Port
LISTEN 127.0.0.1:80 : users:(("nginx",pid=125004,fd=12)) LISTEN ::1:80 :::* users:(("nginx",pid=125004,fd=11))

You can also use the same invocation you're currently using, but remember to sudo:

$ sudo netstat -nlp | grep :80

tcp 0 0 0.0.0.0:80 0.0.0.0:* LISTEN 125004/nginx

You can also use lsof:

$ sudo lsof -n -i :80 | grep LISTEN

nginx 125004 nginx 3u IPv4 6645 0t0 TCP 0.0.0.0:80 (LISTEN)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment