Autossh
I keep forgetting where I put this note.
If you have a dedicated machine on the internet, and you also have other machines that you would l ike to "get back to" from the main machine on the internet, most of the time you have limited choi ces due to the ISPs putting restrictions on your inbound ports.
This is where things like autossh can come in handy. One can setup a ssh connection from the remot e machine to the dedicated machine and then from the dedicated machine "reverse ssh" back into the remote machine.
The dedicated machine has a static IP (or dynamic DNS) and inbound ports are not blocked by your U ISP. The remote is just some home box, so inbound is blocked and your IP changes. You have a user on dedicated (and remote) called youruser. You have setup ssh shared keys between remote and dedic ated.
So basically, from the remote host, you want to create a script, in something like
/usr/local/sbin/
perhaps call it todedicated.sh that contains:
#!/bin/bash
/bin/su -s /bin/bash -c '/usr/bin/autossh -M20406 -f youruser@dedicated (optional ssh parameters go here) -N -o "ServerAliveInterval 60" -o "ServerAliveCountMax 3" -R 19998:localhost:22 youruser
Now, you can either start this by hand, or start it with rc.local if you are on an SysV type init system or if you want to do something like systemd (bah, boo!) you can write a systemd file like t his:
The file is called
/etc/systemd/system/autossh.service
And it would contain something like
[Unit]
Description=Autosshtoplate
After=network-online.target
[Service]
ExecStart=/usr/local/sbin/todedicated.sh
Type=forking
[Install]
WantedBy=multi-user.target
Now you could have to set that up in systemd with:
systemctl enable autossh.service
and then start it with:
systemctl start autossh.service
I suggest checking it by hand first. Now when the remote reboots, this autossh will be running. On the dedicated server, if you want to "get back" to the remote server, you would login and do:
ssh 19998 localhost
Tags: linux, autossh, ssh, remote host