SSH into Remote Linux Machine Using ngrok
Published
Ngrok exposes local servers behind NATs and firewalls to the public internet over secure tunnels. Using ngrok, we can expose our laptop or server to be connected via SSH from the public internet.
Prerequisites
To use ngrok
, you must have an account. To register a new account, visit
the signup page. You can also use your Google or GitHub
account for easy signup.
You should have sudo
privilege in your system.
You should also install snap
if your system does not have it. (snap
is installed by default if
you are using Ubuntu 16.04 LTS or higher). You can run the following command to install snap
:
1
2
sudo apt update
sudo apt install snapd # The d is not a typo!
Enable SSH
Before we use ngrok
to expose our server, we need to first enable SSH on our server. To enable
SSH, we need to install openssh-server
.
1
2
sudo apt update
sudo apt install openssh-server
Once the installation completes, the SSH service should start automatically. You can check its status with the following command:
1
sudo systemctl status ssh
If you see Active: active (running)
, you have successfully enabled SSH!
Install ngrok
To use ngrok
, you should install it to the Linux machine you want to expose. To install ngrok
on
Ubuntu, you can simply run
1
sudo snap install ngrok
If you are not using Ubuntu, you can also visit the download page and
download a zipped file containing ngrok
. Simply unzip it, and you have installed ngrok!
Authenticate ngrok
To use ngrok
, you must be first authenticate it using the ngrok
executable file from the
unzipped folder.
1
2
3
4
# If you installed with snap
ngrok authtoken <YOUR_AUTH_TOKEN>
# If you downloaded a zipped file
./ngrok authtoken <YOUR_AUTH_TOKEN>
Your authentication token can be found in the dashboard page of ngrok.
Run ngrok Server
Now, we can start forwarding the SSH port using ngrok
! Run the following command:
1
2
3
4
# If you installed with snap
ngrok tcp 22
# If you downloaded a zipped file
./ngrok tcp 22
You should see some output similar to this:
1
2
3
4
5
6
7
8
ngrok by @inconshreveable (Ctrl+C to quit)
Session Status online
Account <YOUR_EMAIL> (Plan: <YOUR_PLAN>)
Version 2.3.35
Region Japan (jp)
Web Interface http://127.0.0.1:4040
Forwarding <YOUR_ASSIGNED_URL> -> localhost:22
Your SSH port is now accessible in what is written in <YOUR_ASSIGNED_URL>
! For example, if it says
tcp://0.tcp.jp.ngrok.io:11111
, you can access it using that URL.
Conclusion
You have successfully set up SSH access to your remote Linux machine using ngrok. You can SSH into the machine using the following command:
1
2
# Assuming your URL was tcp://0.tcp.jp.ngrok.io:11111
ssh <YOUR_USERNAME>@0.tcp.jp.ngrok.io -p 11111
We recommend that your ngrok
service runs on background. Use tmux
or screen
to run the ngrok
service on a detached screen!