无法连接到 SSH 服务器。 /.ssh/authorized_keys:权限被拒绝

Posted

技术标签:

【中文标题】无法连接到 SSH 服务器。 /.ssh/authorized_keys:权限被拒绝【英文标题】:Unable to connect to SSH server. /.ssh/authorized_keys: Permission denied 【发布时间】:2018-08-07 18:11:45 【问题描述】:

在过去的 4-5 小时里,我一直在尝试通过 RSA 密钥设置具有 SSH 身份验证的 Ubuntu 服务器,但我无法让它工作。一直在谷歌没有任何运气。

我正在使用 SSH-2.0-OpenSSH_7.2p2 运行 Ubuntu 16.04.3 服务器。刚刚也重新安装了服务器。

我确保将~/.ssh 文件夹的权限设置为700/.ssh/authorized_keys 设置为600

密钥由 Puttygen(RSA 2048 位)生成,我在将其添加到 /home/user/.ssh/authorized_keys 之前将其正确格式化为 ssh-rsa <long random string> comment

这是我尝试使用密钥登录时遇到的错误:

login as: user
Server refused our key

这是我从 sshd 调试日志中得到的错误消息。

2 月 27 日 23:23:21 CSGO6 sshd[1439]:debug1:无法打开授权密钥“/home/user/.ssh/authorized_keys”:权限被拒绝

有谁明白为什么 sshd 没有权限访问 authorized_keys 文件?

这里是 sshd_config

# Package generated configuration file
# See the sshd_config(5) manpage for details

# What ports, IPs and protocols we listen for Port 22
# Use these options to restrict which interfaces/protocols sshd will bind to
#ListenAddress ::
#ListenAddress 0.0.0.0

Protocol 2
# HostKeys for protocol version 2
HostKey /etc/ssh/ssh_host_rsa_key
HostKey /etc/ssh/ssh_host_dsa_key
HostKey /etc/ssh/ssh_host_ecdsa_key
HostKey /etc/ssh/ssh_host_ed25519_key
#Privilege Separation is turned on for security
UsePrivilegeSeparation yes

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

# Logging
SyslogFacility AUTH
LogLevel DEBUG3

# Authentication:
LoginGraceTime 120
PermitRootLogin prohibit-password
StrictModes yes

RSAAuthentication yes
PubkeyAuthentication yes
#AuthorizedKeysFile     %h/.ssh/authorized_keys

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

# To enable empty passwords, change to yes (NOT RECOMMENDED)
PermitEmptyPasswords no

# Change to yes to enable challenge-response passwords (beware issues with
# some PAM modules and threads)
ChallengeResponseAuthentication no

# Change to no to disable tunnelled clear text passwords
#PasswordAuthentication yes

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

# GSSAPI options
#GSSAPIAuthentication no
#GSSAPICleanupCredentials yes

X11Forwarding yes
X11DisplayOffset 10
PrintMotd no
PrintLastLog yes
TCPKeepAlive yes
#UseLogin no

#MaxStartups 10:30:60
#Banner /etc/issue.net

# Allow client to pass locale environment variables
AcceptEnv LANG LC_*

Subsystem sftp /usr/lib/openssh/sftp-server

# 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 yes
user@CSGO6:~$ # Package generated configuration file
user@CSGO6:~$ # See the sshd_config(5) manpage for details
user@CSGO6:~$
user@CSGO6:~$ # What ports, IPs and protocols we listen for
user@CSGO6:~$ Port 22
#ListenAddress ::
#ListenAddress 0.0.0.0
Protocol 2
# HostKeys for protocol version 2
HostKey /etc/ssh/ssh_host_rsa_key
HostKey /etc/ssh/ssh_host_dsa_key
HostKey /etc/ssh/ssh_host_ecdsa_key
HostKey /etc/ssh/ssh_host_ed25519_key
#Privilege Separation is turned on for security
UsePrivilegeSeparation yes

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

# Logging
SyslogFacility AUTH
LogLevel DEBUG3

# Authentication:
LoginGraceTime 120
PermitRootLogin prohibit-password
StrictModes yes

RSAAuthentication yes
PubkeyAuthentication yes
#AuthorizedKeysFile     %h/.ssh/authorized_keys

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

# To enable empty passwords, change to yes (NOT RECOMMENDED)
PermitEmptyPasswords no

# Change to yes to enable challenge-response passwords (beware issues with
# some PAM modules and threads)
ChallengeResponseAuthentication no

# Change to no to disable tunnelled clear text passwords
#PasswordAuthentication yes

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

# GSSAPI options
#GSSAPIAuthentication no
#GSSAPICleanupCredentials yes

X11Forwarding yes
X11DisplayOffset 10
PrintMotd no
PrintLastLog yes
TCPKeepAlive yes
#UseLogin no

#MaxStartups 10:30:60
#Banner /etc/issue.net

# Allow client to pass locale environment variables
AcceptEnv LANG LC_*

Subsystem sftp /usr/lib/openssh/sftp-server

# 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 yes

这是.ssh 文件夹和authorized_keys 文件的ls -la

user@CSGO6:~$ ls -la /home/user/.ssh/
total 12
drwx------ 2 user user 4096 Feb 27 23:39 .
drwxr-xr-x 5 user user 4096 Feb 27 23:14 ..
-rw------- 1 root root  802 Feb 27 23:39 authorized_keys

顺便说一句,使用相同的 rsa 密钥 ssh 到我的树莓派没有问题。

【问题讨论】:

非常适合unix.stackexchange.com @GillesQuenot 是的。我刚刚在 *** 上看到了很多类似的问题,所以这就是为什么我认为这可能是一个很好的提问地方。 显示ls -la /home/user/.ssh/authorized_keys @zombic 我在帖子底部添加了.ssh文件夹的ls -la和authorized_keys。 【参考方案1】:

我终于成功了。

这就是我所做的: 我再次添加了密钥,但这次是通过 scp(使用 putty 程序 pscp.exe)而不是在使用 SSH 会话登录服务器时粘贴密钥。

然后我更改了密钥以匹配 OpenSSH 服务器想要的正确语法,将其重命名为 authorized_keys2 并将权限更改为 600。尝试使用我的密钥和 putty 登录。

不幸的是,我不知道为什么它现在突然起作用了,但我很高兴它确实起作用了。

【讨论】:

以上是关于无法连接到 SSH 服务器。 /.ssh/authorized_keys:权限被拒绝的主要内容,如果未能解决你的问题,请参考以下文章

无法通过 python 通过 SSH 连接到 postgres 服务器

Laravel 5.1 SSH - 无法连接到远程服务器

无法通过 SSH 连接到家用计算机 [关闭]

无法连接到 SSH 服务器。 /.ssh/authorized_keys:权限被拒绝

使用 SSH 隧道时无法通过套接字错误连接到本地 MySQL 服务器

Remmina:无法使用 SSH 连接到远程服务器,但我可以从 shell SSH