How to increase SSH Connection timeout?

May 27, 2017 156.8k views
Linux Basics Ubuntu 16.04

Hi everyone! I have an issue about SSH connection.
Previously I set /etc/ssh/ssh_config as ConnectionTimeout 0 but still closes connection. So Am I missing something? How can I keep alive connection? What can I do anything else client-side? (I use Hyper.js on MacOSX) (Droplet: Ubuntu 16)

Edit: There is no timeout sFTP. While using Atom.

3 Answers


/etc/ssh_config is the client side configuration file not the server side config file.

To prevent all your clients from timing out you need to edit /etc/sshd_config which is the server side configuration file add these two options:

ClientAliveInterval 120
ClientAliveCountMax 720

The first one configures the server to send null packets to clients each 120 seconds and the second one configures the server to close the connection if the client has been inactive for 720 intervals that is 720*120 = 86400 seconds = 24 hours

I hope this helps.

  • @selcukahmet @Mohsen47

    As a general note, you may also need to set these configuration options up on your end, as in on your MacBook. You can do this by creating a config file in ./ssh, i.e.:


    You can add:

    Host *
        ClientAliveInterval ...
        ClientAliveCountMax ...
        ServerAliveInterval ...

    Keep in mind, if you’re MacBook goes in to sleep mode, you shut the lid, or a screensaver pops up, you may still end up with a closed connection.

  • Please fix your answer, you need to edit /etc/ssh/sshd_config

    • You are right I need to fix the path but I cannot find any button to edit my answer,

      The path should be /etc/ssh/sshd_config

  • No. It didn’t help at all.
    Even I used the 2nd path you told in the comment below (/etc/ssh/sshd_config).

    It didn’t succeed, even when I entered the (almost) similar lines to ~/.ssh/config file, as told in the link shared by @larrywachira down here.

    Kindly help.

I have set up the sshd_config files as shown above, and I still get logged out pretty quickly. The login to the Digital Ocean’s system production system looks much the same as the login to my development system. After being logged out by D.O. the host name in the title bar still reads D.O. The other day I was checking some files I had on my D.O. production system, validating them against my local git archives? I had expected differences as the system wasn’t behaving, but there were NO differences. Only later did I realize that D.O. must have logged me off w/o my realizing it, and I was checking against my own files.
These logoffs are no joke. The virtual terminal provided by D.O. is too slow to use. Hopefully someone can give me a hand. Perhaps the tools provided by Atom will help.

I’m not sure if it’s an option for you, but an SSH connection with an active X11 tunnel will not close. If you run nautilus or gedit or something through an X11 connection you won’t have this problem.

Have another answer? Share your knowledge.

You can type !ref in this text area to quickly search our full set of tutorials, documentation & marketplace offerings and insert the link!