42

Each time putty is closing the session after some time if it is idle.

There is no time parameter on putty, so how can I keep my putty ssh session always Alive?

0

8 Answers 8

61

Enable SSH keep-alives by changing the following setting to a positive value:

PuTTY Configuration dialog's Connection page

A value of 300 should suffice in most cases. (5 minutes.) This causes PuTTY to send SSH null packets to the remote host periodically, so that the session doesn't time out.

Note that we don't want the SO_KEEPALIVE option lower on that page. That is a much lower-level mechanism which is best used only when the application-level protocol doesn't have its own keepalive mechanism. SSH does, so we shouldn't use TCP keepalives in this case.

There are other things that can cause connections to drop, but this is a solid first thing to try. If it doesn't work, you'd need to look into these other things: VPN timeouts, router timeouts, settings changes on the remote SSH server, flaky connections, etc.

6
  • 3
    Alternatively run something that produces continuous output such as top when you are away.
    – LawrenceC
    Sep 6, 2011 at 12:08
  • 2
    @ultrasawblade: That is a duct tape fix; it merely treats the symptom but does not address the true problem.
    – Kevin M
    Sep 7, 2011 at 3:47
  • Does this config applies for Windows CLI? I mean, instead of save sessions, I create links to putty.exe passing login credentials as arguments (putty.exe host -l root -pw password), If not, is there any argument to apply this option through CLI?
    – Mc Kernel
    Dec 12, 2016 at 10:21
  • 3
    This is an excellent solution, especially when you are working on an unstable connection, and you don't have to make a single change to the server (unlike the accepted solution).
    – itoctopus
    Jul 13, 2017 at 7:51
  • Does anyone know how to specify this via cmd line? Nov 20, 2018 at 16:59
18

Another thing to check is if your system is setting the environment variable TMOUT. To check this you can just do:

env | grep TMOUT

or

echo $TMOUT

If it is set, you could change it or unset it. To change the value:

export TMOUT=3600

Where the number is the number of seconds until you get logged out. Otherwise unset it to turn off the feature:

unset TMOUT

Note, it may be that your system administrator has set this for security reasons. So if you are not the system administrator you may want to check this before changing anything yourself.

12

In addition to the other answers, I'd suggest running screen to be able to have session management even if putty does terminate (connection dying, vpn going down, etc.).

8

Check the following option in PuTTY:

enter image description here

1
  • 1
    Sadly, SO_KEEPALIVE seem to only be sent every couple of hours (at least by default) so a better name for them would be "check if dead's" LOL
    – rogerdpack
    Oct 10, 2019 at 20:14
3

Just run this on your putty this make sure, to activate your session for every 10-mins.

 while true; do date; sleep 600; done
2
  • I always run this on my server and it does not work.
    – cokedude
    Nov 14, 2019 at 16:28
  • Adding unset TMOUT in combination with what you said worked for me.
    – cokedude
    Dec 3, 2019 at 0:08
2

If none of the above didn't help,
You have to change your system sshd configs!
NOTE THAT, YOU WILL NEED ROOT PERMISSIONS FOR THIS!

Edit your sshd_config file, in my case it was located /etc/ssh/sshd_config

content was:

ClientAliveInterval 300  
ClientAliveCountMax 0

change to:

ClientAliveInterval 6000  
ClientAliveCountMax 3

Don't forget

service sshd restart
2

You can use the top command in the shell prompt. This will keep your session alive.

2
  • This seems like a workaround rather than a solution.
    – Kusalananda
    Jan 31, 2018 at 12:08
  • 1
    @Kusalananda I think workarounds may be also solutions.
    – peterh
    Jan 31, 2018 at 13:19
1

In putty Connection>SSH menu, use the following value as Remote command: bash --rcfile <(echo 'source ~/.bash_profile; unset TMOUT').

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .