My mum has a wee laptop that needs, when it boots up, to reverse-SSH connect itself to a server (so I can help out when needed).
I'm struggling to get the connection to persist when it is started by systemd
.
In /reverse_SSH.sh
I've got something like this:
#!/bin/bash
while true; do
ssh -R 19123:localhost:22 [email protected]
sleep 1000
done
In /etc/systemd/system/reverse_SSH.service
I've got something like this:
[Unit]
Description=reverse-SSH
[Service]
Type=fork
ExecStart=/reverse_SSH.sh
[Install]
WantedBy=multi-user.target
When I run sudo systemctl start reverse_SSH
, the SSH connection does appear to happen, and the MOTD of the server can be seen in the output of sudo systemctl status reverse_SSH
, but the SSH connection doesn't seem to persist and I can't access the machine on the server with a command like ssh sonnyboy@localhost -p 19123
.
What nonsense am I doing wrong? Thanks!
I would prefer to use the
autossh
package for such service. So first install it:Then create configuration entry for your connection, by creating a new file within the directory
/etc/ssh/ssh_config.d/
. Let's call itreverse.ssh.www.example.org.conf
. Here is an example:Finally create the service:
Enable and start the service:
I've used this configuration for last two years in order to solve similar task - it works like a charm. However, here are few references: