Connecting to EC2 Instance (Ubuntu) via SSH - Best Practices, Reference and Troubleshooting

Connecting to EC2 Instance (Ubuntu) via SSH - Best Practices, Reference and Troubleshooting

Last updated:

Raising your SSH Connection Timeout limit

Increase the time your SSH connection can be "idle" (i.e. not running any commands) before your instance's SSH Server kills it:

On your instance, do:

Open file /etc/ssh/sshd_config

Add the following configuration lines to the end of said file:

ClientAliveInterval 30

Save and close file.

Then restart the SSH Server:

$ sudo /etc/init.d/ssh restart

Taking your key with you as you log into your EC2 instance

Intended for use from a Linux PC

It's very common to use private keys to log into remote servers so that you can disable password log in altogether, which protects you from brute-force attacks from ill-intentioned people.

If you have a cluster setup, however, you may need the same key (say, mykey.pem) you use to connect to your server to connect to other instances that may be accessible from it.

In this case, you would need to copy the key from your local PC to your server and then copy it again to other instances. But there's an easier way to do this:

  • Add the key to the ssh-agent:

    username$local ssh-add /path/to/meykey.pem
    
  • Check it's been added (your output may be slightly different)

    username$local ssh-add -l
    2048 bf:dd:59:e7:38:d6:cf /path/to/mykey.pem (RSA)
    
  • SSH into your instance using the -A modifier and check your key goes with you

    username@local ssh -A username@path.to.remote.server
    username@remote ssh-add -l
    2048 bf:dd:59:e7:38:d6:cf /path/to/mykey.pem (RSA)
    

See also