Warning: Undefined array key "ssba_bar_buttons" in /data/f/0/f0e71dcf-4f5a-4882-8c33-9944a28d7ed9/matusbankovic.com/web/wp-content/plugins/simple-share-buttons-adder/php/class-buttons.php on line 602
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:
- 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 user@server.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)