SSHD出现错误,无法打开已授权密钥,尽管权限似乎正确。

42

根据调试日志,在/var/log/secure中出现以下错误,导致我无法登录SSH:

Dec 19 18:01:05 hostname sshd[25119]: debug1: trying public key file /root/.ssh/authorized_keys
Dec 19 18:01:05 hostname sshd[25119]: debug1: Could not open authorized keys '/root/.ssh/authorized_keys': Permission denied

我在根目录上设置了以下权限。
chmod 700 ~/.ssh
chmod 600 ~/.ssh/authorized_keys
chmod go-wrx ~

ls -lah命令会为这些目录显示以下输出:

drwx------.   6 root root 4.0K Dec 19 17:46 root
drwx------.  2 root root 4.0K Dec 19 17:41 .ssh
-rw-------. 1 root root  416 Dec 19 17:12 authorized_keys

我知道我使用的密钥是正确的,因为我刚刚在另一台服务器上设置了它而没有任何问题。
我正在运行:CentOS release 6.4(Final)
我已经添加了我的sshd配置,以防有什么配置不正确可能会导致问题:
#       $OpenBSD: sshd_config,v 1.80 2008/07/02 02:24:18 djm Exp $

# This is the sshd server system-wide configuration file.  See
# sshd_config(5) for more information.

# This sshd was compiled with PATH=/usr/local/bin:/bin:/usr/bin

# The strategy used for options in the default sshd_config shipped with
# OpenSSH is to specify options with their default value where
# possible, but leave them commented.  Uncommented options change a
# default value.

#Port 22
#AddressFamily any
#ListenAddress 0.0.0.0
#ListenAddress ::

# Disable legacy (protocol version 1) support in the server for new
# installations. In future the default will change to require explicit
# activation of protocol 1
Protocol 2

# HostKey for protocol version 1
#HostKey /etc/ssh/ssh_host_key
# HostKeys for protocol version 2
#HostKey /etc/ssh/ssh_host_rsa_key
#HostKey /etc/ssh/ssh_host_dsa_key

# Lifetime and size of ephemeral version 1 server key
#KeyRegenerationInterval 1h
#ServerKeyBits 1024

# Logging
# obsoletes QuietMode and FascistLogging
#SyslogFacility AUTH
SyslogFacility AUTHPRIV
LogLevel DEBUG

# Authentication:

#LoginGraceTime 2m
PermitRootLogin yes
StrictModes no
#MaxAuthTries 6
#MaxSessions 10

RSAAuthentication yes
PubkeyAuthentication yes
AuthorizedKeysFile      .ssh/authorized_keys
#AuthorizedKeysCommand none
#AuthorizedKeysCommandRunAs nobody

# For this to work you will also need host keys in /etc/ssh/ssh_known_hosts
#RhostsRSAAuthentication no
# similar for protocol version 2
#HostbasedAuthentication no
# Change to yes if you don't trust ~/.ssh/known_hosts for
# RhostsRSAAuthentication and HostbasedAuthentication
#IgnoreUserKnownHosts no
# Don't read the user's ~/.rhosts and ~/.shosts files
IgnoreRhosts yes

# To disable tunneled clear text passwords, change to no here!
#PasswordAuthentication yes
#PermitEmptyPasswords no
PasswordAuthentication yes

# Change to no to disable s/key passwords
#ChallengeResponseAuthentication yes
ChallengeResponseAuthentication no

# Kerberos options
#KerberosAuthentication no
#KerberosOrLocalPasswd yes
#KerberosTicketCleanup yes
#KerberosGetAFSToken no
#KerberosUseKuserok yes

# GSSAPI options
#GSSAPIAuthentication no
GSSAPIAuthentication yes
#GSSAPICleanupCredentials yes
GSSAPICleanupCredentials yes
#GSSAPIStrictAcceptorCheck yes
#GSSAPIKeyExchange no

# Set this to 'yes' to enable PAM authentication, account processing,
# and session processing. If this is enabled, PAM authentication will
# be allowed through the ChallengeResponseAuthentication and
# PasswordAuthentication.  Depending on your PAM configuration,
# PAM authentication via ChallengeResponseAuthentication may bypass
# the setting of "PermitRootLogin without-password".
# If you just want the PAM account and session checks to run without
# PAM authentication, then enable this but set PasswordAuthentication
# and ChallengeResponseAuthentication to 'no'.
#UsePAM no
UsePAM yes

# Accept locale-related environment variables
AcceptEnv LANG LC_CTYPE LC_NUMERIC LC_TIME LC_COLLATE LC_MONETARY LC_MESSAGES
AcceptEnv LC_PAPER LC_NAME LC_ADDRESS LC_TELEPHONE LC_MEASUREMENT
AcceptEnv LC_IDENTIFICATION LC_ALL LANGUAGE
AcceptEnv XMODIFIERS

#AllowAgentForwarding yes
#AllowTcpForwarding yes
#GatewayPorts no
#X11Forwarding no
X11Forwarding yes
#X11DisplayOffset 10
#X11UseLocalhost yes
#PrintMotd yes
#PrintLastLog yes
#TCPKeepAlive yes
#UseLogin no
#UsePrivilegeSeparation yes
#PermitUserEnvironment no
#Compression delayed
#ClientAliveInterval 0
#ClientAliveCountMax 3
#ShowPatchLevel no
UseDNS no
#PidFile /var/run/sshd.pid
#MaxStartups 10:30:100
#PermitTunnel no
#ChrootDirectory none

# no default banner path
#Banner none

# override default of no subsystems
Subsystem       sftp    /usr/libexec/openssh/sftp-server

# Example of overriding settings on a per-user basis
#Match User anoncvs
#       X11Forwarding no
#       AllowTcpForwarding no
#       ForceCommand cvs server

任何想法将不胜感激。

1
我假设你在测试时将“StrictModes”设置为“no”了吗?通常情况下,你应该将其保留为默认的“yes”。你在sshd配置中还改变了什么? - Raman
13个回答

119
如果权限正确,SELinux可能仍然阻止sshd打开该文件。 尝试修复.ssh目录(和可能的$HOME)内的标签:
restorecon -FRvv ~/.ssh

如果用户帐户使用非标准的主目录路径,则需要首先将默认标签添加到本地配置中:

semanage fcontext -a -t ssh_home_t "/srv/custom/\.ssh(/.*)?"

(我故意不建议禁用SELinux或将其设置为宽容模式。)

12
+1,特别是对于“我有意不建议禁用SELinux或将其设置为宽容模式 :)”这句话。听起来你认为这是最好的方法(我们在同一页面上:)但你已经知道会有很多拘谨的“道德主义���”会给你很多负面评价 :-) - Tomas
1
我遇到了同样的问题。我想我需要更多地了解SELinux。看起来是一个强大但令人困惑的工具? - Daniel
15
我遇到了和你一样的问题,原因也相同,但是 restorecon 没有起作用。我不得不显式地更改 SELinux 类型。chcon -Rv -t ssh_home_t ~/.ssh - Jonathan
1
当 SELinux 即使我从未接触过它(并且破坏 SSH 是一个严重的问题)也会引起问题时,而且当我处于一个沙盒化的、单一用途的 VM 中且没有互联网访问权限时,禁用 SELinux 绝对是正确的答案。 - Glenn Maynard
在Ubuntu上,我必须先安装它:sudo apt-get install policycoreutils - afrish
显示剩余5条评论

18

如果启用了SELinux:

$ getenforce
Enforcing

要临时启用公钥 SSL 登录到非标准用户主目录位置,请运行以下命令:

$ sudo chcon -t ssh_home_t /srv/jenkins/.ssh/authorized_keys /srv/jenkins/.ssh

$ ls -ldZ /srv/jenkins/.ssh/authorized_keys /srv/jenkins/.ssh/
drwxr-xr-x. jenkins jenkins system_u:object_r:ssh_home_t:s0  /srv/jenkins/.ssh/
-rw-r--r--. jenkins jenkins system_u:object_r:ssh_home_t:s0  /srv/jenkins/.ssh/authorized_keys

请查看https://linux.die.net/man/8/ssh_selinux了解详细信息。

要使SELinux设置永久生效,请运行以下命令:

$ sudo semanage fcontext -a -t ssh_home_t /srv/jenkins/.ssh/authorized_keys
$ sudo semanage fcontext -a -t ssh_home_t /srv/jenkins/.ssh
$ sudo restorecon -R -v /srv/jenkins/.ssh/

如果您使用的是现代版的RHEL、Oracle Linux或CentOS,则会遇到这个问题。


2
我在CentOS7启用SELinux的情况下花了一整天的时间解决这个问题,这是唯一有效的方法。我不明白你说临时和永久之间的区别。 - Raja Khoury
2
临时 - 持续到下一次重启。永久 - 经得起重启。 - gavenkoa
我感觉这将是一段漫长的旅程 :) - Raja Khoury
1
这到底该怎么解决啊?谢谢,老兄,你救了我的一天! - Pezhvak

10

我也曾经遇到过使用密钥认证的问题。

无法打开授权密钥 '/home/myUserName/.ssh/authorized_keys2':权限被拒绝

在我遇到这个问题时,已经检查了以上所有事项(通过谷歌搜索进入了这篇文章)。虽然我意识到这是一篇旧文章,但为了帮助其他遇到同样问题的人们,我还是将解决方法分享在这里。

我将 authorized_keys2 文件的所有者改为了 "myUserName",命令如下:

  

chown myUserName authorized_keys2

这样做解决了我的问题。


这对我很有帮助。在我的情况下,authorized_keys文件是由root/sudo用户创建的,因此它显示'userowner'为'root','groupowner'也为'root'。我使用chown和chgrp命令将其更改为我的用户和组,之后我就能够使用我的用户通过ssh连接到服务器了。 - sjethvani

4

我花了几个小时时间去解决这个问题,阅读了很多明晰的解决方案,这里是它们没有清楚表述的简化版。

总结。

以下内容适用于这些文件:

  • ~/.ssh/
  • ~/.ssh/authorized_keys

所有权

所有者和组必须是登录用户。

权限

组和公共权限必须设置为0(无权限)。是的!默认情况下sshd认为你很蠢。

当然,所有者必须被允许读取。

注意。诸如777的权限与000相同,即使该应用程序策略不允许打开文件也会产生相同的错误。这可能是整个过程中最令人困惑的最大原因。

用户

所有登录用户必须存在。我们所说的登录用户是:

  • 通过url发送的用户:ssh root@localhost
  • 或者客户端机器上(而不是服务器上)存在的公钥中的用户(~/.ssh/id_rsa.pub)

3

检查/home目录的权限。应该是

  • drwxr-xr-x. 9 root root 113 Jun 28 22:57 home

然后检查你的主目录:

  • drwxr----- 5 user group 124 May 18 17:00 User drwx------ 2 user group 29 May 18 12:05 .ssh -rw------- 1 user group 2235 Jun 28 23:09 authorized_keys



我的日志错误信息

/var/log/secure > sshd[22565]: error: Received disconnect from X.X.X.X: 14: No supported authentication methods available [preauth]

客户端

ssh user@X.X.X.X Permission denied (publickey). ssh -vvv user@X.X.X.X ... debug2: we did not send a packet, disable method debug1: No more authentication methods to try. Permission denied (publickey).
服务器端

  • service sshd stop

  • 运行sshd调试模式:

  • /usr/sbin/sshd -ddd

    ... debug1: trying public key file /home/USER/.ssh/authorized_keys debug1: Could not open authorized keys '/home/USER/.ssh/authorized_keys': Permission denied ...


0

如果要找到sshd拒绝接受连接或密钥的原因,有一组通用的步骤。

下面的细节是针对基于systemd的系统,但是使用其他系统的用户也可以轻松地找到解决方法。

如何系统地调试sshd?

开始监视日志

journalctl -u sshd -f

/etc/ssh/sshd_config 中的sshd日志记录设置为调试模式

LogLevel DEBUG

重启守护进程以使更改生效

systemctl restart sshd

我们已经在服务器端设置好了。

现在尝试客户端连接。

ssh -o IdentitiesOnly=yes -v -i ~/.ssh/key_to_the_kingdom king@kingdom.gov
  • IdentitiesOnly禁用除指定身份以外的任何尝试。
  • -v可增加客户端冗长度,以便用户能够查看客户端是否按预期运行(能否在客户端系统上找到并使用密钥,能否协商加密算法等)。

(不为客户端贴出示例。)


在服务器上(以kingdom.gov为例),我们应该在调试模式日志中看到类似以下的内容:
Jul 14 12:46:39 kingdom.gov sshd[4665]: debug1: userauth-request for user king service ssh-connection method none [preauth]
Jul 14 12:46:39 kingdom.gov sshd[4665]: debug1: attempt 0 failures 0 [preauth]
Jul 14 12:46:39 kingdom.gov sshd[4665]: debug1: PAM: initializing for "king"
Jul 14 12:46:39 kingdom.gov sshd[4665]: debug1: PAM: setting PAM_RHOST to "75.73.78.71"
Jul 14 12:46:39 kingdom.gov sshd[4665]: debug1: PAM: setting PAM_TTY to "ssh"
Jul 14 12:46:39 kingdom.gov sshd[4665]: debug1: userauth-request for user king service ssh-connection method publickey [preauth]
Jul 14 12:46:39 kingdom.gov sshd[4665]: debug1: attempt 1 failures 0 [preauth]
Jul 14 12:46:39 kingdom.gov sshd[4665]: debug1: userauth_pubkey: test pkalg rsa-sha2-512 pkblob RSA SHA256:0hjXPXkM8d91W2D8bg3fcapifm5QJd7QV9wwOEMU1 [preauth]
Jul 14 12:46:39 kingdom.gov sshd[4665]: debug1: temporarily_use_uid: 112233/10 (e=0/0)
Jul 14 12:46:39 kingdom.gov sshd[4665]: debug1: trying public key file /home/king/.ssh/authorized_keys
Jul 14 12:46:39 kingdom.gov sshd[4665]: debug1: Could not open authorized keys '/home/king/.ssh/authorized_keys': Permission denied
Jul 14 12:46:39 kingdom.gov sshd[4665]: debug1: restore_uid: 0/0
Jul 14 12:46:39 kingdom.gov sshd[4665]: Failed publickey for king from 83.69.65.84 port 52756 ssh2: RSA SHA256:0hjXPXkM8d91W2D8bg3fcapifm5QJd7QV9wwOEMU1

在我的情况下,问题出在SELinux不允许使用存储在NFS家目录中的authorized_keys

您可能会问:我该如何检查权限?sshd用于访问文件的身份是什么?

查看上面日志中的temporarily_use_uid: 112233/10。应该有正确的用户UID和主要GID。在我的情况下,这些值是从名称服务(LDAP)中获取的,并且与预期相符。

如果客户端用户身份不正确,请查看名称服务配置并解决此问题。

目录和文件所有权和权限都符合预期(至少对于~~/.ssh目录而言,u=x;如果由用户拥有,则authorized_keys至少为u=r)。很明显原因必须是类似SELinux的东西。

让我们来检查一下:

getsebool use_nfs_home_dirs
  use_nfs_home_dirs --> off

对于这种情况,Nadav Aharoni的答案解决了问题。

 setsebool -P use_nfs_home_dirs 1

清理

  • sshd_config 中的 LogLevel DEBUG 恢复为先前的值(或注释掉以恢复默认值)。
  • systemctl restart sshd

0
我遇到了同样的问题,通过同时更改 .ssh和authorized_keys的所有者来解决它: chown MyUsername:Myusername .ssh chown MyUsername:Myusername .ssh/authorized_keys
感谢@niclaslindgren。
另外顺便提一下,在authorized_keys中是否有^M并不重要,我已经测试并证明了它可以用两种方式运行。

0

检查一下以下几点:

  • 您能否cat authorized_keys?文件长什么样子?
  • 您的sshd是否配置为允许root登录?这通常是不被赞同的,
  • 您是以root身份还是sudoer身份执行的?

  1. 是的,看起来正确,它有我的密钥,就是这样。我将其与另一个可以无需密码进行ssh的服务器进行了比较,它完全相同。
  2. 在上面的问题中添加了SSHD配置,已启用。
  3. 我不确定你的意思。当我从客户端PC远程登录到服务器时,我使用“ssh root@hostname”,而现在我正在作为root登录到服务器并进行更改。
- newUserNameHere

0

需要双重检查的几件事情:

  1. 您确定将公钥复制到了authorized_keys而不是私钥吗? :-)
  2. 运行cat -tv authorized_keys。每行末尾有^M字符吗?在authorized_keys上运行dos2unix
  3. 在进行配置更改后,您是否重新启动了ssh守护程序?

感谢您的帮助。是的,没有坏字符,而且没问题。 - newUserNameHere

0
1. 不要对 `~/.ssh/...` 进行 chmod 操作。尽量写出完整路径:`/root/.ssh/...`,因为有时候(例如使用 su 等命令)`~` 的设置可能不正确。检查并再次发布完整路径的权限,不要在命令中使用 `~`。
2. 确保权限完全正确之后,检查你的 `sshd` 是否真的在 root 用户下运行:`ps -A u | grep sshd`。

谢谢您的帮助。我已经以root身份登录,并从目录本身内部重新运行了这些命令。但是没有成功。而且sshd正在以root身份运行。 - newUserNameHere

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接