How to Keep SSH Connections Alive In Linux

ssh-alive-featured

For those that need to work constantly on SSH, it can be very frustrating when the system cut you off after a period of time. This article shows you the method to keep SSH connection alive until you disconnect it.

Login to your remote system, or open the terminal in your own computer.

Open the /etc/ssh/ssh_config file.

and add the following line to the end of the file:

What this option does is to send a null packet to the server at every 60 seconds (of inactivity) to keep the SSH connection alive. You can experiment with the value, setting it to either higher or lower, depending on your system configuration. A value of 60 is a good starting point to start the experimentation.

If you don’t have root access to the system or you just want to configure it for your personal account, you can edit the ~/.ssh/config file instead.

If the file does not exist, this will create a new file.

Add the following line:

Press “Ctrl + o” to save and “Ctrl + x” to exit.

Lastly, restart the SSH server.

That’s it.

12 comments

  1. That is interesting, though I normally just “… -t htop” As long as htop is running, ssh stays open…

    • That will work too. As long as you leave an application running, your SSH connection will always be active.

  2. Nice tip, but how can I restart the SSH service if I have a limited account ? does putting the directive in .ssh/config is parsed on the fly?
    thanks

  3. but now if the network connection between you & the server hangs for a while, you will ‘see’ a dead connection (failed sends) and drop the connection. there is a tradeoff to be made here.

  4. I use Rogers (an awful Canadian ISP), they do SSH throttling and this does not work. :( I am still looking for a way…

  5. http://mosh.mit.edu/ Remote terminal application that allows roaming, supports intermittent connectivity, and provides intelligent local echo and line editing of user keystrokes. BUT requires server installation.

  6. Keep Alive is really a misnomer.. It is really “Check Dead” .. By setting it to 60 seconds it will check to see if the connection still is valid or if there is no route to host.

    So if you have an unstable network connection this will knock you off a lot more than if you don’t set it.

    This has been discussed at length on multiple ssh mailinglists. =)

  7. I use keepalives as mentioned, but totally agree it is more of a “Check Dead”.

    Use it in conjunction with autossh (I prefer the ‘C’ version by harding from motd.ca) and certificate authentication to really keep your ssh session alive. Very handy for port forwarding.

  8. GateOne from LiftoffSoftware provides an automatic means to reconnect to a previous SSH session so you can close your web browser or tab when you leave the house and reconnect to the same session from the office. GateOne is a server that provides an HTML5 client in your web browser.

  9. Sorry for this off-topic comment, but I assume you don’t need sudo when creating/modifying per-user configuration.

  10. Why do you try to restart ssh? ssh_config applies to the ssh client, not sshd, the server. ServerAliveInterval tells the client to make sure the server is alive on a regular basis.

    # chkconfig –list | grep ssh
    sshd 0:off 1:off 2:on 3:on 4:on 5:on 6:off

    # service ssh status
    ssh: unrecognized service

  11. As Paul says, there is no need to restart the server (not your server, and not the one at the remote end). The configuration is purely for the client and will automatically take place the next time you use the ‘ssh’ command to connect somewhere.
    For me it’s useful as a way to keep the connection alive over (in particular) NAT setups where firewalls tend to drop inactive connections after anywhere from ten minutes to (more often) half an hour or more (session becomes unresponsive). So I use 600 seconds (10 minutes). It just needs to see data going over the connection now and then and this is sufficient. IMAP IDLE often uses 24 minutes and seems to get away with it. Anyway, coffe break here we come..

Comments are closed.

Sponsored Stories