如何通过ssh连接到AWS实例?

我有:

Signed up at AWS; Created a public key and a certificate at AWS website and saved them to disk; Went to my console and created environment variables: $ export JAVA_HOME=/usr/lib/jvm/java-6-openjdk/ $ export EC2_CERT=/home/default/aws/cert-EBAINCRNWHDSCWWIHSOKON2YWGJZ5LSQ.pem $ export EC2_PRIVATE_KEY=/home/default/aws/pk-EBAINCRNWHDSCWWIHSOKON2YWGJZ5LSQ.pem Told AWS API to use this keypair and saved the keypair to file: $ ec2-add-keypair ec2-keypair > ec2-keypair.pem Started an AWS Ubuntu 9 instance using this keypair: $ ec2-run-instances ami-ed46a784 -k ec2-keypair Attempted to establish a ssh connection to the instance: $ ssh -v -i ec2-keypair.pem ubuntu@ec2-174-129-185-190.compute-1.amazonaws.com OpenSSH_5.1p1 Debian-5ubuntu1, OpenSSL 0.9.8g 19 Oct 2007 debug1: Reading configuration data /etc/ssh/ssh_config debug1: Applying options for * debug1: Connecting to ec2-174-129-185-190.compute-1.amazonaws.com [174.129.185.190] port 22. debug1: Connection established. debug1: identity file ec2-keypair.pem type -1 debug1: Remote protocol version 2.0, remote software version OpenSSH_5.1p1 Debian-5ubuntu1 debug1: match: OpenSSH_5.1p1 Debian-5ubuntu1 pat OpenSSH* debug1: Enabling compatibility mode for protocol 2.0 debug1: Local version string SSH-2.0-OpenSSH_5.1p1 Debian-5ubuntu1 debug1: SSH2_MSG_KEXINIT sent debug1: SSH2_MSG_KEXINIT received debug1: kex: server->client aes128-cbc hmac-md5 none debug1: kex: client->server aes128-cbc 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: Host 'ec2-174-129-185-190.compute-1.amazonaws.com' is known and matches the RSA host key. debug1: Found key in /home/default/.ssh/known_hosts:11 debug1: ssh_rsa_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: Trying private key: ec2-keypair.pem debug1: read PEM private key done: type RSA debug1: Authentications that can continue: publickey debug1: No more authentication methods to try. Permission denied (publickey). What could be the problem and how to make it work?


当前回答

只是给这个列表加了一条。今天早上,我遇到了一个刚添加到AWS EC2实例的新用户的麻烦。直截了当地说,问题出在selinux(它处于强制模式)上,而且我的用户主目录位于新的EBS附加卷上。我猜selinux不喜欢另一卷。我花了一段时间才弄清楚,因为我查看了所有其他常见的ssh问题(/etc/ssh/sshd_config是好的,当然不允许密码,权限是正确的,等等)。

这是固定的吗?

目前(直到我理解如何允许用户ssh到不同的卷,或者以某种方式使该卷成为真正的主目录点):

sudo perl -pi -e 's/^SELINUX=enforcing/SELINUX=permissive/' /etc/selinux/config
sudo setenforce 0

就是这样。现在我的新用户可以使用他自己的id_rsa密钥登录。

其他回答

对于我的ubuntu图像,它实际上是ubuntu用户,而不是ec2-user;)

现在是:

ssh -v -i ec2-keypair.pem ec2-user@[yourdnsaddress]

只是给这个列表加了一条。今天早上,我遇到了一个刚添加到AWS EC2实例的新用户的麻烦。直截了当地说,问题出在selinux(它处于强制模式)上,而且我的用户主目录位于新的EBS附加卷上。我猜selinux不喜欢另一卷。我花了一段时间才弄清楚,因为我查看了所有其他常见的ssh问题(/etc/ssh/sshd_config是好的,当然不允许密码,权限是正确的,等等)。

这是固定的吗?

目前(直到我理解如何允许用户ssh到不同的卷,或者以某种方式使该卷成为真正的主目录点):

sudo perl -pi -e 's/^SELINUX=enforcing/SELINUX=permissive/' /etc/selinux/config
sudo setenforce 0

就是这样。现在我的新用户可以使用他自己的id_rsa密钥登录。

在我的情况下(Mac OS X),问题是文件的中断类型。试试这个:

1.—使用TextWrangler打开。pem文件

2.-在应用程序底部,验证中断类型是否为“Windows(CRLF)”。

如果pem文件权限太开放,它也会报错。Chmod文件到600来修复这个问题。