Determine process using a port, without sudo

I'd like to find out which process (in particular, the process id) is using a given port. The one catch is, I don't want to use sudo, nor am I logged in as root. The processes I want this to work for are run by the same user that I want to find the process id - so I would have thought this was simple.

Both lsof and netstat won't tell me the process id unless I run them using sudo - they will tell me that the port is being used though.

As some extra context - I have various apps all connecting via SSH to a server I manage, and creating reverse port forwards. Once those are set up, my server does some processing using the forwarded port, and then the connection can be killed. If I can map specific ports (each app has their own) to processes, this is a simple script. Any suggestions?

This is on an Ubuntu box, by the way - but I'm guessing any solution will be standard across most Linux distros.


The --program option to netstat shows you PIDs and names of your own processes. This option is present and working on RHEL 6 in netstat 1.42 out of net-tools 1.60.

I verified that netstat -an --tcp --program shows me the PIDs of my processes.


Pawel's suggestion seems to work fine to me, but as an alternative, here's me listening from shell1:

[madhatta@risby ~]$ nc -l  localhost 3456

and here's me seeing it with lsof from shell2:

[madhatta@risby tmp]$ lsof -i tcp:3456
COMMAND   PID     USER   FD   TYPE   DEVICE SIZE/OFF NODE NAME
nc      18109 madhatta    3u  IPv4 69205153      0t0  TCP localhost.localdomain:vat (LISTEN)

Edit: you write in a comment that

SSH forwards must behave differently - even though the process is owned by the same user, I can't see it listed at all in lsof output unless I run it as root/sudo.

but this is not so for me. Having used ssh to forward local port 8001, with ssh vpn.example.com -L 8001:rt.int:80, I then find:

[madhatta@risby ~]$ lsof -n -i tcp:8001
COMMAND  PID     USER   FD   TYPE DEVICE SIZE/OFF NODE NAME
ssh     5375 madhatta    8u  IPv6 381234      0t0  TCP [::1]:vcom-tunnel (LISTEN)
ssh     5375 madhatta    9u  IPv4 381235      0t0  TCP 127.0.0.1:vcom-tunnel (LISTEN)

Could you perhaps show us some of your sample output, preferably not too heavily redacted?