描述

描述

描述

我已经ssh在我的 MS-Windows PC 和 Raspberry Pi 之间创建了连接。为此,我按照以下步骤操作:

第 1 步:以某种方式获取 Raspberry Pi 的 IP 地址。应该是这样的:192.168.1.52

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

ssh [email protected]

您将需要密码。

步骤 3:在远程电脑的主目录中使用以下命令:

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:在远程电脑中使用以下命令:

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]

问题:当我通过生成名为 fede_ubuntu 的密钥在 Ubuntu PC 中再次执行所有这些步骤时,看起来无论我在命令中插入什么内容,我都能够 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

从你的日志中:

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).

这意味着正在使用来自 SSH 代理的密钥。我假设它是您最近使用过的有效密钥,并且您已手动添加到代理中,或者由于AddKeysToAgent yes~/.ssh/config文件中使用而添加了该密钥。

从 SSH 代理中删除所有身份,然后重试:ssh-add -D。或者,添加IdentityAgent none到您的 SSH 配置或ssh -o IndentityAgent=none在调用 时使用ssh

您还可能会受益于IdentitiesOnly yes在 SSH 配置中使用,这将使您能够更好地控制向 SSH 服务器提供的身份。请参阅 中有关此选项的文档ssh_config(5)

答案2

ssh 找不到您使用 -i 选项建议的密钥,因此它会尝试其他可用的密钥。您在 .ssh 目录中有一个有效密钥,该密钥被接受并建立连接。如果您不想使用该密钥,请将其删除或使用密码保护它,以便您必须提供密码才能允许连接。

您没有使用“错误密钥”进行连接,它使用的密钥位于您的 .ssh 目录中并且对于连接有效。

相关内容