跳到主要内容

记一次ssh免密登录问题:Permission denied

· 阅读需 4 分钟
故事的程序猿👨🏻‍💻
一个后端打酱油程序猿

先说结论:/root 目录的权限问题导致的 ssh 认证不通过!

‍ 问题经过:

我给自己的机器设置了 ssh 免密登录到一个开发服务器上,但是不知道做了何种操作突然不好使了,开始报错:

ssh 192.168.3.203

root@192.168.3.203: Permission denied (publickey,gssapi-keyex,gssapi-with-mic,password).

因为一直是好用的,突然不好使了,首选万能操作重启。结果无济于事!

然后开始查询各种解决办法!⤵

我重新生成了公钥,替换到服务器,结果还是报错!

修改服务端 ssh 的配置文件

vim /etc/ssh/sshd_config

以下的参数值:

大部分和网上修改的一致的(不然我以前肯定也不好使!)

PermitRootLogin yes
PasswordAuthentication yes
PasswordAuthentication yes

关闭 SELinux

sed -i 's/SELINUX=enforcing/SELINUX=disabled/' /etc/selinux/config

修改文件authorized_keys​ 权限

以上修改完毕后,依然报错。🙃

后来查询到客户端 ssh 命令可以查看运行日志:

 ssh -vvv 192.168.3.203

查询日志

ssh -vvv 192.168.3.203
OpenSSH_8.6p1, LibreSSL 3.3.6
debug1: Reading configuration data /Users/lichenghao/.ssh/config
debug1: /Users/lichenghao/.ssh/config line 30: Applying options for 192.168.3.203
debug1: Reading configuration data /etc/ssh/ssh_config
debug1: /etc/ssh/ssh_config line 21: include /etc/ssh/ssh_config.d/* matched no files
debug1: /etc/ssh/ssh_config line 54: Applying options for *
debug1: /etc/ssh/ssh_config line 58: Applying options for *
debug2: resolve_canonicalize: hostname 192.168.3.203 is address
debug3: expanded UserKnownHostsFile '~/.ssh/known_hosts' -> '/Users/lichenghao/.ssh/known_hosts'
debug3: expanded UserKnownHostsFile '~/.ssh/known_hosts2' -> '/Users/lichenghao/.ssh/known_hosts2'
debug1: Authenticator provider $SSH_SK_PROVIDER did not resolve; disabling
debug3: ssh_connect_direct: entering
debug1: Connecting to 192.168.3.203 [192.168.3.203] port 22.
debug3: set_sock_tos: set socket 3 IP_TOS 0x48
debug1: Connection established.
debug1: identity file /Users/lichenghao/.ssh/203-rsa type 0
debug1: identity file /Users/lichenghao/.ssh/203-rsa-cert type -1
debug1: Local version string SSH-2.0-OpenSSH_8.6
debug1: Remote protocol version 2.0, remote software version OpenSSH_7.4
debug1: compat_banner: match: OpenSSH_7.4 pat OpenSSH_7.0*,OpenSSH_7.1*,OpenSSH_7.2*,OpenSSH_7.3*,OpenSSH_7.4*,OpenSSH_7.5*,OpenSSH_7.6*,OpenSSH_7.7* compat 0x04000002
debug2: fd 3 setting O_NONBLOCK
debug1: Authenticating to 192.168.3.203:22 as 'root'
debug3: record_hostkey: found key type ED25519 in file /Users/lichenghao/.ssh/known_hosts:1
debug3: load_hostkeys_file: loaded 1 keys from 192.168.3.203
debug1: load_hostkeys: fopen /Users/lichenghao/.ssh/known_hosts2: No such file or directory
debug1: load_hostkeys: fopen /etc/ssh/ssh_known_hosts: No such file or directory
debug1: load_hostkeys: fopen /etc/ssh/ssh_known_hosts2: No such file or directory
debug3: order_hostkeyalgs: have matching best-preference key type ssh-ed25519-cert-v01@openssh.com, using HostkeyAlgorithms verbatim
debug3: send packet: type 20
debug1: SSH2_MSG_KEXINIT sent
debug3: receive packet: type 20
debug1: SSH2_MSG_KEXINIT received
debug2: local client KEXINIT proposal
debug2: KEX algorithms: curve25519-sha256,curve25519-sha256@libssh.org,ecdh-sha2-nistp256,ecdh-sha2-nistp384,ecdh-sha2-nistp521,diffie-hellman-group-exchange-sha256,diffie-hellman-group16-sha512,diffie-hellman-group18-sha512,diffie-hellman-group14-sha256,ext-info-c
debug2: host key algorithms: ssh-ed25519-cert-v01@openssh.com,ecdsa-sha2-nistp256-cert-v01@openssh.com,ecdsa-sha2-nistp384-cert-v01@openssh.com,ecdsa-sha2-nistp521-cert-v01@openssh.com,sk-ssh-ed25519-cert-v01@openssh.com,sk-ecdsa-sha2-nistp256-cert-v01@openssh.com,rsa-sha2-512-cert-v01@openssh.com,rsa-sha2-256-cert-v01@openssh.com,ssh-rsa-cert-v01@openssh.com,ssh-ed25519,ecdsa-sha2-nistp256,ecdsa-sha2-nistp384,ecdsa-sha2-nistp521,sk-ssh-ed25519@openssh.com,sk-ecdsa-sha2-nistp256@openssh.com,rsa-sha2-512,rsa-sha2-256,ssh-rsa
debug2: ciphers ctos: chacha20-poly1305@openssh.com,aes128-ctr,aes192-ctr,aes256-ctr,aes128-gcm@openssh.com,aes256-gcm@openssh.com
debug2: ciphers stoc: chacha20-poly1305@openssh.com,aes128-ctr,aes192-ctr,aes256-ctr,aes128-gcm@openssh.com,aes256-gcm@openssh.com
debug2: MACs ctos: umac-64-etm@openssh.com,umac-128-etm@openssh.com,hmac-sha2-256-etm@openssh.com,hmac-sha2-512-etm@openssh.com,hmac-sha1-etm@openssh.com,umac-64@openssh.com,umac-128@openssh.com,hmac-sha2-256,hmac-sha2-512,hmac-sha1
debug2: MACs stoc: umac-64-etm@openssh.com,umac-128-etm@openssh.com,hmac-sha2-256-etm@openssh.com,hmac-sha2-512-etm@openssh.com,hmac-sha1-etm@openssh.com,umac-64@openssh.com,umac-128@openssh.com,hmac-sha2-256,hmac-sha2-512,hmac-sha1
debug2: compression ctos: none,zlib@openssh.com,zlib
debug2: compression stoc: none,zlib@openssh.com,zlib
debug2: languages ctos:
debug2: languages stoc:
debug2: first_kex_follows 0
debug2: reserved 0
debug2: peer server KEXINIT proposal
debug2: KEX algorithms: curve25519-sha256,curve25519-sha256@libssh.org,ecdh-sha2-nistp256,ecdh-sha2-nistp384,ecdh-sha2-nistp521,diffie-hellman-group-exchange-sha256,diffie-hellman-group16-sha512,diffie-hellman-group18-sha512,diffie-hellman-group-exchange-sha1,diffie-hellman-group14-sha256,diffie-hellman-group14-sha1,diffie-hellman-group1-sha1
debug2: host key algorithms: ssh-rsa,rsa-sha2-512,rsa-sha2-256,ecdsa-sha2-nistp256,ssh-ed25519
debug2: ciphers ctos: chacha20-poly1305@openssh.com,aes128-ctr,aes192-ctr,aes256-ctr,aes128-gcm@openssh.com,aes256-gcm@openssh.com,aes128-cbc,aes192-cbc,aes256-cbc,blowfish-cbc,cast128-cbc,3des-cbc
debug2: ciphers stoc: chacha20-poly1305@openssh.com,aes128-ctr,aes192-ctr,aes256-ctr,aes128-gcm@openssh.com,aes256-gcm@openssh.com,aes128-cbc,aes192-cbc,aes256-cbc,blowfish-cbc,cast128-cbc,3des-cbc
debug2: MACs ctos: umac-64-etm@openssh.com,umac-128-etm@openssh.com,hmac-sha2-256-etm@openssh.com,hmac-sha2-512-etm@openssh.com,hmac-sha1-etm@openssh.com,umac-64@openssh.com,umac-128@openssh.com,hmac-sha2-256,hmac-sha2-512,hmac-sha1
debug2: MACs stoc: umac-64-etm@openssh.com,umac-128-etm@openssh.com,hmac-sha2-256-etm@openssh.com,hmac-sha2-512-etm@openssh.com,hmac-sha1-etm@openssh.com,umac-64@openssh.com,umac-128@openssh.com,hmac-sha2-256,hmac-sha2-512,hmac-sha1
debug2: compression ctos: none,zlib@openssh.com
debug2: compression stoc: none,zlib@openssh.com
debug2: languages ctos:
debug2: languages stoc:
debug2: first_kex_follows 0
debug2: reserved 0
debug1: kex: algorithm: curve25519-sha256
debug1: kex: host key algorithm: ssh-ed25519
debug1: kex: server->client cipher: chacha20-poly1305@openssh.com MAC: <implicit> compression: none
debug1: kex: client->server cipher: chacha20-poly1305@openssh.com MAC: <implicit> compression: none
debug3: send packet: type 30
debug1: expecting SSH2_MSG_KEX_ECDH_REPLY
debug3: receive packet: type 31
debug1: SSH2_MSG_KEX_ECDH_REPLY received
debug1: Server host key: ssh-ed25519 SHA256:N1fHmTiSGTlsshl2kYqmZ2D8IAiNs32oeMZcmVb2sTc
debug3: record_hostkey: found key type ED25519 in file /Users/lichenghao/.ssh/known_hosts:1
debug3: load_hostkeys_file: loaded 1 keys from 192.168.3.203
debug1: load_hostkeys: fopen /Users/lichenghao/.ssh/known_hosts2: No such file or directory
debug1: load_hostkeys: fopen /etc/ssh/ssh_known_hosts: No such file or directory
debug1: load_hostkeys: fopen /etc/ssh/ssh_known_hosts2: No such file or directory
debug1: Host '192.168.3.203' is known and matches the ED25519 host key.
debug1: Found key in /Users/lichenghao/.ssh/known_hosts:1
debug3: send packet: type 21
debug2: set_newkeys: mode 1
debug1: rekey out after 134217728 blocks
debug1: SSH2_MSG_NEWKEYS sent
debug1: expecting SSH2_MSG_NEWKEYS
debug3: receive packet: type 21
debug1: SSH2_MSG_NEWKEYS received
debug2: set_newkeys: mode 0
debug1: rekey in after 134217728 blocks
debug1: Will attempt key: /Users/lichenghao/.ssh/203-rsa RSA SHA256:ZXjJ7aAlW5YXf4504U/Xfdj9yM7bJiUoGzbE9mKqrGw explicit
debug2: pubkey_prepare: done
debug3: send packet: type 5
debug3: receive packet: type 7
debug1: SSH2_MSG_EXT_INFO received
debug1: kex_input_ext_info: server-sig-algs=<rsa-sha2-256,rsa-sha2-512>
debug3: receive packet: type 6
debug2: service_accept: ssh-userauth
debug1: SSH2_MSG_SERVICE_ACCEPT received
debug3: send packet: type 50
debug3: receive packet: type 51
debug1: Authentications that can continue: publickey,password
debug3: start over, passed a different list publickey,password
debug3: preferred publickey
debug3: authmethod_lookup publickey
debug3: remaining preferred:
debug3: authmethod_is_enabled publickey
debug1: Next authentication method: publickey
debug1: Offering public key: /Users/lichenghao/.ssh/203-rsa RSA SHA256:ZXjJ7aAlW5YXf4504U/Xfdj9yM7bJiUoGzbE9mKqrGw explicit
debug3: send packet: type 50
debug2: we sent a publickey packet, wait for reply
debug3: receive packet: type 51
debug1: Authentications that can continue: publickey,password
debug2: we did not send a packet, disable method
debug1: No more authentication methods to try.
root@192.168.3.203: Permission denied (publickey,password).

好像也没有看出什么端倪,还是认证被拒绝。🤔

最终没解决,就去请教了一下运维的同事,过了一会让我测试下就好了,说重置了下ownership​ ,就可以了!

我自己随后又查询了一下:

客户端看不到什么问题,那么就去服务端看下 ssh 日志。

对于 CentOS 系统日志位于/var/log/secure​,终于发现了点端倪,日志如下:

sudo tail -n 1000 /var/log/secure
Sep  5 10:39:41 dev_perf_3 sshd[14171]: Authentication refused: bad ownership or modes for directory /root
Sep 5 10:39:41 dev_perf_3 sshd[14171]: Connection closed by 10.99.98.122 port 50061 [preauth]

说明/root​ 目录权限有问题!!!

修改目录权限结果如下:

drwx------.  22 root root 4096 95 10:48 root

执行如下操作:重置权限

chown root:root /root/
chmod 700 /root/

然后竟然可以了!


我在网上搜了好久,等我解决完毕了,又随便搜了一下,这才搜索到相关内容比如:

ssh 免密登录失败原因之 root 目录从属错误

应该是我搜索的关键词有问题,一直没有定位到准确的问题。

文章标题:记一次ssh免密登录问题:Permission denied
版权声明:内容遵守
许可协议。转载请注明出处!
侵权提示:部分信息可能来源于网络。如发现有侵权,请随时联系删除!

相关推荐

2分钟在Mac跑个Kafka服务

2分钟在Mac跑个Kafka服务

我一直都是在 centos 服务器上使用 kafka,或者在 k8s 上使用 kafka。 今天 2 分钟在 Mac 跑个 Kafka 服务。

Diy万能博客模板js脚本【静态博客】

Diy万能博客模板js脚本【静态博客】

静态博客工具,一般都使用Markdown文件。那么Front-Matter这些重复的内容就可以使用工具来生成,大部分静态博客工具都支持插件来生成博客文件。

Nacos2.3.2开启服务端用户认证的坑

Nacos2.3.2开启服务端用户认证的坑

最近升级Nacos服务端到版本2.3.2。但是在开启服务端用户认证的时候出现了问题。

Docusaurus 静态博客系列(1)- 系统搭建

Docusaurus 静态博客系列(1)- 系统搭建

这里介绍使用 Docusaurus ,它是一个用于构建开源项目网站的静态网站生成器。它由 Facebook 开发,旨在简化创建、维护和发布技术文档和项目文档的过程。


神评论