SSH between two EC2 instances?

Let’s say I have two EC2 instances A and B.

They both use the same private key file (file.pem), the same security group and are in the same region.

To connect to instance A from host: ssh -i “file.pem” A-user@A-address

To connect to instance B from host: ssh -i “file.pem” B-user@B-address

The question is, Is there a way to connect from instance A to instance B through ssh without copying file.pem to instance A?

I’d like to only have the key file in the host machine and somehow configure the two instances to be able to connect from one to the other (not sure if this is possible, that’s why I’m asking).


Related discussions:

Sounds like you want to read up on ssh agent forwarding.

In a nutshell the sshagent on your machine holds your keys, but when you’ve sshed to A, sshing toB on that instance can ask your computer’s ssh agent to do the operations requiring the private key. A and B can have different keypairs

Ssh does also support host based authentication, but that’s a different setup.

You are probably looking for Agent Forwarding.

Are you asking if:

  • you can connect to A, and then B through A?
  • you can setup A to connect to B (for Cron, scp, etc)?

Ssh agent forwarding is the answer. Also, checkout https://userify.com we use at work and no pem keys are shared around.

Thanks a lot!

Do this…

On your local machine → ssh-add -i my.pem

Then ssh -A A-user@A-address. Then when you are on the box, do a B-user@B-address.

Then Bob’s your uncle.

In addition to the already mentioned agent forwarding, you should also look into setting up your ssh config file to use proxy command. This is useful to set up ssh connections you do often.

Host jumpbox
User ec2-user
HostName 123.45.67.89
IdentityFile ~/.ssh/id_rsa.pem

Host box2
User ec2-user
HostName 10.1.1.1
IdentityFile ~/.ssh/id_rsa2.pem
ProxyCommand ssh jumpbox nc %h %p %r

So what we’ve done here is set up our login to a jumpbox (or bastion host). Then we’ve set up our login to a second box, using the jumpbox as a proxy. We can set a different IdentityFile here, and it’ll work fine. However, unlike with agent forwarding, I won’t be able to use that key to jump into another box. I could fix that by adding AddKeysToAgent yes in both entries.

Edit: Please look at /u/UnrealQuester’s comment below, for a new and improved ProxyJump command that I didn’t know about until now.

Either way is good for me. I just need a quick and efficient way for A to communicate with B (either by connecting first to A, or just setting it up to connect to B).

A solution that is inefficient would be for example communicating through S3. A writes things somewhere in S3 and B reads the data and that’s how they communicate.

An efficient way would be to let A connect to B through ssh. But I want to avoid sharing the private key (avoid copying B’s private key to A).

Based on all the other answers it seems that ssh agent forwarding would solve the first option, do you have a solution for the second one?

Thank you!

Thank you! I’ll look into it. Sounds like a helpful tool.

Thank you!

If they are both ec2s and use the same AMI and keypair, it should just work without any additional configuration assuming your security groups are open. With Amazon Linux, for example, the same keypair could be assigned to ec2-user on both and would allow SSH from either into the other without agent forwarding, but you will need agent forwarding enabled so your local machine private key can be forwarded to the second SSH target.

edit: just finished morning coffee. Obviously the private key needs to be in place on both A and B systems somehow, whether through agent forwarding or other means.

Not really. In order to SSH from one to the other you’d still need the keypair, and I want to avoid copying the keypair from my host machine to one of the instances. That’s why I need agent forwarding.

Right, you would still need the private key on both hosts, which doesn’t happen automatically. My mistake. But, if you simply add -o ForwardAgent=yes to your SSH command then it should work right out of the box, so to speak.

You just add the -A flag to carry the key with you for the next hop.

But! If you want the machines to communicate while you’re not there it doesn’t help one bit. This only works when you bring the key with you.

If you want A to be able to connect to B without you, you can generate an ssh key in A, and add the public key in B under .ssh/authorized_keys and vice versa.

Also be sure to restrict what ip’s can connect to your instances with proper security groups. You do not want a publicly open ssh server in AWS.

Add the public key of InstanceA to the authorized_keys file of InstanceB

You can now SSH from InstanceA to InstanceB without any further prompt for authorisation required

This is the best solution. The jump host is often referred to as a “bastion” host.

I’ve got everything set up so that all servers share the same key but they are inaccessible from the internet. The bastion host is the only point of entry and requires a personal key.

That way if someone quits the company I don’t have to change the regular key, I just have to delete the personal one from the bastion host.

The bastion also only allows ssh from our VPN connection.

Please never give your private pem file to an ec2 instance. Please.

I wouldn’t. But thanks for the glaringly obvious pointer.