首页 > 解决方案 > 可以以 root 身份而不是 ubuntu 用户身份 SSH 到 EC2 ubuntu 服务器

问题描述

嗨,我有来自服务器 A、B 的 3 个 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 身份登录但 root 它正在工作。如果访问权限问题意味着服务器 A 的工作方式。

有人请帮助提前谢谢。

标签: ubuntujenkinsamazon-ec2sshssh-keys

解决方案


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

 vi ~/.ssh/authorized_keys

我确定您没有在此文件中添加服务器 B 的公钥。您在 root 用户的授权密钥中添加了它。如果它为空或仅存在默认密钥,请添加服务器 b 公钥并重试。让我们知道它是否有效。


推荐阅读