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.

  • please accept the answer if it was useful for you

profile picture
已提問 8 個月前檢視次數 558 次
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
專家
已回答 8 個月前
profile picture
專家
已審閱 7 個月前

您尚未登入。 登入 去張貼答案。

一個好的回答可以清楚地回答問題並提供建設性的意見回饋,同時有助於提問者的專業成長。

回答問題指南