I want to use my Amazon ec2 instance but faced the following error:
Permission denied (publickey).
I have created my key pair and downloaded .pem file.
Given:
chmod 600 pem file.
Then, this command
ssh -i /home/kashif/serverkey.pem [email protected]
But have this error:
Permission denied (publickey)
Also, how can I connect with filezilla to upload/download files?
This question is related to
amazon-web-services
ssh
amazon-ec2
This has happened to me multiple times. I have used Amazon Linux AMI 2013.09.2 and Ubuntu Server 12.04.3 LTS which are both on the free tier.
Every time I have launched an instance I have permission denied show up. I haven't verified this but my theory is that the server is not completely set up before I try to ssh into it. After a few tries with permission denied, I wait a few minutes and then I am able to connect. If you are having this problem I suggest waiting five minutes and trying again.
same thing happened to me, but all that was happening is that the private key got lost from the keychain on my local machine.
ssh-add -K
re-added the key, then the ssh command to connect returned to work.
I was able to SSH from one machine, but not from another. Turns out I was using the wrong private key.
The way I figured this out was by getting the public key from my private key, like this:
ssh-keygen -y -f ./myprivatekey.pem
What came out didn't match what was in ~/.ssh/authorized_keys
on the EC2 instance.
I solved the problem just putting sudo
before
sudo ssh -i mykey.pem myec2.amazonaws.com
But the proper solution is to change the ownership first, and then connect as a normal user as Janus Troelsen said below. In my case it would be:
chown wellington:wellington key.pem
I struggled with this for a while too until I found the following:
eb ssh
When you use that from the project directory, bingo-bango no muss no fuss, you're in
I've twice had keys and ssh command line correct (I know because I'm duplicating a working Ubuntu 14.04 instance), but just not been able to ssh into a new instance, even after waiting 5 minutes as suggested by Wade Anderson above.
I had to destroy and re-create the machine. This has happened on two separate occasions. Since I can't get in initially, I can't see what's wrong.
So, if you have this problem, try that.
In this case the problem arises from lost Key Pair. About this:
You can follow these steps:
In general, remember you have to allow your EC2 instance to accept inbound SSH traffic.
To do this, you have to create a specific rule for the Security Group of your EC2 instance. You can follow these steps.
Hope this can help someone as helped me.
In my own case, i did the following:
chmod 400 <key.pem>
ssh -i <key.pem> ec2-user@ec2_public_dns (for debian)
I was initially using root@
part and i got this prompt:
Please login as the user "ec2-user" rather than the user "root".
All of the top ranked answers above are accurate and should work for most cases. In the event that they don't as was in my case, I simply got rid of my ~/.ssh/known_hosts
file on the machine I was trying to ssh from and that solved the problem for me. I was able to connect afterwards.
for the ubuntu 12.04 lts micro instance i had to set the user name as option
ssh -i pemfile.pem -l ubuntu dns
You need to do the following steps:
cd <path to your .pem file>
chmod 400 <filename>.pem
ssh -i <filename>.pem ubuntu@<ipaddress.com>
If ubuntu
user is not working then try with ec2-user
.
It's case sensitive.
Wrong : SSH EC2-user@XXX.XX.XX.XX -i MyEC2KeyPair.pem
Correct : SSH ec2-user@XXX.XX.XX.XX -i MyEC2KeyPair.pem
When you try doing
ssh -i <.pem path> root@ec2-public-dns
You get a message advising you to use the ec2-user
.
Please login as the user "ec2-user" rather than the user "root".
So use
ssh -i <.pem path> ec2-user@ec2-public-dns
This issue can be solved by login into Ubuntu box using below command:
ssh -i ec2key.pem ubuntu@ec2-public-IP
I had same problem and its very strange. If you believe you are doing all good than follow this: Some times there is confusion about user for the EC2 instance!! Some times you get ec2-user, ubuntu, centos etc. So check your username for the machie!!
Login with root user
ssh -i yourkey.pem (400 permission) root@<ip>
It will throw error and will give you the available username. then login with that user.
i had same error but different situation. to me it happened out of the blue after a lot of time i could ssh successfully to my remote computer out there. after a lot of searching the solution to my problem were file permissions. it is strange of course because i didn't change any permissions in my computer or the remote one belonging to the ssh's files/directories. so from the good archlinux wiki here it is:
For the local machine do this:
$ chmod 700 ~/
$ chmod 700 ~/.ssh
$ chmod 600 ~/.ssh/id_ecdsa
For the remote machine do that:
$ chmod 700 ~/
$ chmod 700 ~/.ssh
$ chmod 600 ~/.ssh/authorized_keys
after that my ssh started to working again without the permission denied (publickey) thing.
My private key was set to permission 400
and was resulting in Permission denied setting it to '644' helped me .
key_load_private_type: Permission denied is the specific error I was getting
Solution:
Sudo chmod 644 <key.pem>
Note: set to 644 is must, it was not working with 400
I forgot to add the username (ubuntu) when connecting my Ubuntu instance. So I tried this:
ssh -i /path/my-key-pair.pem my-ec2-instance.amazonaws.com
and the correct way was
ssh -i /path/my-key-pair.pem [email protected]
Another Possible Issue: Wrong login ID
Check 'Usage Instructions'
All good suggestions above, but what I ran into was that I selected a pre-made instance. After the instance has started , look at the usage instructions. I incorrectly used login id of the private key when in the instructions I was supposed to use 'bitnami' (e.g. bitnami@domain -i key.pem)
I had similar error
debug1: Authentications that can continue: publickey
debug1: Next authentication method: publickey
debug1: Trying private key: xxxx.pem
debug1: Authentications that can continue: publickey
debug1: No more authentication methods to try.
Permission denied (publickey).
My problem was that the instance did not start properly due to error on the run-on-start-up script from Step 3: Configure instance detail
under Advanced details:
What I thought I entered:
#include?
https://xxxx/bootstrap.sh?
What actually entered breaks the instance setup
#include
https://xxxx/bootstrap.sh
So the public key on instance side was not created
Here is a possible frustrating scenarios that produces this error:
If you are lunching a new instance from an AMI you created of another instance (say instance xyz), then the new instance will only accept the same key that instance A used. This is totally understandable but it gets confusing because during the step by step process of creating the new instance, you are asked to select or create a key (at the very last step) which will not work.
Regardless of the key you create or select, only the key you were using for instance XYZ will will be accepted by the new instance.
It's a basic thing, but always confirm which user you are trying to do the login. Im my case was just a distraction. I was trying using a root user:
ssh -i ~/keys/<key_name> [email protected]
But was another user:
ssh -i ~/keys/<key_name> [email protected]
I struggled with the same permission denied error apparently due to
key_parse_private2: missing begin marker
In my situation the cause was the ssh config file of the current user (~/.ssh/config).
Using the following:
ssh -i ~/myKey.pem ec2-user@<IP address> -v 'exit'
The initial output showed:
debug1: Reading configuration data /home/ec2-user/.ssh/config
debug1: Reading configuration data /etc/ssh/ssh_config
debug1: /etc/ssh/ssh_config line 56: Applying options for *
debug1: Hostname has changed; re-reading configuration
debug1: Reading configuration data /home/ec2-user/.ssh/config
debug1: Reading configuration data /etc/ssh/ssh_config
... many debug lines cut here ...
debug1: Next authentication method: publickey
debug1: Trying private key: /home/ec2-user/somekey.pem
debug1: key_parse_private2: missing begin marker
debug1: read PEM private key done: type RSA
debug1: Authentications that can continue: publickey
debug1: No more authentication methods to try.
The third line above is where the problem actual was identified; however, I looked for at the debug message four lines from the bottom (above) and was misled. There isn't a problem with the key but I tested it and compared other configurations.
My user ssh config file reset the host via an unintended global setting as shown below. The first Host line should not have been a comment.
$ cat config
StrictHostKeyChecking=no
#Host myAlias
user ec2-user
Hostname bitbucket.org
# IdentityFile ~/.ssh/somekey
# IdentitiesOnly yes
Host my2ndAlias
user myOtherUser
Hostname bitbucket.org
IdentityFile ~/.ssh/my2ndKey
IdentitiesOnly yes
I hope someone else finds this helpful.
you must check these few things:
I had the same problem, and it solved after I changed username to ubuntu. In AWS documentation was mentioned to user ec2-user but somehow does not work for me.
Try using
sudo ssh -i mykey.pem ubuntu@<ec2_ip_public_dns>
OR
sudo ssh -i mykey.pem ec2-user@<ec2_ip_public_dns>
Another possible cause of this error:
When user's home directory is group writeable, the user cannot login.
(Reproduced on Ubuntu instance.)
This is how I solved the problem
ssh -i <key> ec2-user@<ec2 ip>
Source: Stackoverflow.com