SSHD Gives error could not open Authorized Keys, although permissions seem correct
Solution 1
If the permissions are correct, SELinux might still be preventing sshd from opening the file.
Try fixing the labels inside the .ssh directory (and maybe $HOME):
restorecon -FRvv ~/.ssh
(I'm intentionally not suggesting disabling SELinux or setting it to the permissive mode.)
Solution 2
In case if SELinux enabled:
$ getenforce
Enforcing
to temporary enable pub-key ssl login to non-standard user home directory location run:
$ 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
See https://linux.die.net/man/8/ssh_selinux for the details.
To make SELinux settings permanent run:
$ 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/
You hit this if you are on modern RHEL, Oracle Linux, CentOS.
Solution 3
I was struggling to use key authentication as well.
Could not open authorized keys '/home/myUserName/.ssh/authorized_keys2': Permission denied
Had checked all the above things when I ended up here (first link on google). I realize that this is an old post but I will add it here in case somebody else has the same problem as me and end up here.
I had owner of the authorized_keys file to "root", so changing it with:
chown myUserName authorized_keys2
Solved it for me.
Solution 4
Check the /home directory permissions. It should be
drwxr-xr-x. 9 root root 113 Jun 28 22:57 home
and then your home directory detail:
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
My error messages in logs
/var/log/secure > sshd[22565]: error: Received disconnect from X.X.X.X: 14: No supported authentication methods available [preauth]
On client side
ssh [email protected]
Permission denied (publickey).
ssh -vvv [email protected]
...
debug2: we did not send a packet, disable method
debug1: No more authentication methods to try.
Permission denied (publickey).
On server side
service sshd stop
run sshd debug mode:
/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 ...
Solution 5
I've spent a good couple hours with this crap. Reading loads of illuminated solutions to the issue, here is a boil down of what none of them say clearly.
Sumarizing.
The following applies to these files:
~/.ssh/
~/.ssh/authorized_keys
Ownership
The owner and group must be the logging user.
Permissions
Group and public permissions must be set to 0
(no permissions). Yes! sshd
thinks you're stupid by default.
And of course, the owner must be allowed to read.
Note. Permissions such as 777
give the same error as 000
, even though it is an application policy to not open the file. This was probably the biggest source of confusion during this whole endeavour.
Users
All logging users must exist. By logging users we mean:
- The user sent with the url
ssh root@localhost
- Or, the user in the public key present on the client machine (
~/.ssh/id_rsa.pub
in the client, not in the server)
Comments
-
newUserNameHere almost 3 years
I'm unable to login to SSH because of the following error in /var/log/secure (according to the debug logs):
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
I have the following permissions set on root
chmod 700 ~/.ssh chmod 600 ~/.ssh/authorized_keys chmod go-wrx ~
ls -lah gives the following output for those directories:
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
I know the key I'm using is correct, as I just setup another server with it without any problems.
I'm running: CentOS release 6.4 (Final)
I've added my sshd config in case there's something misconfigured in there that might be causing the issue:
# $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
Any ideas would be much appreciated.
-
newUserNameHere over 10 years1. Yes, it looks correct, it has my key in there and that's it. I compared it to another server that I can ssh into without a password and it's exactly the same. 2. Added SSHD config to question above, that is enabled. 3. I'm not sure what you mean. When I login to the server remotely from my client pc I use "ssh root@hostname" and on the server right now I'm logged in as root making these changes.
-
newUserNameHere over 10 yearsThanks for the help. I'm logged in as root and reran those commands from inside the directory itself. No luck. And sshd is running as root.
-
newUserNameHere over 10 yearsThanks for your help. Yes, no bad characters, and yes.
-
Tomas over 10 years+1, especially for "I'm intentionally not suggesting to disable SELinux or set it to permissive mode :)" Sounds like you believe this is the best way (we are on the same page :)) but you already know there will be a lot of uptight "moralists" who would give you a lot of downvotes :-)
-
Daniel about 10 yearsI ran into the same issue. I guess I need to read up more on SELinux. Looks like a powerful but confusing tool?
-
Jonathan almost 9 yearsI had the same problem with the same cause, but
restorecon
didn't work. I had to change the SELinux type explicitly.chcon -Rv -t ssh_home_t ~/.ssh
-
Josh over 7 yearsJonathan's comment above is what I needed to do to solve my problem. I created the .ssh folder myself, so I'm thinking no security context was added to it (which is why "restorecon" didn't work...). I was using CentOS 7.
-
Glenn Maynard over 7 yearsWhen SELinux is causing problems despite my never having touched it (and breaking SSH is a serious problem), and when I'm in a sandboxed, single-purpose VM with no internet access, disabling SELinux is absolutely the right answer.
-
Mark Perryman over 7 yearsThis works when SELinux labels are
unconfined_u:object_r:unlabeled_t:s0
(seels -Z
). This answer resolved the problem, setting the SElinux labels tounconfined_u:object_r:ssh_home_t:s0
for.ssh/
andunconfined_u:object_r:user_home_dir_t:s0
for$HOME
. -
afrish about 7 yearsOn Ubuntu I had to install it first:
sudo apt-get install policycoreutils
-
Sobrique almost 7 years3 years later. This tripped me up. And this was the solution
-
sjethvani about 4 yearsThis helped me . In my case authorized_keys file was created by root/sudo user , hence it was showing 'root' as userowner & 'root' as groupowner . I changed it to my user & group using chown & chgrp command respectively , afterwards I was able to do ssh to server using my user.
-
Raja Khoury about 4 yearsspent an entire day on this problem and this is the only thing the worked for me on CentOS7 SELinux enabled. I dont understand the difference when you say temporary vs permanent.
-
gavenkoa about 4 yearsTemporary - lasts to next reboot. Permanent - survives reboot.
-
Raja Khoury about 4 yearsI feel this is going to be a long journey :)
-
Pezhvak over 3 yearshow on earth should somebody figure this out? thanks man, you saved my day!
-
keithpjolley over 2 yearsthis did the opposite of what i was hoping. i got ssh working with @gavenkoa's solution then tried this and ssh reverted to not not working. selinux trips over its own crank and the errors/documentation are inscrutable. </rant>
-
Jan Včelák about 2 yearsAvoid using
chcon
for non-standard home directory paths and rather adjusting the local configuration usingsemanage fcontext
. That way the correct labels will be set in case the whole filesystem need to be relabeled.