可以通过 SSH 以根用户身份进入 EC2 ubuntu 服务器,而不是作为 ubuntu 用户



嗨,我有 3 个来自服务器 A、B 的 ubuntu EC2 实例,我需要 ssh 到服务器 C。

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 以根身份登录时,它以根用户身份登录

6) ssh -i privatekey root@12.12.12.12

我对我在服务器 B 中错过的内容感到困惑。 为什么它忽略以 Ubuntu 身份登录但 root 它正在工作。 如果访问权限问题意味着它如何为服务器 A 工作。

有人请提前帮忙谢谢。

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

vi ~/.ssh/authorized_keys

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

感谢您发布问题。

问题:您可能使用不同的用户生成了密钥:

只需按照以下步骤操作,您就可以开始了:

0( 删除 ~/.ssh 删除以前的设置。别担心,我们会重新开始

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

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

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

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

权限: -rw-r--r--

主机 C

身份文件 ~/.ssh/id_rsa

用户your_user_name

主机名 C

最新更新