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.

profile picture
已提问 2 个月前324 查看次数
1 回答
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
专家
已回答 2 个月前
profile picture
专家
Artem
已审核 1 个月前

您未登录。 登录 发布回答。

一个好的回答可以清楚地解答问题和提供建设性反馈,并能促进提问者的职业发展。

回答问题的准则