使用无密码 SSH 密钥时无法通过 SSH 进入服务器

使用无密码 SSH 密钥时无法通过 SSH 进入服务器

这可能是一个愚蠢的问题,但我会尽力提供尽可能多的信息。如果您需要任何其他详细信息,请告诉我。

我有一台运行 Ventura 的 Mac 和一台在 VMWare Fusion Pro 13 中运行的 AlmaLinux 虚拟机。AlmaLinux 是 9.1 版本。

首先我在 macOS 上创建了我的 SSH 密钥对:

ssh-keygen -t ed25519 -f ~/.ssh/localdevonly

然后我在 macOS 上也创建了一个config文件。~/.ssh/config

该文件包含以下内容:

Host localdev
    HostName 172.16.91.137
    User simon
    IdentityFile ~/.ssh/localdevonly

然后我上传了 SSH 公钥:

scp ~/.ssh/localdevonly.pub simon@localdev:/home/simon/.ssh/authorized_keys

authorized_keys我在 AlmaLinux 中更改了该文件的权限:

chmod 444 ~/.ssh/authorized_keys

然后我将 SSH 密钥添加到 macOS 上的 SSH 代理:

ssh-add ~/.ssh/localdevonly

然后我尝试从 macOS 登录:

ssh localdev -v

输出结果如下:

OpenSSH_9.0p1, LibreSSL 3.3.6
debug1: Reading configuration data /Users/simon/.ssh/config
debug1: /Users/simon/.ssh/config line 1: Applying options for localdev
debug1: Reading configuration data /etc/ssh/ssh_config
debug1: /etc/ssh/ssh_config line 21: include /etc/ssh/ssh_config.d/* matched no files
debug1: /etc/ssh/ssh_config line 54: Applying options for *
debug1: Authenticator provider $SSH_SK_PROVIDER did not resolve; disabling
debug1: Connecting to 172.16.91.137 [172.16.91.137] port 22.
debug1: Connection established.
debug1: identity file /Users/simon/.ssh/localdevonly type 3
debug1: identity file /Users/simon/.ssh/localdevonly-cert type -1
debug1: Local version string SSH-2.0-OpenSSH_9.0
debug1: Remote protocol version 2.0, remote software version OpenSSH_8.7
debug1: compat_banner: match: OpenSSH_8.7 pat OpenSSH* compat 0x04000000
debug1: Authenticating to 172.16.91.137:22 as 'simon'
debug1: load_hostkeys: fopen /Users/simon/.ssh/known_hosts2: No such file or directory
debug1: load_hostkeys: fopen /etc/ssh/ssh_known_hosts: No such file or directory
debug1: load_hostkeys: fopen /etc/ssh/ssh_known_hosts2: No such file or directory
debug1: SSH2_MSG_KEXINIT sent
debug1: SSH2_MSG_KEXINIT received
debug1: kex: algorithm: curve25519-sha256
debug1: kex: host key algorithm: ssh-ed25519
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: SSH2_MSG_KEX_ECDH_REPLY received
debug1: Server host key: ssh-ed25519 SHA256:bVk7Pm91A5qGjVI+6o+nAo8+4qm+sIyrgmZiwJhH1Dw
debug1: load_hostkeys: fopen /Users/simon/.ssh/known_hosts2: No such file or directory
debug1: load_hostkeys: fopen /etc/ssh/ssh_known_hosts: No such file or directory
debug1: load_hostkeys: fopen /etc/ssh/ssh_known_hosts2: No such file or directory
debug1: Host '172.16.91.137' is known and matches the ED25519 host key.
debug1: Found key in /Users/simon/.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: get_agent_identities: bound agent to hostkey
debug1: get_agent_identities: agent returned 3 keys
debug1: Will attempt key: /Users/simon/.ssh/localdevonly ED25519 SHA256:CsSaNoNXrF/Fdxfiaj8NR1b1TC2/lRy7aXpyt7Bm9sk explicit agent
debug1: Will attempt key: [email protected] ED25519 SHA256:ADJnS0Xnk/Al1TJrahM8AWDXKhrapjwxhz9P17z3cUw agent
debug1: Will attempt key: MuzikStrm ED25519 SHA256:hu4rJT60Pk1Jx9IT73wlmrLPIg6ya3In1JdNFkIVlso agent
debug1: SSH2_MSG_EXT_INFO received
debug1: kex_input_ext_info: server-sig-algs=<ssh-ed25519,[email protected],ssh-rsa,rsa-sha2-256,rsa-sha2-512,ssh-dss,ecdsa-sha2-nistp256,ecdsa-sha2-nistp384,ecdsa-sha2-nistp521,[email protected],[email protected]>
debug1: SSH2_MSG_SERVICE_ACCEPT received
debug1: Authentications that can continue: publickey,keyboard-interactive
debug1: Next authentication method: publickey
debug1: Offering public key: /Users/simon/.ssh/localdevonly ED25519 SHA256:CsSaNoNXrF/Fdxfiaj8NR1b1TC2/lRy7aXpyt7Bm9sk explicit agent
debug1: Authentications that can continue: publickey,keyboard-interactive
debug1: Offering public key: [email protected] ED25519 SHA256:ADJnS0Xnk/Al1TJrahM8AWDXKhrapjwxhz9P17z3cUw agent
debug1: Authentications that can continue: publickey,keyboard-interactive
debug1: Offering public key: MuzikStrm ED25519 SHA256:hu4rJT60Pk1Jx9IT73wlmrLPIg6ya3In1JdNFkIVlso agent
debug1: Authentications that can continue: publickey,keyboard-interactive
debug1: Next authentication method: keyboard-interactive
debug1: Authentications that can continue: publickey,keyboard-interactive
debug1: No more authentication methods to try.
[email protected]: Permission denied (publickey,keyboard-interactive).

我不知道如何解决这个问题。我被难住了。如果有人能给我指明正确的方向,我将不胜感激!

答案1

在服务器上,/var/log/secure会标记该authorized_keys文件或~/.ssh目录由于权限(模式)或所有权而不被接受。

答案2

请设置以下权限并重试:

chmod 600 ~/.ssh/authorized_keys

chmod 700 ~/.ssh

并且最好使用它来ssh-copy-id代替scp命令。

相关内容