ssh key 一模一样的设置,另一个就不能 登录是什么原因?

2021-12-07 23:15:30 +08:00
 selfcreditgiving

要用 user1 用户 ssh 登录两台服务器。

两个服务器,拷贝了相同的公钥:
/home/user1/.ssh/id_rsa.pub
和 /home/user1/.ssh/authorized_keys ( id_rsa.pub 拷进来的)
两个文件的权限都是 700

/home/user1 文件夹的权限是 600

id_rsa.pub 、authorized_keys 、/home/user1 的所属用户和所属组都是 user1

/etc/ssh/sshd_config 设置的也是一模一样的, 也重新启动了 sshd service 。

为什么一台可以登录,一台就登录不了呢?
提示这个错误:

Permission denied (publickey).

每次都被这种东西卡住了,

/etc/ssh/sshd_config 配置都是这样的:

# Package generated configuration file
# See the sshd_config(5) manpage for details

# What ports, IPs and protocols we listen for
Port 22
# Use these options to restrict which interfaces/protocols sshd will bind to
#ListenAddress ::
#ListenAddress 0.0.0.0
Protocol 2
# HostKeys for protocol version 2
HostKey /etc/ssh/ssh_host_rsa_key
HostKey /etc/ssh/ssh_host_dsa_key
HostKey /etc/ssh/ssh_host_ecdsa_key
HostKey /etc/ssh/ssh_host_ed25519_key
#Privilege Separation is turned on for security
UsePrivilegeSeparation no

# Lifetime and size of ephemeral version 1 server key
KeyRegenerationInterval 3600
ServerKeyBits 1024

# Logging
SyslogFacility AUTH
LogLevel INFO

# Authentication:
LoginGraceTime 120
PermitRootLogin no
StrictModes yes

RSAAuthentication yes
PubkeyAuthentication yes
#AuthorizedKeysFile     %h/.ssh/authorized_keys

# Don't read the user's ~/.rhosts and ~/.shosts files
IgnoreRhosts yes
# For this to work you will also need host keys in /etc/ssh_known_hosts
RhostsRSAAuthentication no
# similar for protocol version 2
HostbasedAuthentication no
# Uncomment if you don't trust ~/.ssh/known_hosts for RhostsRSAAuthentication
#IgnoreUserKnownHosts yes

# To enable empty passwords, change to yes (NOT RECOMMENDED)
PermitEmptyPasswords no

# Change to yes to enable challenge-response passwords (beware issues with
# some PAM modules and threads)
ChallengeResponseAuthentication no

# Change to no to disable tunnelled clear text passwords
PasswordAuthentication no

# Kerberos options
#KerberosAuthentication no
#KerberosGetAFSToken no
#KerberosOrLocalPasswd yes
#KerberosTicketCleanup yes

# GSSAPI options
#GSSAPIAuthentication no
#GSSAPICleanupCredentials yes

X11Forwarding yes
X11DisplayOffset 10
PrintMotd no
PrintLastLog yes
TCPKeepAlive yes
#UseLogin no

#MaxStartups 10:30:60
#Banner /etc/issue.net

# Allow client to pass locale environment variables
AcceptEnv LANG LC_*

Subsystem sftp /usr/lib/openssh/sftp-server

# Set this to 'yes' to enable PAM authentication, account processing,
# and session processing. If this is enabled, PAM authentication will
# be allowed through the ChallengeResponseAuthentication and
# PasswordAuthentication.  Depending on your PAM configuration,
# PAM authentication via ChallengeResponseAuthentication may bypass
# the setting of "PermitRootLogin yes
# If you just want the PAM account and session checks to run without
# PAM authentication, then enable this but set PasswordAuthentication
# and ChallengeResponseAuthentication to 'no'.
UsePAM no                                                                                  
4076 次点击
所在节点    问与答
29 条回复
shangyu7
2021-12-08 10:20:48 +08:00
看看 ssh 版本
pixelcn
2021-12-08 10:40:37 +08:00
一台是 RPM 系的吧?有 bug
PubkeyAcceptedKeyTypes 加上 rsa-sha2-512,rsa-sha2-256
ziseyinzi
2021-12-08 10:42:41 +08:00
应该是 17 楼说的,openssh 版本问题。新版本 openssh 因为安全原因会拒绝 rsa 算法的 key 。升级服务端的 openssh 版本或者在本地像 17 楼说的那样添加允许规则。
ysn2233
2021-12-08 11:27:51 +08:00
高版本的 openssh 加密算法换了,要配置一下
selfcreditgiving
2021-12-09 15:45:08 +08:00
抱歉,之前有事情现在才更新。

这两台服务器 是这样的,都是用 docker 起的 ubuntu 镜像容器,而且镜像是一模一样的。开放出了 ssh 端口。

这是两台服务器的 openSSH 版本 ssh -V

服务器 A 和 服务器 B 的结果都是一样的:
OpenSSH_7.2p2 Ubuntu-4ubuntu2.10, OpenSSL 1.0.2g 1 Mar 2016

本身操作用的 macbook pro 的 ssh 版本:
OpenSSH_7.4p1, LibreSSL 2.5.0

/etc/ssh/sshd_config 两台服务器也是一模一样的。

@pixelcn @ziseyinzi @ysn2233 在登录失败的 服务器 B 设置了 /etc/ssh/sshd_config ,添加 PubkeyAcceptedKeyTypes rsa-sha2-512,rsa-sha2-256 ,并重启容器(因为 docker 容器里不能 用 service restart )
结果还是一样的。

感觉我还是设一个强密码,然后设一个 fail2ban ,可不知道 fail2ban 有没有什么坑,可以设置好就不用管了不?
selfcreditgiving
2021-12-10 11:15:30 +08:00
@Osk
@dddd1919
@shangyu7
@pixelcn
@ziseyinzi
@ysn2233 发现原因了,又是自己的粗心大意浪费了大量的时间。

应该是 ~/.ssh 设置为权限 700
~/.ssh/authorized_keys 设置为 600

我设置反了! 想当然 ~/.ssh/authorized_keys 是文件需要 执行权限, ~/.ssh 是文件夹 只要 读写权限就行了。

后面搜到才发现弄反了。

server A (成功)是因为 , 没有手动去改 chomd 权限, ~/.ssh/authorized_keys 默认是 644 的权限。


经常会这样自己搞死自己,浪费大量的时间、熬夜,因为那一块是确认过了的,认为那里肯定是没有问题的,就没想到再去确认一次。大家有没有同样的经历?或者如果规避自己的低级错误导致卡壳的情况?
selfcreditgiving
2021-12-10 11:19:15 +08:00
目前找到一个 便利 和 安全 之间相对折中的方法, 就是:所有的服务器都用同一个 ssh 私钥。ssh 私钥设置 passphrase ,并且 passphrase 保存到 macOS 的 keychain (使用 ssh-add -K keyfile )
ynyounuo
2021-12-21 11:50:50 +08:00
@selfcreditgiving
#13 我提到过这个问题,如果你当时有查看的话会省很多时间
selfcreditgiving
2021-12-21 12:00:21 +08:00
@ynyounuo #28 是的,自己太粗心了,认为这个方面是自己已经知道的,就没有仔细去看是应该文件夹设置 700 。

这是一个专为移动设备优化的页面(即为了让你能够在 Google 搜索结果里秒开这个页面),如果你希望参与 V2EX 社区的讨论,你可以继续到 V2EX 上打开本讨论主题的完整版本。

https://www.v2ex.com/t/820740

V2EX 是创意工作者们的社区,是一个分享自己正在做的有趣事物、交流想法,可以遇见新朋友甚至新机会的地方。

V2EX is a community of developers, designers and creative people.

© 2021 V2EX