由于/ 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
非常感谢任何想法。
答案 0 :(得分:82)
如果权限正确,SELinux可能仍会阻止sshd打开文件。
尝试修复.ssh目录内的标签(可能是$ HOME):
restorecon -FRvv ~/.ssh
(我故意不建议禁用SELinux或将其设置为许可模式。)
答案 1 :(得分:5)
我也在努力使用密钥验证。
无法打开授权密钥'/home/myUserName/.ssh/authorized_keys2':权限被拒绝
当我在这里结束时(谷歌的第一个链接),我已经检查了所有上述内容。 我意识到这是一个旧帖子,但我会在这里添加它以防其他人遇到与我相同的问题并最终在这里。
我拥有authorized_keys文件的所有者为“root”,因此更改为:
chown myUserName authorized_keys2
为我解决了。
答案 2 :(得分:4)
如果启用了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,那么就可以了。
答案 3 :(得分:1)
检查/ 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
...
答案 4 :(得分:0)
要检查几个想法:
答案 5 :(得分:0)
请勿在{{1}}上执行chmod
。尝试写出确切的路径:~/.ssh/...
,因为有时(使用/root/.ssh/...
等时),su
可能设置不正确。再次检查并发布权限,以获取完整路径,而无需在命令中使用~
。
一旦您完全确定权限正常,请检查您的~
是否实际在用户root下运行:sshd
。
答案 6 :(得分:0)
要仔细检查的几件事情:
cat -tv authorized_keys
。每行末尾有^M
个字符吗?在dos2unix
authorized_keys
答案 7 :(得分:0)
我遇到了同样的问题,并通过同时更改.ssh和authorized_keys的所有者来解决问题: chown MyUsername:Myusername .ssh chown MyUsername:Myusername .ssh / authorized_keys
感谢@niclaslindgren。
顺便说一下,无论是否在authorized_keys中都有^ M,我已经测试并证明了它,它适用于两种方式
答案 8 :(得分:0)
我也将在这里提出答案,因为我只是在浪费最后一个小时,试图弄清楚以前在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
答案 9 :(得分:0)
我已经花了好几个小时的废话了。阅读有关此问题的有启发性的解决方案的方法,这里归纳出他们都没有清楚说出的内容。
总结。
以下内容适用于这些文件:
~/.ssh/
~/.ssh/authorized_keys
所有者和组必须是登录用户。
组和公共权限必须设置为0
(无权限)。是! sshd
认为您默认是愚蠢的。
当然,必须允许所有者阅读。
注意。尽管未打开文件是一项应用程序策略,但诸如777
之类的权限也会给出与000
相同的错误。这可能是整个尝试过程中最大的困惑根源。
所有日志记录用户必须存在。通过记录用户,我们的意思是:
ssh root@localhost
~/.ssh/id_rsa.pub
在客户端中,而不在服务器中)答案 10 :(得分:0)
对我来说,这也改变了所有权,但不仅是 authorized_keys
的所有权,还有 .sdd
目录的也:
chown -R user:user ./home/user/.ssd
答案 11 :(得分:0)
就我而言,主目录在 NFS 上,这意味着 ~/.ssh 也在 NFS 上。
找到 this 答案,确实以 root 身份运行以下命令解决了问题:
setsebool -P use_nfs_home_dirs 1