Skip to content

Instantly share code, notes, and snippets.

@hongsw
Last active May 24, 2022 08:19
Show Gist options
  • Save hongsw/dde616f5d43f71ae4fda247e559cbe89 to your computer and use it in GitHub Desktop.
Save hongsw/dde616f5d43f71ae4fda247e559cbe89 to your computer and use it in GitHub Desktop.
Add a new authorized key to an AWS EC2 instance by a new .pem file
chmod 400 new.pem
ssh-keygen -y -f new.pem > new.pub
cat ./new.pub | ssh -i old.pem USER@HOST "cat >> ~/.ssh/authorized_keys"
# Test
ssh -i new.pem USER@HOST
@hongsw
Copy link
Author

hongsw commented May 24, 2022

If you went to add a new user and .pem file at one time,

cat ./new.pub | ssh -i old.pem USER@HOST "sudo adduser newuser && sudo mkdir -p /home/newuser/.ssh && sudo tee /home/newuser/.ssh/authorized_keys"

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment