Permission denied (publickey) when SSH Access to Amazon EC2 instance

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  ubuntu@ec2-54-227-242-179.compute-1.amazonaws.com

But have this error:

Permission denied (publickey)

Also, how can I connect with filezilla to upload/download files?


This error message means you failed to authenticate.

These are common reasons that can cause that:

  • Trying to connect with the wrong key. Are you sure this instance is using this keypair?
  • Trying to connect with the wrong username. ubuntu is the username for the ubuntu based AWS distribution, but on some others it's ec2-user (or admin on some Debians, according to Bogdan Kulbida's answer)(can also be root , fedora , see below)
  • Trying to connect the wrong host. Is that the right host you are trying to log in to?
  • Note that 1. will also happen if you have messed up the /home/<username>/.ssh/authorized_keys file on your EC2 instance.

    About 2. , the information about which username you should use is often lacking from the AMI Image description. But you can find some in AWS EC2 documentation, bullet point 4. : http://docs.aws.amazon.com/AWSEC2/latest/UserGuide/AccessingInstancesLinux.html

    Use the ssh command to connect to the instance. You'll specify the private key (.pem) file and user_name@public_dns_name. For Amazon Linux, the user name is ec2-user. For RHEL5, the user name is either root or ec2-user . For Ubuntu, the user name is ubuntu . For Fedora, the user name is either fedora or ec2-user . For SUSE Linux, the user name is root . Otherwise, if ec2-user and root don't work, check with your AMI provider.

    Finally , be aware that there are many other reasons why authentication would fail. SSH is usually pretty explicit about what went wrong if you care to add the -v option to your SSH command and read the output, as explained in many other answers to this question.


    In this case the problem arises from lost Key Pair. About this:

  • There's no way to change Key Pair on an instance . You have to create a new instance that uses a new Key Pair.
  • You can work around the problem if your instance is used by an application on Elastic Beanstalk .
  • You can follow these steps:

  • Access to AWS Management Console
  • Open Elastic Beanstalk Tab
  • Select your application from All Applications Tab
  • From left side menù select Configuration
  • Click on the Instances Gear
  • In Server Form check the EC2 Key Pair input and select your new Key Pair. You may have to refresh the list in order to see a new Key Pair you're just created.
  • Save
  • Elastic Beanstalk will create for you new instances associated with the new key pair.

  • 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.

  • Access to AWS Management Console
  • Open EC2 Tab
  • From Instances list select the instance you are interested in
  • In the Description Tab chek the name of the Security Group your instance is using.
  • Again in Description Tab click on View rules and check if your Security Group has a rule for inbound ssh traffic on port 22
  • If not, in Network & Security menù select Security Group
  • Select the Security Group used by your instance and the click Inbound Tab
  • On the left of Inbound Tab you can compose a rule for SSH inbound traffic:
  • Create a new rule : SSH
  • Source : IP address or subnetwork from which you want access to instance
  • Note : If you want grant unlimited access to your instance you can specify 0.0.0.0/0, although Amazon not recommend this practice
  • Click Add Rule and then Apply Your Changes
  • Check if you're now able to connect to your instance via SSH.
  • Hope this can help someone as helped me.


    这就是我解决问题的方法

    ssh -i <key> ec2-user@<ec2 ip>
    
    链接地址: http://www.djcxy.com/p/32440.html

    上一篇: EC2实例没有公共DNS

    下一篇: 当SSH访问Amazon EC2实例时,权限被拒绝(publickey)