DEV Community

Cover image for How to create an SSH Tunnel with ngrok on Ubuntu Server (aka ssh access to your local server in local network with dynamic IP)
Diego Carrasco Gubernatis
Diego Carrasco Gubernatis

Posted on • Originally published at diegocarrasco.com

How to create an SSH Tunnel with ngrok on Ubuntu Server (aka ssh access to your local server in local network with dynamic IP)

I have a server at home running Ubuntu 18.04 LTS (normal local network, behind a router with firewall activated, no DMZ) and I wanted to be able to access it though ssh. You can think of it as a TeamViewer alternative for port redirection.

What’s SSH?

Secure Shell (SSH) is a cryptographic network protocol for operating network services securely over an unsecured network. Typical applications include remote command-line login and remote command execution, but any network service can be secured with SSH. -Wikipedia

Enter grok

Public URLs for exposing your local web server.
Spend more time programming. One command for an instant, secure URL to your localhost server through any NAT or firewall.

They weren’t kidding. To set it up do the following:

  1. Register at https://ngrok.com/
  2. Follow the steps on their “Getting started page”. There are 4 steps to follow. If you are in Ubuntu just run the following in the terminal to install. Both methods work just fine.
sudo snap install ngrok
Enter fullscreen mode Exit fullscreen mode

And then specify your auth token form here

ngrok authtoken your-token
Enter fullscreen mode Exit fullscreen mode

  1. Start ngrok on your ssh port. You can read more here
./ngrok tcp 22
Enter fullscreen mode Exit fullscreen mode
  1. Access your server with ssh
ssh  YOUR_USER@0.tcp.ngrok.io -p PORT
Enter fullscreen mode Exit fullscreen mode

where PORT is the port assigned by ngrok and YOUR_USER is your user in the server.
You can also check the port assigned at your ngrok dashboard

Optional: copy your ssh key to your server for a password-less access

  1. I use ssh-copy-id as follows
ssh-copy-id -p port -v 0.tcp.eu.ngrok.io -l user
Enter fullscreen mode Exit fullscreen mode

You can read more about it here

On Ubuntu ssh-copy-id is already installed. On Mac just run

brew install ssh-copy-id
Enter fullscreen mode Exit fullscreen mode

For more details about brew, read here

The missing package manager for macOS (or Linux)"). In a few words, think of brew as a package manager for Mac (like snap, apt, rpm, etc…).
If you just want to get it installed open a terminal window and run

/usr/bin/ruby -e "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/master/install)"
Enter fullscreen mode Exit fullscreen mode

About ngrok

ngrok is free for up to 10 devices. Their plans are in their pricing page
Their free plan allows the following:

  • HTTP/TCP tunnels on random URLs/ports
  • 1 online ngrok process
  • 4 tunnels/ngrok process
  • 40 connections / minute which is enough for personal and demo uses.

Enjoy!

Top comments (0)