可以 SSH 进入 EC2 ubuntu 服务器作为 root 而不是 ubuntu 用户

can SSH into EC2 ubuntu server as a root not as a ubuntu user

您好,我有 3 个 ubuntu 来自服务器 A、B 的 EC2 实例,我需要通过 ssh 连接到服务器 C。我在服务器 A、B 中完成了以下步骤。

1) Created a ssh key for the username - jenkins 

从服务器 A 复制了 public 密钥而不是服务器 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).

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

6) ssh -i privatekey root@12.12.12.12

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

有人请帮忙提前致谢。

感谢您提出问题。

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

只需按照以下步骤操作即可:

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

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

2) 在服务器 B 上执行相同的步骤 3) 耶!!!你很高兴

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

尝试在 ~/.ssh/config

中添加以下行

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

主机 C

IdentityFile ~/.ssh/id_rsa

用户your_user_name

主机名 C

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

 vi ~/.ssh/authorized_keys

我确定您没有在这个 file.you 中添加服务器 B 的 public 密钥,而是在 root 用户的 authorized_keys 中添加了它。如果它是空白的或只有默认密钥存在,请添加服务器 b public 密钥并重试。让我们知道它是否有效。