我正在尝试使用密钥设置 ssh 访问 - 我已将公钥添加到服务器上的 .ssh 目录中。我也重新启动了服务器上的 ssh 服务。当我从本地计算机登录到服务器时,我收到一条拒绝消息(复制如下 - 使用 v 标志)。我从问题中替换了用户名和 IP 地址。知道我可能做错了什么吗?
ssh -v [email protected]
OpenSSH_6.6.1, OpenSSL 1.0.1f 6 Jan 2014
debug1: Reading configuration data /etc/ssh/ssh_config
debug1: /etc/ssh/ssh_config line 19: Applying options for *
debug1: Connecting to xxx.xxx.xxx.xxx [xxx.xxx.xxx.xxx] port 22.
debug1: Connection established.
debug1: identity file /home/user/.ssh/id_rsa type 1
debug1: identity file /home/user/.ssh/id_rsa-cert type -1
debug1: identity file /home/user/.ssh/id_dsa type -1
debug1: identity file /home/user/.ssh/id_dsa-cert type -1
debug1: identity file /home/user/.ssh/id_ecdsa type -1
debug1: identity file /home/user/.ssh/id_ecdsa-cert type -1
debug1: identity file /home/user/.ssh/id_ed25519 type -1
debug1: identity file /home/user/.ssh/id_ed25519-cert type -1
debug1: Enabling compatibility mode for protocol 2.0
debug1: Local version string SSH-2.0-OpenSSH_6.6.1p1 Ubuntu-2ubuntu2
debug1: Remote protocol version 2.0, remote software version OpenSSH_6.6.1p1 Ubuntu-2ubuntu2.3
debug1: match: OpenSSH_6.6.1p1 Ubuntu-2ubuntu2.3 pat OpenSSH_6.6.1* compat 0x04000000
debug1: SSH2_MSG_KEXINIT sent
debug1: SSH2_MSG_KEXINIT received
debug1: kex: server->client aes128-ctr [email protected] none
debug1: kex: client->server aes128-ctr [email protected] none
debug1: sending SSH2_MSG_KEX_ECDH_INIT
debug1: expecting SSH2_MSG_KEX_ECDH_REPLY
debug1: Server host key: ECDSA dd:e7:a3:31:60:21:c5:90:24:b5:11:65:43:d8:cd:a0
debug1: Host 'xxx.xxx.xxx.xxx' is known and matches the ECDSA host key.
debug1: Found key in /home/user/.ssh/known_hosts:1
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
debug1: Next authentication method: publickey
debug1: Offering RSA public key: /home/user/.ssh/id_rsa
debug1: Authentications that can continue: publickey
debug1: Trying private key: /home/user/.ssh/id_dsa
debug1: Trying private key: /home/user/.ssh/id_ecdsa
debug1: Trying private key: /home/user/.ssh/id_ed25519
debug1: No more authentication methods to try.
Permission denied (publickey).
答案1
确保 .ssh 目录和 authorized_keys 的权限正确,例如 .ssh 的权限为 0700,~/.ssh/authorized_keys 的权限为 0600。如果不对,请先修复此问题。
确保您的服务器上启用了密钥验证,但默认情况下这应该是可以的,请检查您的 sshd_config 进行确认。
检查服务器上的日志文件,在 RedHat / Fedora 和大多数 Linux 发行版中,你可以在 /var/log/secure 中找到它们
您不需要重新启动 sshd 守护进程来让他知道您新添加的密钥。
最好使用 ssh-copy-id 将密钥复制到服务器。
看这里,它是 Ubuntu 特有的,但当我们谈论 OpenSSH 时它无论如何都可以工作:
答案2
在我的例子中,我必须将我创建的 id_rsa 文件(私钥)复制到 ssh 进程正在寻找的 id_dsa 文件中(之前未创建)。使用 ssh -v 命令帮助我解决了这个问题。