可以以root身份而不是以ubuntu用户身份通过​​SSH进入EC2 ubuntu服务器

时间:2018-07-03 18:20:38

标签: ubuntu jenkins amazon-ec2 ssh ssh-keys

嗨,我有3个来自服务器A,B的ubuntu EC2实例,我需要通过SSH进入服务器C。我已经在服务器A,B中完成了以下步骤。

1) Created a ssh key for the username - jenkins 

从服务器A复制了比服务器B的公钥,并将其添加到服务器c的

2) vi ~/.ssh/authorized_keys 

而不是如下更改〜/ .ssh和authorized_key文件的文件夹访问权限

3) chmod 600 ~/.ssh/
   chmod 700 ~/.ssh/authorized_keys

现在从服务器A运行ssh命令,如下所示,它以ubuntu用户身份登录到服务器C。

4) ssh -i privatekey ubuntu@12.12.12.12

当我从服务器B执行相同的操作时,出现以下错误

5) ssh -i privatekey ubuntu@12.12.12.12

错误

 Warning: Identity file -i not accessible: No such file or directory.
 ssh: Could not resolve hostname /var/lib/jenkins/secrets/mykey: Name or service not known
 jenkins@ip-11-11-11-99:/home/ubuntu# ssh -v -i /var/lib/jenkins/secrets/mykey ubuntu@12.12.12.12
 OpenSSH_7.2p2 Ubuntu-4ubuntu2.4, OpenSSL 1.0.2g  1 Mar 2016
 debug1: Reading configuration data /etc/ssh/ssh_config
 debug1: /etc/ssh/ssh_config line 19: Applying options for *
 debug1: Connecting to 12.12.12.12 [12.12.12.12] port 22.
 debug1: Connection established.
 debug1: permanently_set_uid: 0/0
 debug1: identity file /var/lib/jenkins/secrets/mykey type 1
 debug1: key_load_public: No such file or directory
 debug1: identity file /var/lib/jenkins/secrets/mykey-cert type -1
 debug1: Enabling compatibility mode for protocol 2.0
 debug1: Local version string SSH-2.0-OpenSSH_7.2p2 Ubuntu-4ubuntu2.4
 debug1: Remote protocol version 2.0, remote software version OpenSSH_7.2p2 Ubuntu-4ubuntu2.4
 debug1: match: OpenSSH_7.2p2 Ubuntu-4ubuntu2.4 pat OpenSSH* compat 0x04000000
 debug1: Authenticating to 12.12.12.12:22 as 'ubuntu'
 debug1: SSH2_MSG_KEXINIT sent
 debug1: SSH2_MSG_KEXINIT received
 debug1: kex: algorithm: curve25519-sha256@libssh.org
 debug1: kex: host key algorithm: ecdsa-sha2-nistp256
 debug1: kex: server->client cipher: chacha20-poly1305@openssh.com MAC: <implicit> compression: none
 debug1: kex: client->server cipher: chacha20-poly1305@openssh.com MAC: <implicit> compression: none
 debug1: expecting SSH2_MSG_KEX_ECDH_REPLY
 debug1: Server host key: ecdsa-sha2-nistp256 SHA256:28Mc4WE3B22oZ2ClomLspX5gQtwmzdNg82XEHgpI+vE
 debug1: Host '12.12.12.12' is known and matches the ECDSA host key.
 debug1: Found key in /root/.ssh/known_hosts:3
 debug1: rekey after 134217728 blocks
 debug1: SSH2_MSG_NEWKEYS sent
 debug1: expecting SSH2_MSG_NEWKEYS
 debug1: rekey after 134217728 blocks
 debug1: SSH2_MSG_NEWKEYS received
 debug1: SSH2_MSG_EXT_INFO received
 debug1: kex_input_ext_info: server-sig-algs=<rsa-sha2-256,rsa-sha2-512>
 debug1: SSH2_MSG_SERVICE_ACCEPT received
 debug1: Authentications that can continue: publickey
 debug1: Next authentication method: publickey
 debug1: Offering RSA public key: /var/lib/jenkins/secrets/mykey
 debug1: Authentications that can continue: publickey
 debug1: No more authentication methods to try.
 Permission denied (publickey).

但是当我从服务器B运行SSH来以root身份登录时,它以root身份登录

6) ssh -i privatekey root@12.12.12.12

我对服务器B中缺少的内容感到困惑。为什么它忽略以Ubuntu身份登录但却可以正常工作。如果访问权限问题意味着服务器A的工作方式。

请有人先帮助谢谢。

2 个答案:

答案 0 :(得分:2)

嗨,我怀疑在服务器C中,当您添加服务器B的公钥时,您可能仅在根用户的authorized_keys文件中添加了。确认。 ssh进入服务器C并以Ubuntu用户身份登录,然后使用以下命令运行以检查authorized_keys文件。

 vi ~/.ssh/authorized_keys

我确定您没有在该文件中添加服务器B的公钥。您已将其添加到root用户的authorized_keys中。如果为空或仅存在默认密钥,请添加服务器b公共密钥,然后重试。让我们知道它是否有效。

答案 1 :(得分:0)

感谢您发布问题。

问题:您可能已使用其他用户生成了密钥:

只需执行以下步骤,您就会很高兴:

0)删除〜/ .ssh 删除以前的设置。不用担心,我们会重新开始

1)将公钥从A和B复制到服务器C 以ubuntu用户身份登录服务器A $ ssh-keygen 接受默认文件位置 这将生成密钥 $ ssh-copy-id ubuntu @ 输入密码

2)在服务器B上执行相同的步骤 3)是的!你很好走

现在测试您的登录名。您不需要更改权限

尝试在〜/ .ssh / config中添加以下行

权限:-rw-r--r-

主机C

IdentityFile〜/ .ssh / id_rsa

用户your_user_name

主机名C