3

I have a Raspberry Pi 2 running behind a firewall that prevents inbound SSH connections, so I get the Raspberry Pi to create a reverse SSH tunnel to an external server so that I can get an SSH connection to the Pi via that external server. The command the Pi runs is something like this:

#!/bin/bash

while true; do
    ssh -R 19998:localhost:22 user1@www.user1website.pro
sleep 30
done

Then, on the server to which it connects, I can access the Pi using a command like the following:

ssh -X pi@localhost -p 19998

What I want is for the Raspberry Pi simply to boot to its terminal and then to run automatically this connection procedure for the user pi, i.e. not as root. What would be a good way to do this?

1 Answers1

1

Let's say the path to that script is /home/pi/bin/tunnel.sh.

Add this to /etc/rc.local:

export PATH=/usr/local/bin:/usr/local/sbin:/bin:/sbin:/usr/bin:/usr/sbin

( exec sudo -H -u pi /home/pi/bin/tunnel.sh ) &

If this last line doesn't work, you could try instead:

nohup sudo -H -u pi /home/pi/bin/tunnel.sh &

This may solve issues related to the backgrounding.

And to the top of tunnel.sh:

export PATH=$HOME/bin:$PATH
exec &> /home/pi/tunnel.log
echo Starting $(date)

The purpose of that is explained here.

goldilocks
  • 60,325
  • 17
  • 117
  • 234