SSH Copy ID Fail: Manually Adding Key to EC2 Instance
When working with two created EC2 instances, SSH access from one instance to another may fail, even with a PEM key. This article will guide you through the process of manually adding the key to the EC2 instance.
Prerequisites
- Two created EC2 instances
- SSH access to the first EC2 instance
- PEM key for the EC2 instances
Background
When creating two EC2 instances, the standard process for SSH access from one instance to another involves using the PEM key. However, sometimes the SSH copy ID command fails, and manual intervention is required. This issue can occur due to various reasons, such as a misconfigured security group or incorrect permissions on the PEM key.
Manually Adding Key to EC2 Instance
To manually add the PEM key to the EC2 instance, follow these steps:
- Log in to the first EC2 instance using SSH.
- Create a new directory for the PEM key.
- Copy the PEM key to the newly created directory.
- Change the permissions on the PEM key.
- Create a new file named
authorized_keysin the.sshdirectory. - Append the contents of the PEM key to the
authorized_keysfile. - Change the permissions on the
authorized_keysfile. - Change the ownership of the
.sshdirectory and theauthorized_keysfile.
mkdir ~/.ssh
cp /path/to/pem/key ~/.ssh/id_rsa
chmod 600 ~/.ssh/id_rsa
touch ~/.ssh/authorized_keys
cat ~/.ssh/id_rsa >> ~/.ssh/authorized_keys
chmod 600 ~/.ssh/authorized_keys
chown -R ec2-user:ec2-user ~/.ssh
Testing SSH Access
After manually adding the PEM key to the EC2 instance, test SSH access to the second EC2 instance. If the SSH access is successful, the manual key addition process is complete.
References
- Amazon Web Services. (2022). Amazon EC2 Key Pairs
- Amazon Web Services. (2022). Prerequisites for Connecting to Your Instance
This article covered the topic of manually adding a PEM key to an EC2 instance when the SSH copy ID command fails. By following the steps outlined in this article, you can establish SSH access between two EC2 instances.