| by Arround The Web | No comments

How to Connect Raspberry Pi from Anywhere Using ngrok

Developing web applications on Raspberry Pi is a widely used task. However, with the traditional SSH service on Raspberry Pi, the development task may pose a security risk. The reason is SSH may look like a secure service until you have set strong credentials for it. But, if someone gets unauthorized access, he may take full control of your system using your login details. Thus, stealing your data, installing malware or damaging your Raspberry Pi system. Further, you cannot access the device from any part of the world unless you have an SSH key on the system.

Keeping these things in mind, the developer has introduced a new, secure and easy way to remotely access the Raspberry Pi system from anywhere. This can be done through ngrok.

Follow this guide to learn:

What is ngrok

The ngrok is a service like SSH that allows you to remotely access the system. However, it is different from traditional SSH service due to the following reasons:

  • It is easier to use and does not allow configuring any port forwarding rules or firewall.
  • It provides a public URL that you can share with others so that they can be able to access the system from any part of the world.
  • The end-to-end traffic between your local machine and ngrok server is encrypted. Thus, no unauthorized user will be able to tamper with your data.

How to Connect Raspberry Pi from Anywhere Using ngrok

To securely connect your Raspberry Pi Using ngrok, follow these steps:

Step 1: Download ngrok for Raspberry Pi
First, you have to download the ngrok source file from the official website for Raspberry Pi ARM based architecture.

If you are using 32 Bit Raspberry Pi OS, you can simply run the following command to download the ngrok source file on your system:

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

Step 2: Extract the Source Files
After downloading the source file, you have to extract the file to /usr/local/bin source directory that can be done from the following command:

sudo tar xvzf ngrok-v3-stable-linux-arm.tgz -C /usr/local/bin

Note: Make sure you provide the correct path where the ngrok source file is downloaded.

Step 3: Confirm ngrok Installation
After extracting the file to the /usr/local/bin directory, you can confirm the ngrok installation on Raspberry Pi using the following command:

ngrok --version

Step 4: Authenticate Raspberry Pi
You have to authenticate your Raspberry Pi on the ngrok network. This can be done by first creating the account on ngrok website and then using the Raspberry Pi option to get the auth token.

The token provided to you will need to be entered in the following command:

ngrok config add-authtoken <enter_token>

This will authenticate your device and saved the Authtoken to configuration file on Raspberry Pi:

Step 5: Create SSH Auth Keygen
Before moving forward towards accessing the Raspberry Pi remotely, you have to create a SSH Auth key. This step is important because you will be able to get connection closed or denied error, if you don’t do it and the step will be used to access Raspberry Pi through a reverse SSH tunnel.

To create SSH auth key on Raspberry Pi, run the following command:

ssh-keygen

Make sure you set the password for it, since you will need it later on:

Step 6: Edit the SSH File
Now, open the SSH configuration file and ChangePasswordAuthentication and ChallengeResponseAuthentication to yes inside the file.

sudo nano /etc/ssh/sshd_config

Then save the file using CTRL+X, add Y and press Enter.

Step 7: Restart SSH Service
After setting up the SSH keygen, restart the SSH service via the following command:

sudo systemctl restart sshd

Step 8: Create a SSH Tunnel with ngrok
Now, use the following command to create a SSH tunnel on Raspberry Pi with ngrok:

ngrok tcp 22

The above command will help you retrieve the port forwarding address that can be used to access the Raspberry Pi securely.

Step 9: Remotely Access Raspberry Pi
Open your terminal on another system that could be a Windows laptop, Mac or Linux system and use the following syntax to remotely access Raspberry Pi:

ssh username@<ngrok forwarding address>-p port_no

Here in my case, the Raspberry Pi username is “pi”, ngrok forwarding address is “0.tcp.in.ngrok.io” and port number is 18265. Make sure you must follow the same pattern for the address generated in your case. After entering the desired command, you will be able to access the Raspberry Pi securely on your other system.

How to Start ngrok Service at Startup on Raspberry Pi

If you want to start ngrok service as soon as your system restart, you can use the following steps:

Step 1: Open rc.local File
First, open the system startup file on Raspberry Pi using the following command:

sudo nano /etc/rc.local

Step 2: Add the Command Inside the File
Then add the following command inside the file right at the end:

/home/pi/ngrok tcp 22 --log=stdout > /home/pi/ngrok.log &

Step 3: Then save the file using CTRL+X, add Y and press Enter. Then restart the device to make the changes to Raspberry Pi.

Conclusion

ngrok is an effective service that allows you to securely connect to Raspberry Pi since it does not require configuring any firewall or port forwarding rules. It is an ideal choice for the Raspberry Pi users who want to develop web applications. You can install ngrok on Raspberry Pi by downloading the source file, extracting them inside the /usr/local/bin directory to complete the installation. After the installation, authenticate Raspberry Pi, generate SSH key, edit the SSH file and create an SSH tunnel. Once done, you can then access the Raspberry Pi device with the address provided to you by ngrok.

Share Button

Source: linuxhint.com

Leave a Reply