Linux – How to get the list of SSH tunnels connected with the username used

linuxsshssh-tunneltunneltunneling

The connection is established from Desktop(D) to Server(S) with ssh -D PORT username@Server.

From the Server, the list of ssh users connected can be obtained with who, but the ssh tunnels are not listed in who or w. Also, with netstat -lnpt | grep ssh, the connected user is not listed.

With other commands, such as ps aux | grep ssh or lsof -i -n | egrep '\<ssh\>', a lot more information is retrieved, looking as if more users were connected.

Is there a (What is the) reliable way of getting the list of ssh tunnels with their respective users on S, ideally including the IP address of D?

Best Answer

The use of ssh -D is not visible to the server. It is only once a socks client connects to the ssh client and request a connection, that the ssh client will ask the server for a forwarding.

Once a connection is fully established, it will be visible on the server. You can see it with netstat -ntp.

It will obviously not be visible with netstat -lntp on the server, because it does not involve any listening sockets.

On the client side running netstat -lntp will show that ssh is listening on the specified port.