通过 SSH 进入 AWS EC2 实例会导致权限被拒绝(公钥)

SSH into AWS EC2 instance gives Permission denied (publickey)

我从我的 AWS 控制台创建了一个新的 EC2 实例并尝试使用我的密钥文件进行 ssh

ssh -i "myKeypair.pem" ec2-user@instancepublicDNS.amazonaws.com

我能够登录到服务器并执行了这些操作:

已安装 httpd。 已安装 php。 安装 mysql.

创建了一个新组 "web" 并将用户 apache 和 ec2-user 添加到此 group.Changed 我的默认主目录 /home/ec2-user/ ec2-user:web

按照 this answer 中的说明为我的实例设置 FTP。

然后我在主目录中安装了 OPencart。

使用 exit 命令从 SSH 注销后

但是现在,当我尝试使用同一个 pem 文件再次登录时,出现此错误:

prince@devilDevice:~$ ssh -v -i "mypemFile.pem" ec2-user@instancepublicDNS.amazonaws.com
OpenSSH_6.6.1, OpenSSL 1.0.1f 6 Jan 2014
debug1: Reading configuration data /etc/ssh/ssh_config
debug1: /etc/ssh/ssh_config line 19: Applying options for *
debug1: Connecting to instancepublicDNS.amazonaws.com [Public IP] port 22.
debug1: Connection established.
debug1: identity file mypemFile.pem type -1
debug1: identity file mypemFile.pem-cert type -1
debug1: Enabling compatibility mode for protocol 2.0
debug1: Local version string SSH-2.0-OpenSSH_6.6.1p1 Ubuntu-2ubuntu2.4
debug1: Remote protocol version 2.0, remote software version OpenSSH_6.6.1
debug1: match: OpenSSH_6.6.1 pat OpenSSH_6.6.1* compat 0x04000000
debug1: SSH2_MSG_KEXINIT sent
debug1: SSH2_MSG_KEXINIT received
debug1: kex: server->client aes128-ctr hmac-md5-etm@openssh.com none
debug1: kex: client->server aes128-ctr hmac-md5-etm@openssh.com none
debug1: sending SSH2_MSG_KEX_ECDH_INIT
debug1: expecting SSH2_MSG_KEX_ECDH_REPLY
debug1: Server host key: ECDSA {somefingerprinthere}
debug1: Host 'instancepublicDNS.amazonaws.com' is known and matches the ECDSA host key.
debug1: Found key in /home/prince/.ssh/known_hosts:18
debug1: ssh_ecdsa_verify: signature correct
debug1: SSH2_MSG_NEWKEYS sent
debug1: expecting SSH2_MSG_NEWKEYS
debug1: SSH2_MSG_NEWKEYS received
debug1: SSH2_MSG_SERVICE_REQUEST sent
debug1: SSH2_MSG_SERVICE_ACCEPT received
debug1: Authentications that can continue: publickey
debug1: Next authentication method: publickey
debug1: Offering RSA public key: {my_email_address}
debug1: Authentications that can continue: publickey
debug1: Trying private key: mypemFile.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.
Permission denied (publickey).

请帮忙。

这就是罪魁祸首。

将我的默认主目录 /home/ec2-user/ 所有权更改为 ec2-user:web

sshd 试图从 /home/ec2-user/.ssh/authorized_keys 文件中获取 public 密钥,但不能,因为您更改了所有者。所以你被拒绝访问。