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回答
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ヶ月前

ログインしていません。 ログイン 回答を投稿する。

優れた回答とは、質問に明確に答え、建設的なフィードバックを提供し、質問者の専門分野におけるスキルの向上を促すものです。

質問に答えるためのガイドライン

関連するコンテンツ