【问题标题】:SSHD Gives error could not open Authorized Keys, although permissions seem correctSSHD Gives 错误无法打开授权密钥,尽管权限似乎正确
【发布时间】:2014-01-08 10:38:32
【问题描述】:

由于 /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

我在 root 上设置了以下权限

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 6.4 版(最终版)

我已经添加了我的 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

任何想法都将不胜感激。

【问题讨论】:

  • 我假设您将 StrictModes 设置为 no 进行测试?您通常应该将其保留为默认 yes。您在 sshd 配置中还有哪些更改?

标签: ssh centos centos6 sshd


【解决方案1】:

如果权限正确,SELinux 可能仍会阻止 sshd 打开文件。

尝试修复 .ssh 目录(可能还有 $HOME)中的标签:

restorecon -FRvv ~/.ssh

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

【讨论】:

  • +1,尤其是对于 “我故意不建议禁用 SELinux 或将其设置为许可模式 :)” 听起来您认为这是最好的方法(我们在同一页上 :)) 但你已经知道会有很多紧张的“道德主义者”会给你很多反对票:-)
  • 我遇到了同样的问题。我想我需要阅读更多关于 SELinux 的内容。看起来像一个强大但令人困惑的工具?
  • 我遇到了同样的问题,原因相同,但restorecon 没有用。我必须明确更改 SELinux 类型。 chcon -Rv -t ssh_home_t ~/.ssh
  • Jonathan 上面的评论是我需要做的来解决我的问题。我自己创建了 .ssh 文件夹,所以我认为没有添加任何安全上下文(这就是“restorecon”不起作用的原因......)。我使用的是 CentOS 7。
  • 当我从未接触过 SELinux 导致问题时(并且破坏 SSH 是一个严重的问题),并且当我在一个无法访问互联网的沙盒、单一用途 VM 中时,禁用 SELinux 是绝对正确的答案。
【解决方案2】:

如果启用 SELinux:

$ getenforce
Enforcing

临时启用 pub-key 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,您就可以做到这一点。

【讨论】:

  • 在这个问题上花了一整天的时间,这是我在启用 CentOS7 SELinux 时唯一有效的方法。当您说临时与永久时,我不明白其中的区别。
  • 临时 - 持续到下次重新启动。永久 - 在重新启动后仍然存在。
  • 我觉得这将是一个漫长的旅程:)
  • 到底应该怎么解决这个问题?谢谢你,你救了我的一天!
【解决方案3】:

我也在努力使用密钥身份验证。

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

当我来到这里时,已经检查了上述所有内容(谷歌上的第一个链接)。 我知道这是一篇旧帖子,但我会在此处添加它,以防其他人遇到与我相同的问题并最终出现在这里。

我将 authorized_keys 文件的所有者设置为“root”,因此将其更改为:

chown myUserName authorized_keys2

帮我解决了。

【讨论】:

  • 这对我有帮助。在我的例子中,authorized_keys 文件是由 root/sudo user 创建的,因此它显示 'root' 作为 userowner 和 'root' 作为 groupowner 。我分别使用 chown 和 chgrp 命令将其更改为我的用户和组,之后我能够使用我的用户对服务器执行 ssh。
【解决方案4】:

检查 /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]:错误:收到与 X.X.X.X 的断开连接:14:没有可用的支持的身份验证方法 [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 ...

【讨论】:

    【解决方案5】:

    我已经花了好几个小时来处理这个废话。阅读大量对该问题的启发式解决方案,这里是他们都没有说清楚的总结。

    总结。

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

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

    所有权

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

    权限

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

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

    注意。诸如777 之类的权限会给出与000 相同的错误,即使不打开文件是应用程序策略。这可能是整个过程中最大的混乱来源。

    用户

    所有登录用户都必须存在。通过记录用户,我们的意思是:

    • 用户发送的网址为ssh root@localhost
    • 或者,公钥中的用户存在于客户端计算机上(~/.ssh/id_rsa.pub 在客户端,而不是在服务器中)

    【讨论】:

      【解决方案6】:

      需要检查的几个想法:

      • 你能查到authorized_keys 吗?文件是什么样的?
      • 您的 sshd 是否配置为允许 root 登录?这通常是不受欢迎的,
      • 您是以 root 身份还是以 sudoer 身份执行此操作?

      【讨论】:

      • 1.是的,它看起来是正确的,里面有我的钥匙,就是这样。我将它与无需密码即可 ssh 进入的另一台服务器进行了比较,结果完全相同。 2.上面的问题增加了SSHD配置,即启用。 3. 我不确定你的意思。当我从客户端 PC 远程登录服务器时,我使用“ssh root@hostname”,现在在服务器上我以 root 身份登录,进行这些更改。
      【解决方案7】:
      1. 不要在~/.ssh/... 上做chmod。尝试写出确切的路径:/root/.ssh/...,因为有时(使用su 等时)~ 可能设置不正确。再次检查并发布完整路径的权限,不要在命令中使用~

      2. 一旦你确定权限没问题,检查你的sshd是否真的在root用户下运行:ps -A u | grep sshd

      【讨论】:

      • 感谢您的帮助。我以 root 身份登录并从目录本身重新运行这些命令。没有运气。并且 sshd 以 root 身份运行。
      【解决方案8】:

      需要仔细检查的几件事:

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

      【讨论】:

      • 感谢您的帮助。是的,没有坏字符,是的。
      【解决方案9】:

      我遇到了同样的问题,并通过同时更改 .ssh 和 authorized_keys 的所有者来解决: chown 我的用户名:我的用户名 .ssh chown MyUsername:Myusername .ssh/authorized_keys

      感谢@niclaslindgren。

      顺便说一句,无论authorized_keys中是否有^M,我已经测试并证明了,它适用于两种方式

      【讨论】:

        【解决方案10】:

        我也打算在这里给出我的答案,因为我刚刚浪费了最后一个小时试图找出解决方法,而我之前在 Azure 上又浪费了​​一个小时。

        这不起作用:

        AuthorizedKeyFiles ~/.ssh/authorized_keys
        

        在使用上述配置时,使用备用端口(即/usr/sbin/sshd -ddd -p 12345)生成服务器端日志会显示以下内容:

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

        然而,以下任何一个都是正确的:

        AuthorizedKeyFiles /home/%u/.ssh/authorized_keys
        
        AuthorizedKeyFiles .ssh/authorized_keys
        

        另请参阅:https://www.ssh.com/ssh/authorized_keys/openssh

        【讨论】:

          【解决方案11】:

          对我来说,它也在改变所有权,但不仅仅是 authorized_keys 的所有权,还有.sdd 目录的

          chown -R user:user ./home/user/.ssd
          

          【讨论】:

            【解决方案12】:

            在我的例子中,主目录位于 NFS 上,这意味着 ~/.ssh 也在 NFS 上。

            找到this 的答案,确实(以root 身份)运行以下命令解决了问题:

            setsebool -P use_nfs_home_dirs 1
            

            【讨论】:

              猜你喜欢
              • 2015-04-29
              • 2012-08-16
              • 1970-01-01
              • 1970-01-01
              • 1970-01-01
              • 1970-01-01
              • 2015-04-01
              • 2012-03-22
              • 2016-08-19
              相关资源
              最近更新 更多