1. Install the ngrok Agent
To download and install the ngrok agent on your remote LinuxRaspberry Pi OS device, follow these steps:- Open a terminal into your remote Raspberry Pi OS device.
- Download the latest ngrok binary for your Linux distribution. You can find the correct binary on our ngrok download page: Select your operating system, select the version, and copy the link that appears in the Download button.:
- Unzip the downloaded file and move it to a directory in your PATH. Below is an example for
/usr/local/bin:
- Now that you have installed ngrok on your LinuxRaspberry Pi OS device, link it to your ngrok account by using your authtoken:
NGROK_AUTHTOKEN with your unique ngrok authtoken found in the ngrok dashboard.
2. Enable SSH access
To enable remote SSH access to your device via ngrok:- Test that the ngrok agent is configured correctly by starting a TCP tunnel on your remove device.
TCP endpoints are only available on a free plan after adding a valid payment method to your account. If you get a different error, ensure your authtoken is configured correctly.
- The ngrok agent assigns you a TCP address and port. Use these values to test the SSH access via ngrok by running the following command from another server or from a desktop.
- NGROK_PORT: The port number of the ngrok agent (that is, if the agent shows
tcp://1.tcp.ngrok.io:12345, your port number is12345. - USER: A valid ssh login to access your remote device’s operating system.
- NGROK_TCP_ADDRESS: The address of the ngrok agent (that is, if the agent shows
tcp://1.tcp.ngrok.io:12345, your TCP address is1.tcp.ngrok.io.
3. Adding IP restrictions
Once you confirmed that you have connectivity to the device, add some security so that you are the only one who can access it. Note: This capability requires ngrok’s IP Restrictions feature, which is only available with a paid subscription.-
On the remote LinuxRaspberry Pi OS device terminal, stop the ngrok process using the
ctrl+ccommand. - Add an allow rule to restrict access to your LinuxRaspberry Pi OS device to an IP address or a range of IP addresses.
ALLOWED_IP_ADDRESS_CIDR with the CIDR notation for the allowed IP Address(es) (that is, 123.123.123.0/24).
Alternatively, you can create an IP policy in the ngrok dashboard (under Security > IP Restrictions), and leverage the same policy to control access to your entire device fleet.
4. Configure ngrok to recover on outages
The ngrok agent works with native OS services likesystemd. This helps you ensure that the ngrok service is available even after the machine restarts. Before we do this though, it’s useful to reserve a TCP address in the ngrok dashboard which allows you to reuse the same address each time the device is restarted.
- Navigate to the ngrok Dashboard and access Universal Gateway > TCP Addresses. Create a new TCP address with a description and click Save. Your new TCP address will look something like
1.tcp.ngrok.io:12345.
- Open the ngrok config file:
- Add the following to the end of the file and then save it:
1.tcp.ngrok.io:12345) and ALLOWED_IP_ADDRESS_CIDR with the CIDR notation of the allowed IP Address(es) (that is, 123.123.123.0/24).
Note: Make note of the location of the ngrok.yml file.
- Enable ngrok in service mode:
sudo depending on your system
- Run the following command to ensure your operating system launches ngrok with the ssh ingress whenever your device starts:
sudo depending on your system
- With ngrok running on your device, you should be able to SSH into the device using the reserved address from the dashboard.