Christian Rodriguez
Christian Rodriguez

Reputation: 690

SSH server - Get pid of sshd process forwarding port #N

I'm running a server (Ubuntu Server 14.04) which allows the clients to make a ssh tunnel from their device (Raspberry Pi) so they can access their web server from the internet (as a mean to traverse NATs). I can get a list of processes owned by the user (which is the same for all the devices) using ps -u username (this user only runs sshd to forward ports), but I can't filter those processes by the port they're forwarding. So the question is, how can I get the pid of the sshd that is forwarding port #N?

Upvotes: 0

Views: 1369

Answers (1)

zochamx
zochamx

Reputation: 950

You can make use of lsof command since everything is a file on linux.

Something like lsof -Pan -i | grep :PORT will get you what you ask. It has an output like this when i run it for port 80 on my machine:

Command    PID     USER     FD  TYPE  DEVICE SIZE/OFF NODE  NAME
nginx      1104     root    6u  IPv4  23348      0t0  TCP *:80 (LISTEN)
nginx      1105 www-data    6u  IPv4  23348      0t0  TCP *:80 (LISTEN)
nginx      1106 www-data    6u  IPv4  23348      0t0  TCP *:80 (LISTEN)
nginx      1107 www-data    6u  IPv4  23348      0t0  TCP *:80 (LISTEN)
nginx      1108 www-data    6u  IPv4  23348      0t0  TCP *:80 (LISTEN)

More on lsof can be found here

Upvotes: 2

Related Questions