SSH to a machine behind NAT

SSHing to a machine behind NAT has generally been a daunting task. One of the common way that people tend to use is through forwarding a port from router on to the machine. But it is not always the case that a person has access to router configuration settings of his network. I recently stumbled upon an awesome tool named ngrok which solves this problem very elegantly. Even though this tool’s mail purpose is to take your localhost online but it has many other features out of which we will leverage one now. Let do it:

  • Download ngrok to the Linux or Mac machine you want to ssh to. You can download it from here.
  • Unzip it by going to the folder it is downloaded to and executing the following command –
    unzip ngrok.zip
  • After extracting run the following command –
    ./ngrok -proto=tcp 22

    Running the above command will take you to a screen like below –

    In the above screenshot we can see that port 58339 on ngrok.com is forwarded to port 22 of our Linux/Mac machine.
  • So now you are ready to SSH to your machine from some other machine. Use the following command to ssh –
    ssh -p <nrgok_port> <local_machine_username>@ngrok.com
    

    Like in the case of screen shot above I need to type in the following command –

    ssh -p 58339 myusername@ngrok.com
    

Enjoy SSHing fellas !!