permanent ssh tunnel


Permanent SSH tunnel

Goal:

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.

Scenario:

Raspberry pi A with Raspbian OS.

Raspberry pi B with Raspbian OS reachable from the internet via Dynamic DNS.

Solution:
Raspberry pi A:
  1. sudo apt-get update
  2. sudo apt-get upgrade
  3. sudo apt-get install screen
  4. sudo apt-get install autossh
  5. SSH login without password (Ensured that from rpi A to rpi B its possible to login without password)
  6. 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 user@server.com -p 22
  7. sudo reboot
  8. screen -ls (way to check if screen session with name reverse-ssh-tunnel exists)
Rasberry pi B:
  1. netstat -nat (way how to check if the tunnel was established and the raspberry pi B is listening on port 10000 )
  2. ssh user@localhost -p 10000 (So it is possible to connect to raspberry pi A)




Category: Raspberry pi

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.