在Mac上通过终端连接EC2实例

9

我完全不懂编程,正在Coursera上学习初创企业课程:https://class.coursera.org/startup-001/lecture/87

我在尝试连接我在AWS上创建的EC2 Ubuntu实例时遇到了“Permission Denied (publickey)”错误。

  1. 设置AWS实例
  2. 将安全性设置为默认值,并添加端口22,然后启动
  3. 课程告诉我接下来输入:

    chmod 400 trevor-learning.pem ssh -i trevor-learning.pem \ ec2-user@ec2-XX-XXX-XX-XX.us-west-2.compute.amazonaws.com

然后我就收到了这个错误:Permission denied (publickey)。

我查看了其他问题并尝试将ubuntu@切换为ec2-user和其他一些方法,但仍无法连接。

我还尝试了以下内容: trevor-larsons-macbook-pro:downloads trev$ chmod 600 trevor-learning.pem trevor-larsons-macbook-pro:downloads trev$ ssh -v -i trevor-learning ubuntu@ec2-XX-XXX-XX- XX.us-west-2.compute.amazonaws.com

它给了我:

OpenSSH_5.9p1, OpenSSL 0.9.8x 10 May 2012
Warning: Identity file trevor-learning not accessible: No such file or directory.
debug1: Reading configuration data /etc/ssh_config
debug1: /etc/ssh_config line 20: Applying options for *
debug1: Connecting to ec2-XX-XXX-XX-XX.us-west-2.compute.amazonaws.com [XX.XXX.XX.XX] port     22.
debug1: Connection established.
debug1: identity file /Users/trev/.ssh/id_rsa type -1
debug1: identity file /Users/trev/.ssh/id_rsa-cert type -1
debug1: identity file /Users/trev/.ssh/id_dsa type -1
debug1: identity file /Users/trev/.ssh/id_dsa-cert type -1
debug1: Remote protocol version 2.0, remote software version OpenSSH_5.9p1 Debian-5ubuntu1
debug1: match: OpenSSH_5.9p1 Debian-5ubuntu1 pat OpenSSH*
debug1: Enabling compatibility mode for protocol 2.0
debug1: Local version string SSH-2.0-OpenSSH_5.9
debug1: SSH2_MSG_KEXINIT sent
debug1: SSH2_MSG_KEXINIT received
debug1: kex: server->client aes128-ctr hmac-md5 none
debug1: kex: client->server aes128-ctr hmac-md5 none
debug1: SSH2_MSG_KEX_DH_GEX_REQUEST(1024<1024<8192) sent
debug1: expecting SSH2_MSG_KEX_DH_GEX_GROUP
debug1: SSH2_MSG_KEX_DH_GEX_INIT sent
debug1: expecting SSH2_MSG_KEX_DH_GEX_REPLY
debug1: Server host key: RSA xx:xx:xx:xx:xx:xx:xx:xx:xx:xx:xx:xx:xx:xx:xx:xx
debug1: Host 'ec2-XX-XXX-XX-XX.us-west-2.compute.amazonaws.com' is known and matches the RSA host key.
debug1: Found key in /Users/trev/.ssh/known_hosts:2
debug1: ssh_rsa_verify: signature correct
debug1: SSH2_MSG_NEWKEYS sent
debug1: expecting SSH2_MSG_NEWKEYS
debug1: SSH2_MSG_NEWKEYS received
debug1: Roaming not allowed by server
debug1: SSH2_MSG_SERVICE_REQUEST sent
debug1: SSH2_MSG_SERVICE_ACCEPT received
debug1: Authentications that can continue: publickey
debug1: Next authentication method: publickey
debug1: Trying private key: /Users/trev/.ssh/id_rsa
debug1: Trying private key: /Users/trev/.ssh/id_dsa
debug1: No more authentication methods to try.
Permission denied (publickey).

我完全不知道该怎么做,请帮帮我!

另外,不确定这是否与此有关,当我下载实例证书(显示在Chrome浏览器左下角时),它会打开我的钥匙串访问并显示“发生错误。无法导入项目。无法检索此项目的内容。”


你需要注意的错误是:警告:身份文件trevor-learning不可访问:没有这个文件或目录。 - datasage
@datasage 我真的不太明白我在做什么。文件在我的下载文件夹中。我进入终端中的目录,然后从那里进行其余操作。最终会询问:主机 'ec2-XX-XXX-XX-XXX.us-west-2.compute.amazonaws.com (XX.XXX.XX.XXX)' 的真实性无法确定。 RSA 密钥指纹为 2f:71:6a:82:4d:ec:29:a2:eb:12:96:f3:09:17:71:e1。 您确定要继续连接吗(是/否)?是 警告:已将 'ec2-XX-XXX-XX-XXX.us-west-2.compute.amazonaws.com,XX.XXX.XX.XX'(RSA)永久添加到已知主机列表中。 权限被拒绝(公钥)。 - funitect
1个回答

11

请在您的终端中尝试以下步骤:

ssh-keygen -R 12.345.678.999 // This Reset your publickey

然后尝试使用您的密钥对名称和EC2的公共DNS进行访问:


ssh -i KeyPairName.pem ubuntu@ec2-xx-xxx-xxx-xxx.eu-west-1.compute.amazonaws.com

太好了,非常感谢!老实说,删除KeyPairName和ubuntu@之间的“\”是解决问题的关键,不知道为什么,但还是谢谢! - funitect
4
在记录因宽度限制而需要换行的长命令时,“\”字符常用于表示连续行。它不是用来键入的,但一般如果你将其复制粘贴到终端中,终端会忽略它并正确执行命令。请参见此维基百科文章以获得更好的解释。 - ljs.dev

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接