使用 SSH 公钥登录不起作用

使用 SSH 公钥登录不起作用

我有家用电脑和服务器。我正尝试使用私钥登录服务器。

在我的服务器上,我有一个用户serveruser,我使用我的家用电脑登录

ssh -Y [email protected]

homeuser在我的家用电脑上,我有家用电脑上的用户。

现在,我想使用我的私钥登录服务器。为此,我已经有一个密钥,密码为/home/homeuser/.ssh/。所以我拿到了公钥 (id_rsa.pub),并将其复制到我的服务器/home/serveruser/.ssh/authorized_keys

现在我想使用该密钥登录,因此我尝试使用

ssh -Y [email protected]

它仍然要求我输入密码,而不是密码短语。

我也尝试使用

ssh -i /home/homeuser/.ssh/id_rsa -Y [email protected]

并且仍要求我输入用户密码。

我做错了什么?请指教。

感谢您的一切努力。

编辑:根据 Mason Heller 的建议,我执行了 ssh-add,但它仍然坚持要求我输入密码[电子邮件保护]

编辑:信息来自ssh -v -Y myserver.com(为匿名而修改的个人信息)。

OpenSSH_5.9p1 Debian-5ubuntu1.1, OpenSSL 1.0.1 14 Mar 2012
debug1: Reading configuration data /etc/ssh/ssh_config
debug1: /etc/ssh/ssh_config line 19: Applying options for *
debug1: Connecting to myserver.com [100.100.100.100] port 22.
debug1: Connection established.
debug1: identity file /home/homeuser/.ssh/id_rsa type 1
debug1: Checking blacklist file /usr/share/ssh/blacklist.RSA-2048
debug1: Checking blacklist file /etc/ssh/blacklist.RSA-2048
debug1: identity file /home/homeuser/.ssh/id_rsa-cert type -1
debug1: identity file /home/homeuser/.ssh/id_dsa type -1
debug1: identity file /home/homeuser/.ssh/id_dsa-cert type -1
debug1: identity file /home/homeuser/.ssh/id_ecdsa type -1
debug1: identity file /home/homeuser/.ssh/id_ecdsa-cert type -1
debug1: Remote protocol version 2.0, remote software version OpenSSH_6.0p1 Debian-4
debug1: match: OpenSSH_6.0p1 Debian-4 pat OpenSSH*
debug1: Enabling compatibility mode for protocol 2.0
debug1: Local version string SSH-2.0-OpenSSH_5.9p1 Debian-5ubuntu1.1
debug1: SSH2_MSG_KEXINIT sent
debug1: SSH2_MSG_KEXINIT received
debug1: kex: server->client aes128-ctr hmac-md5 none
debug1: kex: client->server aes128-ctr hmac-md5 none
debug1: sending SSH2_MSG_KEX_ECDH_INIT
debug1: expecting SSH2_MSG_KEX_ECDH_REPLY
debug1: Server host key: ECDSA b2:1a:68:21:5a:72:c4:f7:ec:ea:60:12:e4:f8:b5:71
debug1: Host 'myserver.com' is known and matches the ECDSA host key.
debug1: Found key in /home/homeuser/.ssh/known_hosts:11
debug1: ssh_ecdsa_verify: signature correct
debug1: SSH2_MSG_NEWKEYS sent
debug1: expecting SSH2_MSG_NEWKEYS
debug1: SSH2_MSG_NEWKEYS received
debug1: Roaming not allowed by server
debug1: SSH2_MSG_SERVICE_REQUEST sent
debug1: SSH2_MSG_SERVICE_ACCEPT received
debug1: Authentications that can continue: publickey,password
debug1: Next authentication method: publickey
debug1: Offering RSA public key: /home/homeuser/.ssh/id_rsa
debug1: Authentications that can continue: publickey,password
debug1: Offering RSA public key: [email protected]
debug1: Authentications that can continue: publickey,password
debug1: Trying private key: /home/homeuser/.ssh/id_dsa
debug1: Trying private key: /home/homeuser/.ssh/id_ecdsa
debug1: Next authentication method: password

更多信息:

cat /home/homeuser/.ssh/id_rsa.pub

--removed for security--

/home/serveruser/.ssh/authorized_keys

total 4
-rwx------ 1 serveruser serveruser 400 Jan 26 01:09 id_rsa.pub

猫/home/serveruser/.ssh/authorized_keys

cat: /home/serveruser/.ssh/authorized_keys: Is a directory

答案1

/home/serveruser/.ssh/authorized_keys应该是一个文件。

您可以通过将 id_rsa.pub 的内容添加到其中来创建它(即 cat /home/homeuser/.ssh/id_rsa.pub >> authorized_keys)。

(显然您必须先将公钥复制到服务器。)

答案2

为了使用您的密钥对,您可能需要运行 ssh-agent 来处理身份验证。

eval $(ssh-agent)
ssh-add

然后,您的密钥对将可用于该终端中的所有后续 ssh 会话。ssh-agent在您的终端会话中添加环境变量,您需要使用这些变量才能ssh使用它。我通常ssh-agent > ~/.ssha; . ~/.ssha; ssh-add在一个 pty 中运行,然后source ~/.ssha在任何其他需要使用 ssh 的地方运行,以便我的所有 pty 都可以使用代理。

此外,我发现使用它更容易ssh-copy-id将我的公钥添加到服务器。它也可能让你的生活更轻松。:)

相关内容