
🌐 이 글의 한국어 원문이 있습니다.한국어 원문 보기 →
Accessing a Raspberry Pi Installed at a Client Site
Every now and then I want to put a server or a Raspberry Pi on a client's premises. The problem is that getting into the client's internal network always meant dealing with port forwarding.
Recently I had to develop on a Raspberry Pi again, and I was thinking about an easy way to solve this problem. That's when I learned about something called 'reverse SSH tunneling'…
Server → Raspberry Pi (external IP, 123.123.123.123)
Normally in this situation, you connect from the server to the Raspberry Pi like this.
user@server~$ ssh rasp@123.123.123.123But to do this, the Raspberry Pi's SSH port has to be exposed to the outside. There's also the risk that the tiny Raspberry Pi has to endure countless hacking attempts all on its own.
On top of that, when the Raspberry Pi goes into the client's 'internal network,' you could only connect if the client set up forwarding for you.
And I, foolishly, had only just now discovered this newfangled thing (?) called SSH reverse tunneling.
Raspberry Pi → Server (external IP: 100.100.100.100)
Reverse tunneling works the other way around: it lets you reach the Raspberry Pi's SSH through an external server.
rasp@rasp~$ ssh -R 2222:localhost:22 user@100.100.100.100It looks as if the Raspberry Pi is connecting to the server's SSH. But what it actually means is, "Let the user account on the server connect to the Raspberry Pi through port 2222 'on the server.'"
After running this command on the Raspberry Pi, you can connect from the server like this.
user@serv~$ ssh localhost -p 2222The server tries to SSH into its own port 2222. And then, amazingly, you witness the rare sight of it connecting to the Raspberry Pi…
Let's look at the parameters of the command we ran on the Raspberry Pi earlier.
rasp@rasp~$ ssh -R $tunneling_port:$reversed_ip:$rasp_port $server_user@$server_ip-
$tunneling_port: Sets 'which port' the server will use to connect to the Raspberry Pi. In other words, the 'Raspberry Pi' decides 'which port' on the server will be used. -
$reversed_ip: Sets which address the server 'passes' the SSH connection on to. Above, I putlocalhosthere. In other words, "If you come to me through port 2222 on the server, I'll connect you to localhost's SSH."- With this, you can connect not only to the Pi itself but also to the SSH of other servers around the Raspberry Pi. In effect, the Raspberry Pi can become an SSH relay.
-
$rasp_port: The SSH port configured on the Raspberry Pi. -
$server_user: Specifies 'which user' the tunnel comes through. -
$server_ip: The server's externally exposed IP address.- If the server's SSH port isn't 22, just add
-p $server_portat the end.
- If the server's SSH port isn't 22, just add
With this, the Raspberry Pi asks the SSH on the 'server' to 'set up a tunnel.'
If you add the following to the Raspberry Pi via crontab -e, the tunnel will be set up every time the Raspberry Pi reboots.
@reboot ssh -R 2222:localhost:22 user@100.100.100.100Alternatively, you can use autossh so that it reconnects automatically even if the tunneling session drops.
Now I'm ready to admit it.
I've been a fool all this time….
*For reference, even with tunneling in place, trying to SSH into port 2222 on the server from the outside won't work. Tunneling only happens when you connect to port 2222 on 'localhost' from inside the server (TEST: UBUNTU 20.04).
Beyond that, as long as proper security settings are in place, you should be able to operate devices on external networks comfortably.
Also, even with tunneling, the access credentials (authentication keys) have to be held by the 'server.' Just coming through the tunnel doesn't mean the door opens unconditionally, so rest assured.

