Whether you’re setting up a new EC2 instance or recovering from a lost .pem key, this guide covers the essential steps - plus easy SSH config tricks for smoother access.
.pem file.chmod 400 /path/to/downloaded-key.pem
Lost your private key? Here’s the way back in.
.pem file from the EC2 dashboard.chmod 400 /path/to/new-key.pem
.pem file to extract the corresponding public key:ssh-keygen -y -f /path-to/key-file-pair.pem -t rsa -b 4096
authorized_keys on the EC2 InstanceUse EC2 Instance Connect (browser-based login) or any other method to access the server and run:
echo "your-new-public-key" >> ~/.ssh/authorized_keys
You’re back in.
nano ~/.ssh/config
Add this block:
Host server
HostName [instance-IP]
User [username]
IdentityFile /path/to/key-file-pair.pem
Now you can simply run:
ssh server
And you’re in.
.pem File Permissions (Ubuntu + Windows)chmod 400 ~/keys/*.pem
Use icacls to fix key file permissions:
icacls "C:\Users\hp\Documents\keys\*.pem" /reset
icacls "C:\Users\hp\Documents\keys\*.pem" /grant:r %USERNAME%:F
icacls "C:\Users\hp\Documents\keys\*.pem" /remove "Users" "Authenticated Users" "Everyone" /T /C
Proper permissions help avoid "unprotected private key" errors when using SSH on Windows via WSL, Git Bash, or other tools.
.pem files somewhere safe. These are your literal keys to the kingdom.chmod 400 or set ACLs, or SSH will scream at you.