Keep SSH connections Alive

Problem

I have a connection that keep timing out every few minutes

Solution

You can make changes on the server and on the client. I preferred to make the changes to the client since the server has more users and may not be not be desirable to make the changes across the board.

  • Create a ssh config file in you home .ssh directory e.g /home/whiscard/.ssh/config

$ vim .ssh/config

  • Add the server details in the file:

Host RemoteServer
HostName remote.server.org
Port 2211
User whiscard
ServerAliveInterval 60
ServerAliveCountMax 5

  • ServerAliveInterval: number of seconds that the client will wait before sending a null packet to the server (to keep the connection alive).
  • ServerAliveCountMax: If the client does not receive a response after five tries (as specified by the ServerAliveCountMax setting), it closes the connection
  • The Host value can be any name you want; it is simply a label for the other settings. The Hostname value is the remote host you want to access. Replace username with your own.
  • Save the file and make it only read and writeable to your user

$ chmod 600 .ssh/config

  • Connect to your account using SSH. To do this, simply type ssh RemoteServer where RemoteServer represents the Host value you specified above.

$ ssh RemoteServer

References

How to disable SSH timeout

How to keep SSH connections alive

Keep SSH session alive

Add ssh key to gihub

First you need to generate your ssh key as described in this post.

Then add the public key to your github account in the settings page

Then test whether you can use ssh with your github account from your linux terminal:


imela@curtsey ~ $ ssh -T git@github.com
Warning: Permanently added the RSA host key for IP address '194.50.23.122' to the list of known hosts.
Hi whiscardz! You've successfully authenticated, but GitHub does not provide shell access.

References

Testing your SSH connection

Specify private SSH-key to use when executing shell command with or without Ruby?

Generate ssh keys

Problem

I needed to generate a generic ssh key that I could use on multiple machines as well as on web portals that support ssh keys

Solution

Generate a key that was tired to my email account:


$ ssh-keygen -t rsa -b 4096 -C "your_email@example.com"

Enter a file in which to save the key (/home/you/.ssh/id_rsa): [Press enter]

Enter passphrase (empty for no passphrase): [Type a passphrase]
Enter same passphrase again: [Type passphrase again]

Adding your ssh key to your keyring:

Start the ssh-agent in the background and add your private key to the ssh agent. Incase you used a key with a different name other than id_rsa then edit accordingly:


$ eval "$(ssh-agent -s)"
Agent pid 59566

$ ssh-add ~/.ssh/id_rsa

References

Generating a new SSH key and adding it to the ssh-agent

How To Set Up SSH Keys

How to generate an SSH key pair in Linux?

Git on the Server – Generating Your SSH Public Key