SSHD Gives error could not open Authorized Keys, although permissions seem correct

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.)


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.


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.