Enabling SSH access to your AWS EC2 instances to users outside your account

You've setup your AWS EC2 instance, everything is up and running. Now, you need to give access to a friend to collaborate with, or perhaps a client needs to SSH in for housekeeping. Here's how to enable SSH access

Enabling SSH access to your AWS EC2 instances to users outside your account
Photo by Mehmet Ali Peker / Unsplash

While you usually create or reuse an existing key pair when launching a new EC2 instance, you may often find yourself having to provide share SSH access to someone outside your AWS account. Setting this up is fairly straightforward. This approach is especially useful, if after sharing access you want to control or stop someone from gaining access in the future.

First, create a new key pair in the EC2 console. Your region may vary, but the link usually resembles https://us-west-1.console.aws.amazon.com/ec2/v2/home?region=us-west-2#KeyPairs: Let's call it second-key.pem

Creating a key pair in the EC2 console

You will need to update the file permissions of the downloaded key.

chmod 400 second-key.pem
💡
Ensure that the key you are about to create, is in the the same region as the EC2 instance to which you will be providing access

From the downloaded private key created in the previous step, you need to obtain the public key. Do this using:

ssh-keygen -y -f ~/Downloads/second-key.pem

Next, we need to add the public key that we just created, to your EC2 instance. This is done by appending the contents of the it to ~/.ssh/authorized_keys. Paste the public key (below the existing one, if so) using a plain text editor.

If needed, restart ssh daemon or the EC2 instance itself.

That's it. SSH into the instance using the same username as before, but this time, use the second private key.

Managing SSH configurations, remote servers, SSH keys
Understanding the SSH config file, managing multiple SSH configurations, conventions and other settings.
Quick reference of commonly used Linux commands
Looking to find the size of a directory? Or hunt for a rogue running process and terminate it? Here’s a list of commonly used linux commands along with examples