Leaderboard ad


How To Stop Your SSH Session From Disconnecting

Question sent in by: Augustino from Tallahassee. 

Q: When I connect to one of my servers via SSH, after about 10 minutes of inactivity my connection is closed.  How do I stop this from happening.

A: You can configured your SSH client to send a small packet called no-op (no operation). The no-op is basically tells the other system "Nothing to do" but in communicating with the system tells SSH that your alive on the other side, thus not closing the TCP connection and logging you out.

To configure this for your current username only, add the following line to "/home/user/.ssh/config" or "~/.ssh/config" (the tilde "~" is just an alias for your home directory):

Host *
 ServerAliveInterval 60

NOTE: Make sure to indent the second line.

The "Host *" line basically tells the SSH client to apply the settings to ANY host.  Will the number after "ServerAliveInterval" is the amount of seconds to wait to send a no-op.

If you would like to apply this to one host only, you can. You can also adjust the interval seconds to meet your needs.

Host putorius.net
 ServerAliveInterval 300

Now you have to read (or source) the file like so:

source ~/.ssh/config
That configured your SSH client to send a no-op every 60 seconds. This will ONLY effect the one user with this in his/her home directory.

To apply this setting globally (or to ALL users) you can add (or change) the line in /etc/ssh/ssh_config:

ServerAliveInterval 60
NOTE: Make sure you add it to the SSH CLIENT configuration "ssh_config" and not the SSH SERVER configuration "sshd_config".

Let me add, SSH sessions are usually logged out after a certain amount of time for security reasons.  It is a best practice to close idle sessions to avoid unattended access to a system. Many companies have policies that govern the way a system is configured.  Circumventing these policies could be ground for disciplinary actions.

No comments:

Post a Comment