Is it possible to keep SSH session alive while computer sleep? When I put my Mac (Lion) to sleep for a short period of time and then wake it up session still alive, but if I leave it overnight session connection is closed.
I've tried set:
ClientAliveInterval 3600 ClientAliveCountMax 10
same result
Why session "survive" for a short period of time? How can I control this timeout?
The only problem is, SSH doesn't work on any of these OS's unless you prevent the computers from entering sleep mode. While researching a solution it appears that there are a couple decent programs, but I've only come across platform-specific solutions.
When you log in to the server, the terminal session won't automatically close. Instead, the configuration file will keep sending the alive signal after the specific interval set in the configuration file to keep the terminal session alive.
I found the answer it depends on tcp keepalive settings:
For the list of available TCP settings (FreeBSD 4.8 an up and 5.4):
sysctl -A | grep net.inet.tcp
net.inet.tcp.keepidle
- Amount of time, in milliseconds, that the (TCP) connection must be idle before keepalive probes (if enabled) are sent.
net.inet.tcp.keepintvl
- The interval, in milliseconds, between keepalive probes sent to remote machines. After TCPTV_KEEPCNT (default 8) probes are sent, with no response, the (TCP)connection is dropped.
net.inet.tcp.always_keepalive
- Assume that SO_KEEPALIVE is set on all TCP connections, the kernel will periodically send a packet to the remote host to verify the connection is still up.
Therefore formula to calculate maximum TCP inactive connection time is following:
net.inet.tcp.keepidle + (net.inet.tcp.keepintvl x 8)
the result is in milliseconds. Therefore, by setting
net.inet.tcp.keepidle = 10000
net.inet.tcp.keepintvl = 5000
net.inet.tcp.always_keepalive = 1 (must be 1 always)
the system will disconnect a call when TCP connection is dead for: 10000 + (5000 x 8) = 50000
msec (50 sec). To make system remember these settings at startup, you should add them to /etc/sysctl.conf
file
If you want an elegant solution for keeping a remote session alive, you should check the screen
utitity. When using this utility you can keep your remote programs running even after you logout or shutdown your computer. Then you can reconnect your available screen
session later.
Screen requires some time to learn the basics, but when you got the handle of it, you will see how powerful it is.
You can check this link for a tutorial on using screen: http://www.thegeekstuff.com/2010/07/screen-command-examples/
Another possible solution is mosh, which isn't exactly SSH but does keep your shell connection alive when your computer goes to sleep, or even after changing IP addresses.
Another solution is to use autossh, which basically monitors your ssh session and reconnects as soon as it is interrupted (e.g. after a sleep cycle, but also if you lose a WiFi connection for a while, for instance). This works especially well for forwarded ports/ssh tunnels.
Here is a tutorial: http://linuxaria.com/howto/permanent-ssh-tunnels-with-autossh.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With