Haifeng Zhang Haifeng Zhang - 5 months ago 12x
Linux Question

Keep SSH session alive

I use

ssh -p8520 remote_user@remote_host
to login remote server.

It works properly when I'm in my workplace, it can be connected in a day or more, but the terminal freezes in 10 - 15 minutes when I am logged into the remote server at home, which is really annoying me. There's no error/timeout report, the cursor in the terminal cannot move any more. The only thing I can do is close the terminal and reconnect.

When enter w to check the login users, some zombies login users are there, and I have to kill them manually.

This is quite annoying. Can anyone help me?


sshd (the server) closes the connection if it doesn't hear anything from the client for a while. You can tell your client to send a sign-of-life signal to the server once in a while.

The configuration for this is in the file "~/.ssh/config", create it if the configuration file does not exist. To send the signal every four minutes (240 seconds) to the remote host, put the following in your "~/.ssh/config" file.

Host remotehost:

HostName remotehost.com
ServerAliveInterval 240

This is what I have in my "~/.ssh/config":

To enable it for all hosts use:

Host *
ServerAliveInterval 240

Also make sure to run:

chmod 600 ~/.ssh/config

because the config file must not be world-readable.