If you’re using SSH to access your Linux computer from a remote location, you may occasionally experience a problem where the SSH session disconnects unexpectedly. This can be frustrating because it can prevent you from working on your computer or accessing important files. To prevent this problem from happening, follow these steps:

  1. Make sure that your SSH daemon is running properly. If it isn’t, you may experience problems connecting to your computer and disconnecting unexpectedly. To check whether your SSH daemon is running properly, run the following command: sudo systemctl status sshd
  2. Make sure that your sshd_config file is up to date. If it isn’t, you may experience problems connecting to your computer and disconnecting unexpectedly. To update your sshd_config file, run the following command: sudo nano /etc/ssh/sshd_config
  3. Add the following line to the bottom of the file: # Allow clients with no user ID or password to connect # (This is a security feature for high-security environments) # UserKnownHostsFile=/etc/ssh/known_hosts #4. Reload the ssh daemon after making changes to the file: sudo service ssh reload

You can configure the ssh client to automatically send a protocol no-op code code every number of seconds so that the server won’t disconnect you. This is setting is sometimes referred to as Keep-Alive or Stop-Disconnecting-So-Much in other clients.

Global Configuration

Add the following line to the /etc/ssh/ssh_config file:

The number is the amount of seconds before the server with send the no-op code.

Current User Configuration

Add the following lines to the ~/.ssh/config file (create if it doesn’t exist)

Make sure you indent the second line with a space.

Per-Host Configuration

If you only want to enable keep alive for a single server, you can add that into the ~/.ssh/config file with the following syntax:

Works quite well, hope it helps somebody else out there.