Permission Denied Error while copying SSH Public Key to EC2 Server

0

Hi AWS, I am trying to copy a SSH public key (/root/.ssh/id_rsa.pub) from my local machine to the EC2 server (Linux) under the location (/root/.ssh/authorized_keys) using root user (sudo su) and while I am firing the command ssh-copy-id -i ~/.ssh/id_rsa.pub ec2-user@ec2-11-222-333-444.compute-1.amazonaws.com, I am getting the error provided in the screenshot below.

Copy SSH public key to EC2 server

The purpose of doing this is for Ansible testing by running adhoc commands to check ping-pong connection.

Please guide what could be the issue. I know it's something related to linux file permissions but I am not sure about that.

1 Answer
1
  1. Use SSH Agent to Manage Your Keys If you haven't already, start the SSH agent in the background and add your existing private key (the one you currently use to access your EC2 instance) to the agent. This step is necessary to facilitate password-less login via the SSH protocol.
eval $(ssh-agent -s)
ssh-add /path/to/your_existing_private_key

Replace /path/to/your_existing_private_key with the actual path to your private key file.

  1. Use ssh-copy-id to Copy the New Public Key Now, you can use ssh-copy-id to copy the new public key to your EC2 instance. Specify the -i option with the path to the new public key you wish to copy.
ssh-copy-id -i /path/to/new_public_key.pub ec2-user@your-ec2-public-dns

Replace /path/to/new_public_key.pub with the path to your new public key file and your-ec2-public-dns with the public DNS or IP address of your EC2 instance. The default user name for Amazon Linux instances is ec2-user, but this may vary depending on the AMI used (for example, ubuntu for Ubuntu AMIs).

profile picture
EXPERT
answered a month ago
profile picture
EXPERT
Artem
reviewed a month ago

You are not logged in. Log in to post an answer.

A good answer clearly answers the question and provides constructive feedback and encourages professional growth in the question asker.

Guidelines for Answering Questions