If I connect to a server like so:
JP_PORT=$(shuf -i 8895-9100 -n 1)
TB_PORT=$(shuf -i 6010-6200 -n 1)
ssh -Y -L ${JP_PORT}:127.0.0.1:${JP_PORT} -L ${TB_PORT}:127.0.0.1:${TB_PORT} <host>
Once connection is established, is there any terminal command that will return the list of ports I forwarded?
You can list via lsof
You may use grep to filter results
If it is enough to list only the forwarded connections that are actually used, and if your session is interactive (like in your case), you can use the
~#
escape within the ssh session. Just type enter~#.This will also list forwardings that were added later within the session using escape commands, and thus are not appearing on the command line.
Try
~?
for other useful commands inside the ssh session. See also the section on ESCAPE CHARACTERS in the ssh manual page.There's a couple more ways to list ssh's connections/tunnels. Firstly one can use
netstat
(p
show process name (sudo needed),ip
- show IP connections):Secondly one can also use the socket stats command
ss
(p
- show process name (sudo needed),e
- extended socket info for userid,t
- TCP connections):Note: Local (
-L
) ssh tunnels are not created on the server until the something on the client side has initiated a connection to the local port - only then will the tunnel be set up on the server side and be visible by these methods or any of the other answers.