EC2 ssh权限被拒绝(publickey,gssapi-keyex,gssapi-with-mic)

时间:2015-11-30 05:12:47

标签: linux ssh amazon-ec2 permission-denied

当我想ssh到我的ec2主机时,我收到了此权限被拒绝的问题。我尝试了现有的解决方案chmod 600 "My.pem",但仍然无效。这是我的调试信息:

debug1: Reading configuration data /etc/ssh/ssh_config
debug1: /etc/ssh/ssh_config line 21: Applying options for *
debug1: Connecting to 54.223.47.74 [54.223.47.74] port 22.
debug1: Connection established.
debug1: key_load_public: No such file or directory
debug1: identity file My.pem type -1
debug1: key_load_public: No such file or directory
debug1: identity file My.pem-cert type -1
debug1: Enabling compatibility mode for protocol 2.0
debug1: Local version string SSH-2.0-OpenSSH_6.9
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: Authenticating to 54.223.47.74:22 as 'root'
debug1: SSH2_MSG_KEXINIT sent
debug1: SSH2_MSG_KEXINIT received
debug1: kex: server->client chacha20-poly1305@openssh.com <implicit> none
debug1: kex: client->server chacha20-poly1305@openssh.com <implicit> none
debug1: expecting SSH2_MSG_KEX_ECDH_REPLY
debug1: Server host key: ecdsa-sha2-nistp256 SHA256:tfjxcE5kePSv1cJK7SWBp/56kgm2DQkyPLSLZ4d73Io
debug1: Host '54.223.47.74' is known and matches the ECDSA host key.
debug1: Found key in /Users/tan/.ssh/known_hosts:24
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,gssapi-keyex,gssapi-with-mic
debug1: Next authentication method: publickey
debug1: Trying private key: My.pem
debug1: Authentications that can continue: publickey,gssapi-keyex,gssapi-with-mic
debug1: No more authentication methods to try.
Permission denied (publickey,gssapi-keyex,gssapi-with-mic).

19 个答案:

答案 0 :(得分:13)

我使用命令

在我的centos机器中解决了这个问题
ssh -i <Your.pem> ec2-user@<YourServerIP>

关于 userName ,在我的情况下是ec2-user

引自:AMAZONTroubleshooting

答案 1 :(得分:7)

通过与用户centos而不是ec2-user联系来解决。

答案 2 :(得分:3)

您可以在此处找到ect2实例的默认用户名: https://alestic.com/2014/01/ec2-ssh-username/

但是如果您想查找实例的用户名: 单击“连接”按钮以查看默认用户名。

找到用户名后,运行此命令,以确保您的密钥不公开。

chmod 400

然后使用其公共DNS或IP连接到您的实例:

ssh -i ec2-user @

答案 3 :(得分:1)

将用户添加到/etc/sshd_special_user

答案 4 :(得分:1)

我注意到每个Linux实例都使用默认的Linux系统用户帐户启动。根据您的实例,这可能与ec2-user不同。您使用以下命令登录,其中DefaultUserName代表下面引号中的用户名。

ssh -i <Your.pem> <DefaultUserName>@<YourPublicServerIP>

每个Linux实例使用默认的Linux系统用户帐户启动。默认用户名由启动实例时指定的AMI确定。

  • 对于 Amazon Linux 2 或Amazon Linux AMI,用户名为 ec2-user
  • 对于 CentOS AMI,用户名为 centos
  • 对于 Debian AMI,用户名为 admin
  • 对于 Fedora AMI,用户名为 ec2-user fedora
  • 对于 RHEL AMI,用户名为 ec2-user root
  • 对于 SUSE AMI,用户名是 ec2-user root
  • 对于 Ubuntu AMI,用户名为 ubuntu
  • 否则,如果ec2-user和root不起作用,请与AMI提供程序联系。

答案 5 :(得分:1)

确保您在.pem文件所在的目录中。

使用命令,chmod 0400 example.pem

然后使用命令ssh -i example.pem ec2-user @ YOUR-IP

答案 6 :(得分:1)

检查您是否在您的密钥所在的同一目录中。 我有同样的问题,并发现这是我试图连接的错误的目录形式

答案 7 :(得分:0)

在Mac上:

cd .ssh
sudo nano know_hosts

并删除有问题的主机的信息。

答案 8 :(得分:0)

最近我遇到了这个问题, 偶然地,我使用chmod -R g+rw .更改了“居家”权限 它更改了.ssh文件夹权限。

  1. 如果没有解决办法, 在Amazon-EC2上创建临时实例
  2. 卸下服务器存储(在关闭源计算机之前)
  3. 将其挂载为临时实例中的辅助存储
  4. 执行以下权限更改

    [ec2-user〜] $ chmod 600 mount_point / home / ec2-user / .ssh / authorized_keys

    [ec2-user〜] $ chmod 700 mount_point / home / ec2-user / .ssh

    [ec2-user〜] $ chmod 700 mount_point / home / ec2-user

  5. 从临时实例卸载源磁盘

  6. 将其附加回源计算机

  7. 现在使用相同的命令登录,

    ssh -i FileName.pem用户名@MachineIP

有关更多详细信息,请参见此AWS Trouble Shooting Docs

答案 9 :(得分:0)

检查selinux是否阻止对文件的访问。

尝试以下操作:

import cv2
import numpy as np
import os

def frame_capture(file):
    cap = cv2.VideoCapture(file)
    try:
        if not os.path.exists('data'):
            os.makedirs('data')
    except OSError:
        print('Error: Creating directory of data')

    currentFrame = 0

    while(True):
      # Capture frame by frame
        ret, frame = cap.read()
'''
(((how do I change this block here to get it for every 3 seconds?)))
'''
        if currentFrame == 5:
            name = './data/frame' + str(currentFrame) + '.jpg'
            print ('Creating...' + name)
            cv2.imwrite(name, frame)

      # To stop duplicate images
        currentFrame += 1

    cap.release()
    cv2.destroyAllWindows()
    print ("Done!")

for file in os.listdir("/users/x/Desktop/y/videos"):

    if file.endswith(".mp4"):
        path=os.path.join("/users/x/Desktop/y/videos", file))
        frame_capture(path)

答案 10 :(得分:0)

cd .ssh
rm authorized_keys

文件或编辑并删除您要访问的计算机的已保存密钥。

使用chmod 400 keyname更改密钥文件的权限(确保keyname与您在Amazon上拥有的完全匹配)。

使用ec2-user@IPaddress -i keypair.pem

重试

答案 11 :(得分:0)

确保EG命令指定ec2用户:

PHP common macros
ssh

答案 12 :(得分:0)

终端中的解决错误

权限被拒绝(publickey,gssapi-keyex,gssapi-with-mic,密码)。

SELECT cid, COUNT(*) AS num_zines
FROM Subscriptions
GROUP BY cid

xx - ip host

http://pastebin.com/YpqGSJ2E

答案 13 :(得分:0)

除了此处提到的用户名问题外,很可能是一个问题。

对我来说,这是公钥不匹配。这就是我解决它的方式。我在装有ssh客户端的Mac上。

您可以通过运行以下命令从.pem文件中获取本地公钥:

ssh-keygen -y -f /path_to_key_pair/my-key-pair.pem

在您的实例上,导航到您的authorized_keys文件,该文件通常在以下位置找到:

/home/username/.ssh/authorized_keys

将公共密钥添加到此文件。保存并完成。那应该做。

关于我为什么遇到这个问题的一点背景。我必须创建一个新的.pem文件,因为启动实例时丢失了我下载的文件。出于安全原因,无法再次下载此文件。当我创建一个新的.pem文件时,它使用它创建了一个新的公共密钥。该公钥需要在实例上手动更新,因为authorized_keys文件仍指向旧的公钥。

还有更正式的9个步骤来对此进行排序。见这里。
https://docs.aws.amazon.com/AWSEC2/latest/UserGuide/replacing-lost-key-pair.html

答案 14 :(得分:0)

我遇到了同样的问题并通过 -

解决
  1. 在Window机器中,将key保存到Pageant中 见https://aws.amazon.com/es/blogs/security/securely-connect-to-linux-instances-running-in-a-private-amazon-vpc/

  2. 然后在 Putty 中选择 SSH->Auth-> 选中“Allow Agent Forwarding”并将 ppk 文件放入“Private Key file for Authentication”。

答案 15 :(得分:0)

我遇到了同样的问题,就我而言,问题是应该使用管理员用户创建文件“My.pem”

所以,解决方案是,首先使用 sudo 创建文件“My.pem”并将权限更改为 400

$ sudo su
$ sudo vim My.pem
  #paste the content

$ sudo vim chmod 400 My.pem

$ ssh -i My.pem user@host
  # Login ok

答案 16 :(得分:0)

我遇到了同样的问题,但就我而言,这是因为我创建了一个新密钥以从不同的设备进行连接。密钥对仅在您创建新实例时添加,如果您想在创建实例后创建新密钥,则必须手动添加。

您可以在此处遵循本指南https://docs.aws.amazon.com/AWSEC2/latest/UserGuide/ec2-key-pairs.html#identify-key-pair-specified-at-launch

  1. ssh 使用您的旧密钥对进入 AWS 实例

  2. cd 进入 ~/.ssh 文件夹并

  3. 使用 authorized_keys 或任何您喜欢的方式打开 nano 文件

  4. 然后转到新的 .pem 密钥对并使用

    检索公钥

    ssh-keygen -y -f /path_to_key_pair/my-key-pair.pem

  5. 复制返回的公钥并将其粘贴到您的其他密钥下方的authorized_keys文件中,保存并退出。

  6. chmod 400 my-key-pair.pem 如果您还没有

  7. 那么您应该可以使用新的密钥对进行连接了。

答案 17 :(得分:0)

如果您在 mac 上使用终端 (.zsh) 通过 ssh 连接 ec2,那么您必须在所有命令中将名称从 .pem 更改为 .cer,因为在 mac 上您下载的 key_pair 文件的扩展名为 .cer, 否则,您可以按照 aws 上给出的命令进行操作。

答案 18 :(得分:-1)

您必须在ec2主机上运行以下命令以ssh

ssh -i <user.pem> ec2-user@<public ip>

如果有

WARNING: UNPROTECTED PRIVATE KEY FILE!          
Permissions 0644 for 'user.pem' are too open.
It is required that your private key files are NOT accessible by others.
This private key will be ignored.

然后运行chmod 0400 <user.pem>

在运行以上命令后运行 ssh -i <user.pem> ec2-user@<public ip>