Skip to content

Instantly share code, notes, and snippets.

@taufiqpsumarna
Last active September 5, 2024 04:26
Show Gist options
  • Save taufiqpsumarna/5726548056e52a47cdc29ceebd3b3d39 to your computer and use it in GitHub Desktop.
Save taufiqpsumarna/5726548056e52a47cdc29ceebd3b3d39 to your computer and use it in GitHub Desktop.
Restore deleted default AWS EC2 Instance key.pem

A quick note how to add a key from myKey.pem file to authorized keys on Linux.

The permissions for ~/.ssh/ folder should be 700. Permissions for authorized_keys file and myKey.pem are to be 600.

1.Generate a public key first (usually from AWS EC2 key)

ssh-keygen -y -f AnalyticsSharedKeyPair.pem > AnalyticsSharedKeyPair.pub

2.Add a new public key to authorized_keys:

cat id_dsa.pub >> .ssh/authorized_keys

Command Explanation:

ssh-keygen -y -f AnalyticsSharedKeyPair.pem > AnalyticsSharedKeyPair.pub
  1. ssh-keygen: This is a command-line tool used to generate, manage, and convert SSH keys.
  2. -y: This option tells ssh-keygen to read a private key file and output the corresponding public key.
  3. > -f AnalyticsSharedKeyPair.pem: This specifies the input file, which is the private key file (AnalyticsSharedKeyPair.pem). The .pem format is commonly used for SSH keys.
  4. > AnalyticsSharedKeyPair.pub: The > symbol is used to redirect the output (which is the public key) to a file. Here, the public key is saved in the file named AnalyticsSharedKeyPair.pub.

Source: https://dmitrypukhov.pro/adding-ssh-keys-to-authorized_keys/

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