This is probably a stupidly simple question to some :)
I\'ve created a new linux instance on Amazon EC2, and as part of that downloaded the .pem file to allow me to
Do a chmod 400 yourkeyfile.pem If your instance is Amazon linux then use ssh -i yourkeyfile.pem ec2-user@ip for ubuntu ssh -i yourkeyfile.pem ubuntu@ip for centos ssh -i yourkeyfile.pem centos@ip
Please ignore this answer if it is irrelevant for you, but from my experience I've seen people having an issue with Permission denied (publickey)
because they simply pasted their public key (on a target machine) without the first letter!
This happens when using vim to edit (paste) the key. Since vim by default opens in command mode (not in an insert mode), pasting the key without switching to an insert mode (i.e. i
) will result in skipping the first s
letter, e.g. instead of
ssh-rsa <key>
you end up pasting
sh-rsa <key>
So before trying other solutions, see if you've pasted your key correctly! i.e.
cat ~/.ssh/id_rsa.pub
Only if you're certain, perform the next steps; trying to ssh in a verbose mode (i.e. flag -v
) might point you to the actual issue:
ssh -v -i <private_key> <name>@<ip> -p <port>
As a side note, as it has been already mentioned here by others, in majority of cases starting an empty ssh agent (program that keeps your keys in memory) and adding your key should resolve the issue:
ssh-agent bash
ssh-add <private_key>
Just change the permission of pem file to 0600 allowing only for the allowed user and it will work like charm.
sudo chmod 0600 myfile.pem
And then try to ssh it will work perfectly.
ssh -i myfile.pem <<ssh_user>>@<<server>>
Following are the simple steps for Linux user to connect with the server using .pem file:
Step1: To to the location of pem file and copy it to home .ssh location.
cp example.pem ~/.ssh/example.pem
Step2: Change the permission
chmod 400 ~/.ssh/example.pem
Step3: Run the following command
ssh -i ~/.ssh/example.pem ec2-user@host.com
As this command is too long so you sould create the alias of this using following commands:
vim ~/.bashrc
Write the same command in the following manner at the last.
alias sshConnect='ssh -i ~/.ssh/example.pem ec2-user@host.com'
Now restart your system and use sshConnect
to connect with your server.
I have seen two reasons behind this issue
1) access key does not have the right permission. pem keys with default permission are not allowed to make a secure connection. You just have to change the permission:
chmod 400 xyz.pem
2) Also check whether you have logged-in with proper user credentials. Otherwise, use sudo while connecting
sudo ssh -i {keyfile} ec2-user@{ip address of remote host}
What fixed this for me was to move the .pem file within the apps directory. Soo say fooapp is the name of my app. I placed it directly in there.