Taking a Closer Look at Ngrok for SSH Tunneling

Taking a Closer Look at Ngrok for SSH Tunneling

Photo by h heyerlein / Unsplash

Howdy tech enthusiasts! 🤓 Today, we're taking a deep dive into the world of Ngrok and SSH tunneling. I'll be your guide through this process and by the end of it, you'll be a master at setting up Ngrok to expose your SSH service on port 22 to the internet. Plus, I'll show you how to stop the Ngrok process when you're done using it. Sound good? Let's get started!

Getting Started with Ngrok

1. Download the Ngrok binary from the official website:

wget https://bin.equinox.io/c/bNyj1mQVY4c/ngrok-v3-stable-linux-amd64.tgz

2. Unpack the downloaded file using the tar command:

tar --extract --verbose --file ngrok-v3-stable-linux-amd64.tgz

3. Move the Ngrok binary to a directory in your system's PATH:

mv ngrok /usr/local/bin

Setting Up Your Ngrok Account

3. Get your Ngrok authtoken from your account dashboard on the Ngrok website.

4. Add the authtoken to the configuration file to authenticate with the Ngrok service:

ngrok config add-authtoken Your_Authtoken_Here

Running Ngrok

5. Run Ngrok to expose your SSH service to the internet (replace 22 with your SSH service port number if different):

ngrok tcp 22

Or you can run Ngrok in the background and keep it running even after closing the terminal using the nohup command and send the process to the background using &:

nohup ngrok tcp 22 --log=/path/to/your/logfile.log &

Monitoring the Ngrok Log

6. You can monitor the Ngrok log to see the status of the tunnel:

tail --follow=name /path/to/your/logfile.log

At the end of the log output, you should see a line that looks like this:

t=2023-05-10T05:52:42+0000 lvl=info msg="started tunnel" obj=tunnels name=command_line addr=//localhost:22 url=tcp://0.tcp.jp.ngrok.io:10465

The URL at the end of this line is the public URL that you can use to access your SSH service from the internet.

Wrapping Up Your Ngrok Session

Once you're done with Ngrok, you can stop the process by finding its process ID (PID) and then using the kill command:

7. Find the PID of the Ngrok process using the ps aux | grep ngrok command:

ps aux | grep --color=auto ngrok

8. Once you have the PID, use the kill command to stop the Ngrok process:

kill Ngrok_PID

Don't forget to replace Ngrok_PID with the actual process ID of the Ngrok process.

And that's a wrap, folks! You've successfully set up Ngrok for SSH tunneling on your Linux system. Congratulations! You can now access your SSH service from anywhere with an internet connection.

Thanks for tuning in and happy hacking! 😎

Ethan Yu

Ethan Yu

A software engineer with expertise in TypeScript, React, Node.js, Python, C++, MATLAB, etc. I share my tech experiences on my blog, with a focus on Linux, macOS, and Docker.