打开 SSH:身份验证被拒绝:文件所有权或模式错误

打开 SSH:身份验证被拒绝:文件所有权或模式错误

我正在尝试连接到运行 Open SSH 的 Dockerized 容器。authorized_keys文件指定为/home/jenkins/.ssh/authorized_keys。这是我的sshd_config

PermitRootLogin         yes

AuthorizedKeysFile      /home/jenkins/.ssh/authorized_keys

GatewayPorts            yes

Subsystem               sftp /usr/lib/ssh/sftp-server

PubkeyAuthentication    yes
LogLevel                DEBUG1

当尝试通过公钥连接时,我收到权限被拒绝的错误。以下是相应的日志:

2021-07-18 08:56:30.005308217  debug1: Set /proc/self/oom_score_adj from 0 to -1000
2021-07-18 08:56:30.005324377  debug1: Bind to port 2222 on 0.0.0.0.
2021-07-18 08:56:30.005325247  Server listening on 0.0.0.0 port 2222.
2021-07-18 08:56:30.005325987  debug1: Bind to port 2222 on ::.
2021-07-18 08:56:30.005329257  Server listening on :: port 2222.
2021-07-18 08:56:33.596858088  debug1: Forked child 324.
2021-07-18 08:56:33.596896278  debug1: Set /proc/self/oom_score_adj to 0
2021-07-18 08:56:33.596935527  debug1: rexec start in 5 out 5 newsock 5 pipe 7 sock 8
2021-07-18 08:56:33.600171495  debug1: inetd sockets after dupping: 4, 4
2021-07-18 08:56:33.600192115  Connection from 172.17.0.1 port 37096 on 172.17.0.5 port 2222 rdomain ""
2021-07-18 08:56:33.600216025  debug1: Local version string SSH-2.0-OpenSSH_8.6
2021-07-18 08:56:33.600235315  debug1: Remote protocol version 2.0, remote software version OpenSSH_8.4p1 Ubuntu-5ubuntu1
2021-07-18 08:56:33.600237005  debug1: compat_banner: match: OpenSSH_8.4p1 Ubuntu-5ubuntu1 pat OpenSSH* compat 0x04000000
2021-07-18 08:56:33.600704154  debug1: list_hostkey_types: rsa-sha2-512,rsa-sha2-256,ssh-rsa,ecdsa-sha2-nistp256,ssh-ed25519 [preauth]
2021-07-18 08:56:33.600717613  debug1: SSH2_MSG_KEXINIT sent [preauth]
2021-07-18 08:56:33.600749023  debug1: SSH2_MSG_KEXINIT received [preauth]
2021-07-18 08:56:33.600776023  debug1: kex: algorithm: curve25519-sha256 [preauth]
2021-07-18 08:56:33.600784683  debug1: kex: host key algorithm: ecdsa-sha2-nistp256 [preauth]
2021-07-18 08:56:33.600785563  debug1: kex: client->server cipher: [email protected] MAC: <implicit> compression: none [preauth]
2021-07-18 08:56:33.600786183  debug1: kex: server->client cipher: [email protected] MAC: <implicit> compression: none [preauth]
2021-07-18 08:56:33.600790363  debug1: expecting SSH2_MSG_KEX_ECDH_INIT [preauth]
2021-07-18 08:56:33.601919379  debug1: SSH2_MSG_KEX_ECDH_INIT received [preauth]
2021-07-18 08:56:33.604337860  debug1: rekey out after 134217728 blocks [preauth]
2021-07-18 08:56:33.604338530  debug1: SSH2_MSG_NEWKEYS sent [preauth]
2021-07-18 08:56:33.604338990  debug1: Sending SSH2_MSG_EXT_INFO [preauth]
2021-07-18 08:56:33.604343240  debug1: expecting SSH2_MSG_NEWKEYS [preauth]
2021-07-18 08:56:33.605814995  debug1: SSH2_MSG_NEWKEYS received [preauth]
2021-07-18 08:56:33.605815805  debug1: rekey in after 134217728 blocks [preauth]
2021-07-18 08:56:33.605819625  debug1: KEX done [preauth]
2021-07-18 08:56:33.606126884  debug1: userauth-request for user root service ssh-connection method none [preauth]
2021-07-18 08:56:33.606128154  debug1: attempt 0 failures 0 [preauth]
2021-07-18 08:56:33.614995123  debug1: userauth-request for user root service ssh-connection method publickey [preauth]
2021-07-18 08:56:33.614996263  debug1: attempt 1 failures 0 [preauth]
2021-07-18 08:56:33.615014533  debug1: userauth_pubkey: test pkalg rsa-sha2-512 pkblob RSA SHA256:Y3VcCaK/kPX8G4TgWRDvnm/kd0VjwjsWfDS4azoDtXo [preauth]
2021-07-18 08:56:33.615038512  debug1: temporarily_use_uid: 0/0 (e=1002/1002)
2021-07-18 08:56:33.615040852  debug1: trying public key file /home/jenkins/.ssh/authorized_keys
2021-07-18 08:56:33.615083762  debug1: fd 5 clearing O_NONBLOCK
2021-07-18 08:56:33.615110552  Authentication refused: bad ownership or modes for file /home/jenkins/.ssh/authorized_keys
2021-07-18 08:56:33.615110952  debug1: restore_uid: (unprivileged)
2021-07-18 08:56:33.615124002  Failed publickey for root from 172.17.0.1 port 37096 ssh2: RSA SHA256:Y3VcCaK/kPX8G4TgWRDvnm/kd0VjwjsWfDS4azoDtXo
2021-07-18 08:56:33.623841240  debug1: userauth-request for user root service ssh-connection method publickey [preauth]
2021-07-18 08:56:33.623842310  debug1: attempt 2 failures 1 [preauth]
2021-07-18 08:56:33.623871450  debug1: userauth_pubkey: test pkalg rsa-sha2-512 pkblob RSA SHA256:Y3VcCaK/kPX8G4TgWRDvnm/kd0VjwjsWfDS4azoDtXo [preauth]
2021-07-18 08:56:33.623895940  debug1: temporarily_use_uid: 0/0 (e=1002/1002)
2021-07-18 08:56:33.623896530  debug1: trying public key file /home/jenkins/.ssh/authorized_keys
2021-07-18 08:56:33.623905610  debug1: fd 5 clearing O_NONBLOCK
2021-07-18 08:56:33.623915180  Authentication refused: bad ownership or modes for file /home/jenkins/.ssh/authorized_keys
2021-07-18 08:56:33.623915720  debug1: restore_uid: (unprivileged)
2021-07-18 08:56:33.623926690  Failed publickey for root from 172.17.0.1 port 37096 ssh2: RSA SHA256:Y3VcCaK/kPX8G4TgWRDvnm/kd0VjwjsWfDS4UzoDtXo
2021-07-18 08:56:33.632847617  Connection closed by authenticating user root 172.17.0.1 port 37096 [preauth]
2021-07-18 08:56:33.632849117  debug1: do_cleanup [preauth]
2021-07-18 08:56:33.632905007  debug1: monitor_read_log: child log fd closed
2021-07-18 08:56:33.632909387  debug1: do_cleanup
2021-07-18 08:56:33.632909867  debug1: Killing privsep child 325

尤其是那行字Authentication refused: bad ownership or modes for file /home/jenkins/.ssh/authorized_keys似乎很引人注目。我似乎没有发现权限问题...

root@64c9c830e6e8:/# ls -ld /home/jenkins/
drwx------ 1 jenkins jenkins 4096 Jul 18 05:08 /home/jenkins/
root@64c9c830e6e8:/# ls -ld /home/jenkins/.ssh/
drwx------ 1 jenkins jenkins 4096 Jul 18 08:56 /home/jenkins/.ssh/
root@64c9c830e6e8:/# ls -ld /home/jenkins/.ssh/authorized_keys 
-rw------- 1 jenkins jenkins 0 Jul 18 08:56 /home/jenkins/.ssh/authorized_keys

流程所有者:

root@64c9c830e6e8:/# ps -u -p 308
USER         PID %CPU %MEM    VSZ   RSS TTY      STAT START   TIME COMMAND
jenkins      308  0.0  0.0   4556  3656 ?        Ss   08:56   0:00 sshd.pam: /usr/sbin/sshd.pam -D -e -p 2222 [listener] 0 of 
root@64c9c830e6e8:/# id jenkins
uid=1002(jenkins) gid=1002(jenkins) groups=1002(jenkins)

非常感谢您的帮助!

编辑 只有一个jenkins用户

root@64c9c830e6e8:/# cat /etc/passwd | grep jenkins
jenkins:x:1002:1002::/home/jenkins:/bin/ash

答案1

我很确定这是您的authorized_keys文件的权限:

对于密钥文件:chmod u=rw,og=r /home/jenkins/.ssh/authorized_keys

这使得该组 + 其他用户/系统生成的用户有机会阅读其内容。 - 希望这会有所帮助:)

相关内容