Trying to SSH into an Amazon Ec2 instance - permission error

后端 未结 29 3276
一生所求
一生所求 2020-11-27 08:51

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

相关标签:
29条回答
  • 2020-11-27 09:30

    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

    0 讨论(0)
  • 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>
    
    0 讨论(0)
  • 2020-11-27 09:31

    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>>
    
    0 讨论(0)
  • 2020-11-27 09:31

    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.

    0 讨论(0)
  • 2020-11-27 09:33

    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}

    0 讨论(0)
  • 2020-11-27 09:33

    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.

    0 讨论(0)
提交回复
热议问题