为什么即使使用错误的密钥我也能通过 SSH 连接远程机器?

为什么即使使用错误的密钥我也能通过 SSH 连接远程机器?

描述:我已经在 Windows PC 和 Raspberry Pi 之间创建了 ssh 连接。为此,我遵循了以下步骤:

步骤 1:获取 Raspberry Pi 的 IP 地址。它应该是这样的:192.168.1.52

第 2 步:打开 shell 并通过 ssh 访问 Raspberry Pi:

ssh [email protected]

您将需要密码。

步骤 3:在远程 PC 的主目录中使用这些命令:

mkdir .ssh

步骤 4:通过私钥/公钥保护 ssh 连接。在本地电脑上使用以下命令:

ssh-keygen -f .ssh/fede_windows -t rsa -b 4096

如果你的本地机器是基于 Linux 的,请运行以下行:

chmod 600 .ssh/fede_windows # if linux

最后:

scp .ssh/fede_windows.pub [email protected]:.ssh

步骤 5:在远程 PC 上使用以下命令:

sudo nano /etc/ssh/sshd_config

并修改配置文件的以下几行:

ChallengeResponseAuthentication no
PasswordAuthentication no
UsePAM no

最后:

sudo systemctl reload sshd

步骤 6:在远程计算机上使用以下命令:

cat ~/.ssh/fede_windows.pub >> ~/.ssh/authorized_keys
chmod 700 ~/.ssh/
chmod 600 ~/.ssh/*

步骤 7:在本地计算机运行此命令登录到远程计算机:

ssh -i .ssh/fede_windows [email protected]

问题:当我在我的 Ubuntu 电脑上再次执行所有这些步骤并生成一个名为 fede_ubuntu 的密钥时,看起来无论我在命令中插入什么,我都可以 ssh 我的 Raspberry Pi:

ssh -i .ssh/fede_xyz [email protected]

它一直有效,但这种情况不应该发生,因为它应该仅限于我刚刚创建的密钥。如果我切换到我的 Windows 机器,一切都会按预期工作,并且只有我指定正确的密钥才会起作用。

问题:您能否提出该问题的可能原因以及如何解决它?

编辑:通过输入以下命令我得到:ssh -i .ssh/key_that_does_not_exits -v [email protected]

Warning: Identity file .ssh/key_that_does_not_exits not accessible: No such file or directory.
OpenSSH_8.2p1 Ubuntu-4ubuntu0.3, OpenSSL 1.1.1f  31 Mar 2020
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 *
debug1: Connecting to 192.168.1.52 [192.168.1.52] port 22.
debug1: Connection established.
debug1: identity file /home/federico/.ssh/id_rsa type -1
debug1: identity file /home/federico/.ssh/id_rsa-cert type -1
debug1: identity file /home/federico/.ssh/id_dsa type -1
debug1: identity file /home/federico/.ssh/id_dsa-cert type -1
debug1: identity file /home/federico/.ssh/id_ecdsa type -1
debug1: identity file /home/federico/.ssh/id_ecdsa-cert type -1
debug1: identity file /home/federico/.ssh/id_ecdsa_sk type -1
debug1: identity file /home/federico/.ssh/id_ecdsa_sk-cert type -1
debug1: identity file /home/federico/.ssh/id_ed25519 type -1
debug1: identity file /home/federico/.ssh/id_ed25519-cert type -1
debug1: identity file /home/federico/.ssh/id_ed25519_sk type -1
debug1: identity file /home/federico/.ssh/id_ed25519_sk-cert type -1
debug1: identity file /home/federico/.ssh/id_xmss type -1
debug1: identity file /home/federico/.ssh/id_xmss-cert type -1
debug1: Local version string SSH-2.0-OpenSSH_8.2p1 Ubuntu-4ubuntu0.3
debug1: Remote protocol version 2.0, remote software version OpenSSH_7.9p1 Raspbian-10+deb10u2+rpt1
debug1: match: OpenSSH_7.9p1 Raspbian-10+deb10u2+rpt1 pat OpenSSH* compat 0x04000000
debug1: Authenticating to 192.168.1.52:22 as 'pi'
debug1: SSH2_MSG_KEXINIT sent
debug1: SSH2_MSG_KEXINIT received
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
debug1: expecting SSH2_MSG_KEX_ECDH_REPLY
debug1: Server host key: ecdsa-sha2-nistp256 SHA256:hC5w2kDxgHH5eFRY1vOJaS7ipPR+8OWX2tkkEZbF194
debug1: Host '192.168.1.52' is known and matches the ECDSA host key.
debug1: Found key in /home/federico/.ssh/known_hosts:1
debug1: rekey out after 134217728 blocks
debug1: SSH2_MSG_NEWKEYS sent
debug1: expecting SSH2_MSG_NEWKEYS
debug1: SSH2_MSG_NEWKEYS received
debug1: rekey in after 134217728 blocks
debug1: Will attempt key: federico@federico RSA SHA256:E96Hu2Ee+IyAuoZ06GxTvo+ZmAkzqfihbAKkFqxU1AU agent
debug1: Will attempt key: /home/federico/.ssh/id_rsa 
debug1: Will attempt key: /home/federico/.ssh/id_dsa 
debug1: Will attempt key: /home/federico/.ssh/id_ecdsa 
debug1: Will attempt key: /home/federico/.ssh/id_ecdsa_sk 
debug1: Will attempt key: /home/federico/.ssh/id_ed25519 
debug1: Will attempt key: /home/federico/.ssh/id_ed25519_sk 
debug1: Will attempt key: /home/federico/.ssh/id_xmss 
debug1: SSH2_MSG_EXT_INFO received
debug1: kex_input_ext_info: server-sig-algs=<ssh-ed25519,ssh-rsa,rsa-sha2-256,rsa-sha2-512,ssh-dss,ecdsa-sha2-nistp256,ecdsa-sha2-nistp384,ecdsa-sha2-nistp521>
debug1: SSH2_MSG_SERVICE_ACCEPT received
debug1: Authentications that can continue: publickey,keyboard-interactive
debug1: Next authentication method: publickey
debug1: Offering public key: federico@federico RSA SHA256:E96Hu2Ee+IyAuoZ06GxTvo+ZmAkzqfihbAKkFqxU1AU agent
debug1: Server accepts key: federico@federico RSA SHA256:E96Hu2Ee+IyAuoZ06GxTvo+ZmAkzqfihbAKkFqxU1AU agent
debug1: Authentication succeeded (publickey).
Authenticated to 192.168.1.52 ([192.168.1.52]:22).
debug1: channel 0: new [client-session]
debug1: Requesting [email protected]
debug1: Entering interactive session.
debug1: pledge: network
debug1: client_input_global_request: rtype [email protected] want_reply 0
debug1: Remote: /home/pi/.ssh/authorized_keys:2: key options: agent-forwarding port-forwarding pty user-rc x11-forwarding
debug1: Remote: /home/pi/.ssh/authorized_keys:2: key options: agent-forwarding port-forwarding pty user-rc x11-forwarding
debug1: Sending environment.
debug1: Sending env LC_ADDRESS = it_IT.UTF-8
debug1: Sending env LC_NAME = it_IT.UTF-8
debug1: Sending env LC_MONETARY = it_IT.UTF-8
debug1: Sending env LC_PAPER = it_IT.UTF-8
debug1: Sending env LANG = en_US.UTF-8
debug1: Sending env LC_IDENTIFICATION = it_IT.UTF-8
debug1: Sending env LC_TELEPHONE = it_IT.UTF-8
debug1: Sending env LC_MEASUREMENT = it_IT.UTF-8
debug1: Sending env LC_TIME = it_IT.UTF-8
debug1: Sending env LC_NUMERIC = it_IT.UTF-8
Last login: Sun Aug 22 22:26:00 2021 from 192.168.1.197

答案1

命令cat ~/.ssh/fede_windows.pub >> ~/.ssh/authorized_keys 附加关键在于authorized_keys,它不会取代已经存在的东西。

代替所有旧密钥,运行cat foobar.pub > ~/.ssh/authorized_keys。这将截断文件,然后添加新信息。

您可以添加任意数量的键。要检查,只需使用文本编辑器打开文件并查看内容即可。

拥有多把钥匙的能力实际上是一种安全特性。这意味着你可以使用来自不同计算机的不同钥匙,并且如果计算机丢失,你只需要移除被盗用的钥匙,而不必移除所有钥匙。

相关内容