Always use ServerAliveInterval
SSH option in case the tunnel issues are generated by expired NAT sessions.
Always use a respawning method in case the connectivity goes down entirely, you have at least three options here:
autossh program
bash script (
while true do ssh ...; sleep 5; done
) do not remove the sleep command,ssh
may fail quickly and you'll respawn too many processes/etc/inittab
, to have access to a box shipped and installed in another country, behind NAT, without port forwarding to the box, you can configure it to create an ssh tunnel back to you:tun1:2345:respawn:/usr/bin/ssh -i /path/to/rsaKey -f -N -o "ServerAliveInterval 180" -R 55002:localhost:22 user@publicip 'sleep 365d'
upstart script on Ubuntu, where
/etc/inittab
is not available:start on net-device-up IFACE=eth0 stop on runlevel [01S6] respawn respawn limit 180 900 exec ssh -i /path/to/rsaKey -N -o "ServerAliveInterval 180" -R 55002:localhost:22 user@publicip post-stop script sleep 5 end script
or always use both methods.