I was trying to figure out a small problem with connecting to vnc through an SSH tunnel today where one of the possible issues was that someone else was already tunneled to the port.  Without going into the specifics of that problem, it made me realize I'm not as knowledgeable at how to ssh tunnels are allocating resources and how to view them.So I have two questions.  First, if I create an SSH tunnel but am not currently using that tunnel (ie, nothing on my local system has connected to the tunneled port) which ports are currently blocked and unavailable for others to use?  I'm pretty sure my source port will be owned by my tunnel and thus not be available to be bound by another application, but what about the destination port?  Is that port already bound to the tunnel or does it only get bound when I try to connect to my local port and the connection is tunneled through SSH to my destination port?Secondly, how can I view what ssh tunnels exist on my system?  If I created the tunnel locally I can do a ps and grep for the appropriate program, but if I have a client creating a local tunnel to my server's port is there a way that, from my server, I can see that this tunnel exists; even if the client has not yet connected to a program through the tunnel?
Understanding, and viewing, how ports are allocated by ssh tunnels?
ssh;networking;ssh tunneling
netstat and lsof or fuser are typical programs to view what ports are opened by what (though may require root or such access, depending, and vary by flavour of unix). A shared destination port is fine. It may help to work through e.g. Beej's guide to networking to understand the system calls involved.If the client has not connected, then any ports it tunnels are not bound, as that step will only happen as a connection is made. (Exception: there may be ports lingering from a now closed connection in TIME_WAIT or such.)