解决非ROOT用户SSH免密码无法登陆的问题
最近打算使用Git来管理源码,因此安装了Gogs作为管理系统,装好后发现无法使用SSH方式。经排查发现是SSH无法免密码登陆,按免密码登陆流程设置好以后,发现 ssh git@git.domain.com
无法免密码,而 ssh root@git.domain.com
却是可以的
按照网上说的,把 /home/git
、/home/git/.ssh
及 /home/git/.ssh/authorized_keys
都重新设置了一遍权限,但问题依然存在
把/etc/ssh/sshd_config
里的日志级别改为debug后,查看日志文件:/var/log/secure
发现还是提示/home/git/.ssh/authorized_keys
的权限不足:
Mar 17 10:32:02 web12 sshd[18767]: debug1: trying public key file /home/git/.ssh/authorized_keys
Mar 17 10:32:02 web12 sshd[18767]: debug1: Could not open authorized keys '/home/git/.ssh/authorized_keys': Permission denied
后来,网上看到一篇类似问题的文章里有提到日志/var/log/audit/audit.log
于是在里面看到:
type=AVC msg=audit(1489721077.234:763): avc: denied { search } for pid=20284 comm="sshd" name="/" dev=sdb1 ino=2 scontext=unconfined_u:system_r:sshd_t:s0-s0:c0.c1023 tcontext=system_u:object_r:file_t:s0 tclass=dir
type=SYSCALL msg=audit(1489721077.234:763): arch=c000003e syscall=2 success=no exit=-13 a0=7fd8fb6d75e0 a1=800 a2=1 a3=4000 items=0 ppid=20059 pid=20284 auid=0 uid=0 gid=0 euid=501 suid=0 fsuid=501 egid=501 sgid=0 fsgid=501 tty=(none) ses=11 comm="sshd" exe="/usr/sbin/sshd" subj=unconfined_u:system_r:sshd_t:s0-s0:c0.c1023 key=(null)
type=AVC msg=audit(1489721077.234:764): avc: denied { search } for pid=20284 comm="sshd" name="/" dev=sdb1 ino=2 scontext=unconfined_u:system_r:sshd_t:s0-s0:c0.c1023 tcontext=system_u:object_r:file_t:s0 tclass=dir
结合文章的问题,突然想到,我的服务器的/home
目录也是外挂的另一个硬盘,那问题也就是出在 /home
目录的context上了
然后运行:
restorecon -r -vv /home/
完成后,SSH免密登陆成功