Permanent SSH tunnel
How to create and permanent SSH tunnel which is unbreakable and ensure that each time Raspberry Pi A boots or reboots an SSH tunnel to Raspberry Pi B is established automatically?
In case the SSH session will be interrupted for whatever reason, it should be automatically re-established. This can be achieved with help of screen and autossh.
Raspberry pi A with Raspbian OS.
Raspberry pi B with Raspbian OS reachable from the internet via Dynamic DNS.
Raspberry pi A:
- sudo apt-get update
- sudo apt-get upgrade
- sudo apt-get install screen
- sudo apt-get install autossh
- SSH login without password (Ensured that from rpi A to rpi B its possible to login without password)
- crontab -e
- @reboot /usr/bin/screen -S revere-ssh-tunnel -d -m autossh -i /home/user/.ssh/id_rsa -o “ServerAliveInterval 60” -o “ServerAliveCountMax 3” -R 10000:localhost:22 email@example.com -p 22
- sudo reboot
- screen -ls (way to check if screen session with name reverse-ssh-tunnel exists)
Rasberry pi B:
- netstat -nat (way how to check if the tunnel was established and the raspberry pi B is listening on port 10000 )
- ssh user@localhost -p 10000 (So it is possible to connect to raspberry pi A)