SSH tunnel between two servers behind firewalls

You want to set up an SSH tunnel from computerA to computerB, so that computer B can connect to computer A however both computer A and B have firewalls that prevent incoming ssh connections.

To facilitate this, we'll tunnel through a third computer computerC which does allow incoming ssh connections
  1. 1

    On computer A

    ssh -o TCPKeepAlive=no -o ServerAliveInterval=15 -nNT -R 8822:localhost:22 username@computerC
    This command will forward port 22 (SSH) to port 8822, which is an unprivileged port on computerC

    the -n prevents ssh from reading comands from stdin
    the -N does not execute remote commands
    the -T prevents a pseudo-tty being allocated

    the -o commands change ssh settings and in this case set up a 15 second keep alive signal that is designed to prevent the ssh tunnel from going stale (which happens after about 5 minutes otherwise forcing you to reconnect)

  2. 2

    On computer B

    ssh -o TCPKeepAlive=no -o ServerAliveInterval=15 -nNT -L 8822:localhost:8822 username@computerC
    This one forwards the port 8822 on computerC to our own 8822 on ComputerB.
    That's it, the link is now set up, let's connect to computer A from computer B

  3. 3

    On computer B

    ssh -o TCPKeepAlive=no -o ServerAliveInterval=15 username@localhost -p 8822
    The username should be the username on computerA that you are connecting to.

    the -p option tells computerB to ssh via the unprivileged 8822 port that we forwarded from computerA