SSH 权限被拒绝(公钥)

SSH 权限被拒绝(公钥)

我正在尝试创建自己的 SSH 密钥来从一个 VM A 连接到另一个 VM B(两者都是 Debian/bullseye64 系统)。

在我的系统上,两个虚拟机都使用 vagrant 配置为位于公共网络上,其中 A 位于 192.168.0.103,B 位于 192.168.0.104,我使用以下命令在虚拟机 A 上生成密钥并命名它ansible_id_rsa,同时将私钥和公钥复制到/etc/ssh虚拟机 A。

sudo ssh-keygen -t rsa

生成后,我ansible_id_rsa.pub从 VM A 复制内容并将其复制到~/.ssh/authorized_keysVM B 中。(请注意:B 在 authorized_keys 中已经有一个现有密钥可以连接到我的系统,我在下一行将其附加到它)。

.ssh这是两个文件夹的权限

drwx------ 2 vagrant vagrant 4096 Aug 18 02:53 .
drwxr-xr-x 3 vagrant vagrant 4096 Aug 18 03:09 ..
-rw------- 1 root    root    2590 Aug 18 01:50 ansible_id_rsa
-rw-r--r-- 1 root    root     563 Aug 18 01:50 ansible_id_rsa.pub
-rw------- 1 vagrant vagrant  409 Dec 30  2021 authorized_keys
-rw-r--r-- 1 vagrant vagrant  223 Aug 18 02:52 known_hosts

我还编辑了该/etc/ssh/sshd_config文件以使其具有:

PasswordAuthentication no

PermitRootLogin yes

PubKeyAuthentication yes

RSAAuthentication yes

然后我尝试通过在 VM A 上运行以下命令从 VM A 连接到 VM B ~/.ssh

ssh -i ansible_id_rsa -vvv [email protected]

连接失败并在终端上打印以下内容:

OpenSSH_8.4p1 Debian-5+deb11u1, OpenSSL 1.1.1n  15 Mar 2022
debug1: Reading configuration data /etc/ssh/ssh_config
debug1: /etc/ssh/ssh_config line 19: include /etc/ssh/ssh_config.d/*.conf matched no files
debug1: /etc/ssh/ssh_config line 21: Applying options for *
debug2: resolve_canonicalize: hostname 192.168.0.104 is address
debug3: expanded UserKnownHostsFile '~/.ssh/known_hosts' -> '/home/vagrant/.ssh/known_hosts'
debug3: expanded UserKnownHostsFile '~/.ssh/known_hosts2' -> '/home/vagrant/.ssh/known_hosts2'
debug2: ssh_connect_direct
debug1: Connecting to 192.168.0.104 [192.168.0.104] port 22.
debug1: Connection established.
debug1: identity file ansible_id_rsa type 0
debug1: identity file ansible_id_rsa-cert type -1
debug1: Local version string SSH-2.0-OpenSSH_8.4p1 Debian-5+deb11u1
debug1: Remote protocol version 2.0, remote software version OpenSSH_8.4p1 Debian-5
debug1: match: OpenSSH_8.4p1 Debian-5 pat OpenSSH* compat 0x04000000
debug2: fd 3 setting O_NONBLOCK
debug1: Authenticating to 192.168.0.104:22 as 'root'
debug3: hostkeys_foreach: reading file "/home/vagrant/.ssh/known_hosts"
debug3: record_hostkey: found key type ECDSA in file /home/vagrant/.ssh/known_hosts:2
debug3: load_hostkeys: loaded 1 keys from 192.168.0.104
debug3: order_hostkeyalgs: have matching best-preference key type [email protected], using HostkeyAlgorithms verbatim
debug3: send packet: type 20
debug1: SSH2_MSG_KEXINIT sent
debug3: receive packet: type 20
debug1: SSH2_MSG_KEXINIT received
debug2: local client KEXINIT proposal

debug2: compression ctos: none,[email protected],zlib
debug2: compression stoc: none,[email protected],zlib
debug2: languages ctos:
debug2: languages stoc:
debug2: first_kex_follows 0
debug2: reserved 0
debug2: peer server KEXINIT proposal

debug2: compression ctos: none,[email protected]
debug2: compression stoc: none,[email protected]
debug2: languages ctos:
debug2: languages stoc:
debug2: first_kex_follows 0
debug2: reserved 0
debug1: kex: algorithm: curve25519-sha256
debug1: kex: host key algorithm: ecdsa-sha2-nistp256
debug1: kex: server->client cipher: [email protected] MAC: <implicit> compression: none
debug1: kex: client->server cipher: [email protected] MAC: <implicit> compression: none
debug3: send packet: type 30
debug1: expecting SSH2_MSG_KEX_ECDH_REPLY
debug3: receive packet: type 31
debug1: Server host key: ecdsa-sha2-nistp256 SHA256:fqpDpY0+XthUBnzv4qVjeL7dErgkvw9ocdiBRR4un38
debug3: hostkeys_foreach: reading file "/home/vagrant/.ssh/known_hosts"
debug3: record_hostkey: found key type ECDSA in file /home/vagrant/.ssh/known_hosts:2
debug3: load_hostkeys: loaded 1 keys from 192.168.0.104
debug1: Host '192.168.0.104' is known and matches the ECDSA host key.
debug1: Found key in /home/vagrant/.ssh/known_hosts:2
debug3: send packet: type 21
debug2: set_newkeys: mode 1
debug1: rekey out after 134217728 blocks
debug1: SSH2_MSG_NEWKEYS sent
debug1: expecting SSH2_MSG_NEWKEYS
debug3: receive packet: type 21
debug1: SSH2_MSG_NEWKEYS received
debug2: set_newkeys: mode 0
debug1: rekey in after 134217728 blocks
debug1: Will attempt key: ansible_id_rsa RSA SHA256:i9s/9B3nDrmfC1fJsiTMs7FqeATpPKpvXFInHfwXMC4 explicit
debug2: pubkey_prepare: done
debug3: send packet: type 5
debug3: receive packet: type 7
debug1: SSH2_MSG_EXT_INFO received

debug3: receive packet: type 6
debug2: service_accept: ssh-userauth
debug1: SSH2_MSG_SERVICE_ACCEPT received
debug3: send packet: type 50
debug3: receive packet: type 51
debug1: Authentications that can continue: publickey
debug3: start over, passed a different list publickey
debug3: preferred gssapi-with-mic,publickey,keyboard-interactive,password
debug3: authmethod_lookup publickey
debug3: remaining preferred: keyboard-interactive,password
debug3: authmethod_is_enabled publickey
debug1: Next authentication method: publickey
debug1: Offering public key: ansible_id_rsa RSA SHA256:i9s/9B3nDrmfC1fJsiTMs7FqeATpPKpvXFInHfwXMC4 explicit
debug3: send packet: type 50
debug2: we sent a publickey packet, wait for reply
debug3: receive packet: type 51
debug1: Authentications that can continue: publickey
debug2: we did not send a packet, disable method
debug1: No more authentication methods to try.
[email protected]: Permission denied (publickey).

有人能告诉我我可能做错了什么吗?我尝试了在类似线程中找到的各种方法,但无法使它们正常工作。

附言:必须删除 -vvv 日志的部分内容,因为在发布问题时它被标记为垃圾邮件

答案1

好的,终于找到答案了。看来我在尝试通过 SSH 访问 VM B 的用户 root 时将公钥添加到了 VM B 中用户 vagrant 的 authorized_keys 中,这就是它不起作用的原因。

我在虚拟机 B 上将用户更改为 root,然后转到其主目录 (~)。虽然那里没有 .ssh,但我创建了它,然后向其中添加了一个名为 authorized_keys 的文件,在那里添加了公钥,然后使用 重新启动了 ssh 服务sudo systemctl restart ssh.service

现在我可以以 root 身份从 VM A ssh 进入 VM B。

相关内容