To reach out a machine behind NAT/firewall/whatever you don't need anything except for that machine being able to make an outbound connection to the public network and ssh access to an instance in the public network.
The setup on the machine behind NAT is to ensure it makes an outbout ssh connection with the reverse port-forwarding enabled:
ssh -R:60000:127.0.0.1:22 [email protected]
where [email protected]
is the account and the address of the instance in the public network you have access to. This command, basically, means the following: establish an SSH connection to friendly.domain.tld
using user joe
to authenticate and expose our local tcp port 22 at the remote end as tcp port 60000. If the SSH server software is recent enough (and configuration allows it) you may also directly map your local forwarded port to an internet facing IP address on friendly.domain.tld
, but I assume here that such a configuration is not allowed.
Now, that our machine has established the outgoing link all we need to do to connect to it from anywhere is to use the friendly.domain.tld
as a proxy in our SSH connection. For this you will need to open two terminal windows: the first will be used to establish a link with the friendly.domain.tld
instance to port-forward the exposed tcp port 60000 to your local machine:
ssh -L60000:127.0.0.1:60000 [email protected]
This maps tcp port 60000 on the loopback interface of friendly.domain.tld
to the loopback interface of the instance you are running the ssh command on.
From this point on, you can connect to your machine behind NAT by simply using:
ssh -p60000 my_account@localhost
where my_account
is an account on your machine behind NAT.
All in all, there are three SSH tunnels in play:
machine_behind_nat <=1=> friendly_instance <=2=> your_laptop
<=================3==================>
The reason for such a setup is to ensure that even if "friendly_instance" is broken into your communication with the machine behind NAT is not compromised (we are not trusting the "friendly_instance" beyond allowing it to transfer bytes via its loopback interface. We are using end-to-end encryption in our tunnel #3 which travels over tunnels #1 and #2.
I covered this setup in my blog if you want a bit of background and other tricks you can do with ssh.