Ubuntu – How to prevent autossh from becoming stuck once in a while


I am using autossh to build a reverse ssh tunnel but from time to time the tunnel stops working and I need to kill autossh and start it again.

@reboot autossh -f -nNT -R 3269:intranet.example.com:3269 public.example.com &

autossh seems to remain in a strange state, where the forwarded port is still open but you get no response from the other side. By restarting autossh this is solved.

How can I prevent this problem from occurring?

Best Answer

Use the ServerAliveInterval (with a value in seconds) make the ssh client send a null (keepalive) packet over the encrypted channel every so often in order to detect a broken connection:

@reboot autossh -f -nNT -R 3269:intranet.example.com:3269 -o ServerAliveInterval=30 public.example.com &

You should probably also set the corresponding ClientAliveInterval setting in /etc/ssh/sshd_config on your server to make the server drop dead client connections too:

# Drop dead client connections after 10 minutes of inactivity
ClientAliveInterval 600