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

依然范特西╮ 提交于 2019-11-29 20:19:06

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

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.

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.

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

A couple ideas to check:

  • Can you cat authorized_keys? What does the file look like?
  • Is your sshd configured to allow root login? This is generally frowned upon,
  • Are you doing it as root or as a sudoer?
  1. Don't do chmod on ~/.ssh/.... Try to write the exact path: /root/.ssh/..., since sometimes (when using su etc), the ~ can be setup incorrectly. Check and post the permissions again for the full path without using ~ in the command.

  2. Once you are absolutely sure the permissions are OK, check if your sshd is actually running under user root: ps -A u | grep sshd.

A couple of things to double-check:

  1. Are you sure you copied the PUBLIC key to the authorized_keys, not the private key? :-)
  2. Do cat -tv authorized_keys. Any ^M characters at the end of each line? Do a dos2unix on authorized_keys
  3. Did you restart the ssh daemon after making configuration changes?

I encountered this same issue and got it solved by changing both .ssh and authorized_keys's owner at the same time: chown MyUsername:Myusername .ssh chown MyUsername:Myusername .ssh/authorized_keys

Thanks to @niclaslindgren.

And BTW, it's no matter with whether there is ^M in authorized_keys or not, I had tested and proved it, it works with both the ways

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!