从 Mac OS 使用 scp 将文件复制到 Amazon EC2 实例时出现权限被拒绝错误

6
我已经阅读了几篇有关这个问题的StackOverflow文章并在其他来源上进行了谷歌搜索,但没有任何收获。我已经检查了AWS文档,但无法解决问题。我已经花费了几个小时来处理这个问题,但卡住了。
我发现有些文章提到了类似的问题,但问题似乎总是由用户的拼写错误或操作失误引起的。在我的情况下,我确信我已经确保我所做的一切都没有拼写错误或错误。
我使用Mac OS X Yosemite连接到我的AWS EC2实例。我无法将文件从本地机器传输到我的实例中,但我过去曾经能够这样做,我相信是在升级到Yosemite之前(尽管我不确定是在升级之前还是之后)。无论如何,我现在遇到了权限被拒绝的错误。
我能够通过ssh正常登录到我的实例。我是这样做的:
ssh -i mykey.pem ec2-user@myEC2host.myzone.compute.amazonaws.com

通过 ssh 一切正常。但当我尝试这样使用 scp:

scp —i mykey.pem ~/Sites/test.html ec2-user@myEC2host.myzone.compute.amazonaws.com:/var/www/html/

我遇到了以下错误:
"Permission denied (publickey). lost connection"
我已经尝试了数小时并确认了以下内容:
1. 我可以正常使用ssh命令。 2. 我的.pem文件权限为400,且不对外公开。 3. 我在/var/www和/var/www/html文件夹中拥有正确的权限和所有权。(我重新按照Amazon的设置教程进行操作,确保我使用了他们描述的所有权限设置。我的用户是具有访问这些文件夹权限的组的一部分。)
当我运行scp -v命令时,我可以看到scp似乎甚至没有尝试使用指定的身份验证文件。我得到以下输出:
(请注意,执行的程序从scp命令中删除了-i)
Executing: program /usr/bin/ssh host myEC2host.myzone.compute.amazonaws.com, user ec2-user, command scp -v -d -t /var/www/html/
OpenSSH_6.2p2, OSSLShim 0.9.8r 8 Dec 2011
debug1: Reading configuration data /etc/ssh_config
debug1: /etc/ssh_config line 20: Applying options for *
debug1: Connecting to myEC2host.myzone.compute.amazonaws.com [public AWS ip] port 22.
debug1: Connection established.
debug1: identity file /Users/myusername/.ssh/id_rsa type -1
debug1: identity file /Users/myusername/.ssh/id_rsa-cert type -1
debug1: identity file /Users/myusername/.ssh/id_dsa type -1
debug1: identity file /Users/myusername/.ssh/id_dsa-cert type -1
debug1: Enabling compatibility mode for protocol 2.0
debug1: Local version string SSH-2.0-OpenSSH_6.2
debug1: Remote protocol version 2.0, remote software version OpenSSH_6.2
debug1: match: OpenSSH_6.2 pat OpenSSH*
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: 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  {some hex output}
debug1: Host 'myEC2host.myzone.compute.amazonaws.com' is known and matches the RSA host key.
debug1: Found key in /Users/myusername/.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/myusername/.ssh/id_rsa
debug1: Trying private key: /Users/myusername/.ssh/id_dsa
debug1: No more authentication methods to try.
Permission denied (publickey).
lost connection

然而,当我运行ssh -v时,我会得到以下输出,它立即使用了正确的身份验证文件:

ssh -v -i mykey ec2-user@myEC2host.myzone.compute.amazonaws.com
OpenSSH_6.2p2, OSSLShim 0.9.8r 8 Dec 2011
debug1: Reading configuration data /etc/ssh_config
debug1: /etc/ssh_config line 20: Applying options for *
debug1: Connecting to myEC2host.myzone.compute.amazonaws.com [54.69.211.59] port 22.
debug1: Connection established.
debug1: identity file mykey.pem type -1
debug1: identity file mykey.pem-cert type -1
debug1: Enabling compatibility mode for protocol 2.0
debug1: Local version string SSH-2.0-OpenSSH_6.2
debug1: Remote protocol version 2.0, remote software version OpenSSH_6.2
debug1: match: OpenSSH_6.2 pat OpenSSH*
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: 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 {some hex output}
debug1: Host 'myEC2host.myzone.compute.amazonaws.com' is known and matches the RSA host key.
debug1: Found key in /Users/myusername/.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: mike.pem
debug1: read PEM private key done: type RSA
debug1: Authentication succeeded (publickey).
Authenticated to myEC2host.myzone.compute.amazonaws.com ([myEC2IP]:22).
debug1: channel 0: new [client-session]
debug1: Requesting no-more-sessions@openssh.com
debug1: Entering interactive session.
debug1: Sending environment.
debug1: Sending env LANG = en_CA.UTF-8

我不确定还有什么其他尝试方法,也不知道如何解决这个问题。我希望有人能够确认这个问题或提供解决方案,特别是在同样的环境下。
谢谢!

你的.pem文件路径不是绝对路径。在同一个文件夹中运行ssh和scp吗? - at0mzk
我尝试按照你所说的方式运行scp命令,并且它正确地将“-i”选项传递给ssh。你能否重试产生第一个调试跟踪的scp命令,并确保指定了-i选项?并在调试跟踪中包含实际的scp命令? - Kenster
嗯,仔细看了一下,在 scp —i mykey.pem... 中的“—i”不是ASCII短横线。它是一些Unicode破折号字符。当我将该行复制/粘贴到终端窗口时,scp并不将其解释为-i选项。执行命令时,请确保键入ASCII短横线。 - Kenster
好的,就是这样了。谢谢Kenster。我一定是把那段代码复制到/从某个地方粘贴过来时搞错了破折号。我以前从未遇到过这种情况。我不确定自己是否应该感到愚蠢!:-S - Steve K
2个回答

4
scp —i mykey.pem ~/Sites/test.html ec2-user@myEC2host.myzone.compute.amazonaws.com:/var/www/html/
Executing: program /usr/bin/ssh host myEC2host.myzone.compute.amazonaws.com, user ec2-user, command scp -v -d -t /var/www/html/

您的scp —i...命令中的短横线是Unicode EM破折号,而不是ASCII短横线。Scp没有将其解释为命令行选项。实际上,它将“—i”,“mykey.pem”和“〜/Sites/test.html”解释为要复制的三个文件。您可以通过远程scp实例的命令行中添加“-d”标志来确定此操作,该标志告诉远程scp实例目标必须是目录。当scp复制多个文件时,它会将该标志添加到远程命令中,但不会为复制单个文件添加该标志。也许您从文字处理文档中复制了scp命令?Microsoft Word以更改破折号和引号为排版版本而闻名。这是需要注意的事情。

1

Kenster正确指出,我代码中-i中的破折号实际上在复制到终端之前被转换为EMdash。 :-s

重新输入整个命令纠正了这个问题。

感谢您发现我的疏忽。


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