為什麼即使使用錯誤的密鑰,我也能通過 SSH 連接遠端機器?
描述
我已經
ssh
在我的 MS-Windows PC 和 Raspberry Pi 之間建立了連接。為此,我按照以下步驟操作:第 1 步:以某種方式獲取 Raspberry Pi 的 IP 地址。它應該是這樣的:192.168.1.52
第 2 步:打開 shell 並通過 ssh 訪問樹莓派:
ssh pi@192.168.1.52
您將需要密碼。
第 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 pi@192.168.1.52:.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 pi@192.168.1.52
問題:當我通過生成一個名為 fede_ubuntu 的密鑰在我的 Ubuntu Pc 中再次執行所有這些步驟時,看起來無論我在命令中插入什麼內容,我都能夠對我的 Raspberry Pi 進行 ssh:
ssh -i .ssh/fede_xyz pi@192.168.1.52
它一直有效,這不應該發生,因為它應該僅限於我剛剛創建的密鑰。如果我切換到我的 Windows 機器,一切都會按預期工作,並且只有當我指定正確的鍵才能工作。
問題:您能否提出此問題的可能原因以及如何解決?
編輯:通過輸入以下命令
ssh -i .ssh/key_that_does_not_exits -v pi@192.168.1.52
,我得到: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: 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: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 no-more-sessions@openssh.com debug1: Entering interactive session. debug1: pledge: network debug1: client_input_global_request: rtype hostkeys-00@openssh.com 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
從您的日誌中:
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)
。