Linux服务器配置SSH免密码登录后,登录仍提示输入密码(一次真实的问题排查解决记录)

这篇具有很好参考价值的文章主要介绍了Linux服务器配置SSH免密码登录后,登录仍提示输入密码(一次真实的问题排查解决记录)。希望对大家有所帮助。如果存在错误或未考虑完全的地方,请大家不吝赐教,您也可以点击"举报违法"按钮提交疑问。

我们知道两台Linux服务器机器之间如果使用ssh命令登录或scp/rsync命令传输文件每一次都需要输入用户名相对应的密码,如果要免密码,则需要对两台Linux服务器机器之间进行SSH互信。

一.SSH介绍

1.SSH互信原理

虽然这是废话,也希望大家了解一下。

SSH(Secure Shell)是一种安全的传输协议,它可以让Linux系统中的服务器和客户端之间进行安全可靠的通讯,它常被用于在本地网络中的多台计算机之间实现远程登录,文件传输和系统管理。

SSH使用“加密”的传输方式,以保证客户端和服务器之间的通讯安全。具体而言,SSH使用加密技术(默认加密技术:rsa,加密位:2048位)将用户数据和控制指令加密,以保护数据不被第三方拦截。
SSH可以验证客户端的身份,确保只有授权的用户才能访问服务器。

要在Linux系统中运行SSH,需要两个软件:一个是服务器端的软件,另一个是客户端的软件。服务器端的软件叫做OpenSSH-Server,主要实现SSH服务器功能。它可以处理SSH消息并执行用户指令,以实现远程登录功能。客户端的软件叫做SSH-Client,会根据服务器端提供的信息将用户指令加密,并且可以认证服务器的身份。

2.SSH RPM包

OpenSSH所对应的RPM包共有5个,

[root@rhel77 ~]# ls /mnt/Packages/openssh*
/mnt/Packages/openssh-7.4p1-21.el7.x86_64.rpm
/mnt/Packages/openssh-askpass-7.4p1-21.el7.x86_64.rpm
/mnt/Packages/openssh-clients-7.4p1-21.el7.x86_64.rpm
/mnt/Packages/openssh-keycat-7.4p1-21.el7.x86_64.rpm
/mnt/Packages/openssh-server-7.4p1-21.el7.x86_64.rpm

说明如下:

OpenSSH-RPM Packages
Packages名 说明
openssh-7.4p1-21.el7.x86_64.rpm openssh核心文件
openssh-askpass-7.4p1-21.el7.x86_64.rpm ⽀持对话框窗⼝ 显示 X系统
openssh-clients-7.4p1-21.el7.x86_64.rpm 客户端软件包
openssh-keycat-7.4p1-21.el7.x86_64.rpm openssh公钥,私钥文件
openssh-server-7.4p1-21.el7.x86_64.rpm 服务器端软件包

3. SSH秘钥文件介绍

目录路径:/root/.ssh/

[root@rhel77 .ssh]# cd ../.ssh/
[root@rhel77 .ssh]# pwd
/root/.ssh
[root@rhel77 .ssh]# ls -la
total 28
drwx------   2 root root   80 Jun  8 15:32 .
drwxrwxrwx. 17 root root 8192 Jun  9 08:33 ..
-rw-------   1 root root  395 Jun  8 15:32 authorized_keys
-rw-------   1 root root 1675 Jun  8 15:18 id_rsa
-rw-r--r--   1 root root  393 Jun  8 15:18 id_rsa.pub
-rw-r--r--   1 root root  346 Jun  8 15:31 known_hosts
[root@rhel77 .ssh]# 

其中:

id_rsa:私钥,相当于"锁"。文件权限:600,不能更改。

id_rsa.pub:公钥,相当于"钥匙"。文件权限:644,不能更改。

authorized_keys:认证文件,记录"别人"(即:对端)给你的公钥“钥匙”。文件权限:600,不能更改。

known_hosts:“指纹”文件,记录首次SSH互信认证"别人"(即:对端)留给你的“指纹”信息。文件权限:600,不能更改。

4.ssh配置文件sshd_config

目录路径:/etc/ssh

sshd日志默认保存在/var/log/secure中

(cat /etc/ssh/sshd_config):

SyslogFacility AUTHPRIV

(cat /etc/rsyslog.conf):

authpriv.*                                              /var/log/secure

二.问题重现

1.环境信息

VMware CentOS7.9(IP:192.168.10.135)、RHEL7.7(IP:192.168.10.110)

防火墙及selinux关闭,参考(Chapter1):

Linux常规基础配置_小黑要上天的博客-CSDN博客

2.两台机器实现openssh rpm安装

-->RHEL7.7(ip:192.168.10.110)机器

[root@rhel77 /]# yum install -y openssh*
Loaded plugins: langpacks, product-id, search-disabled-repos, subscription-manager
This system is not registered with an entitlement server. You can use subscription-manager to register.
rhel7.7                                                                             | 2.8 kB  00:00:00     
Package openssh-server-7.4p1-21.el7.x86_64 already installed and latest version
Package openssh-7.4p1-21.el7.x86_64 already installed and latest version
Package openssh-clients-7.4p1-21.el7.x86_64 already installed and latest version
Package openssh-askpass-7.4p1-21.el7.x86_64 already installed and latest version
Package openssh-keycat-7.4p1-21.el7.x86_64 already installed and latest version
Nothing to do
[root@rhel77 /]# 

-->CentOS7.9(ip:192.168.10.135)机器

[root@centos79 ~]# yum install -y openssh*
已加载插件:fastestmirror
Loading mirror speeds from cached hostfile
 * base: mirrors.bupt.edu.cn
 * extras: mirror.lzu.edu.cn
 * updates: mirror.lzu.edu.cn
base                                                                                | 3.6 kB  00:00:00     
docker-ce-stable                                                                    | 3.5 kB  00:00:00     
extras                                                                              | 2.9 kB  00:00:00     
updates                                                                             | 2.9 kB  00:00:00     
软件包 openssh-cavs-7.4p1-22.el7_9.x86_64 已安装并且是最新版本
软件包 openssh-server-7.4p1-22.el7_9.x86_64 已安装并且是最新版本
软件包 openssh-7.4p1-22.el7_9.x86_64 已安装并且是最新版本
软件包 openssh-clients-7.4p1-22.el7_9.x86_64 已安装并且是最新版本
软件包 openssh-askpass-7.4p1-22.el7_9.x86_64 已安装并且是最新版本
软件包 openssh-ldap-7.4p1-22.el7_9.x86_64 已安装并且是最新版本
软件包 openssh-server-sysvinit-7.4p1-22.el7_9.x86_64 已安装并且是最新版本
软件包 openssh-keycat-7.4p1-22.el7_9.x86_64 已安装并且是最新版本
无须任何处理
[root@centos79 ~]# 

3.两台机器机器实现ssh互信

-->RHEL7.7(ip:192.168.10.110)机器

命令:

cd ~

ssh-keygen

cd .ssh/

ls

ssh-copy-id 192.168.10.135

[root@rhel77 ~]# ssh-keygen
Generating public/private rsa key pair.
Enter file in which to save the key (/root/.ssh/id_rsa): #强烈建议直接回车使用默认路径
/root/.ssh/id_rsa already exists.
Overwrite (y/n)? y
Enter passphrase (empty for no passphrase): #密钥的密码短语(建议留空则直接回车)
Enter same passphrase again:            #密钥的密码短语确认(建议留空则直接回车)
Your identification has been saved in /root/.ssh/id_rsa.
Your public key has been saved in /root/.ssh/id_rsa.pub.
The key fingerprint is:
SHA256:jHTGpurRdAzUvx4haQJJRFR5bZiS3j5TNyuB85/SXWc root@rhel77
The key's randomart image is:
+---[RSA 2048]----+
|   ==oo+ +       |
|    o.+.= o      |
|     oo+==       |
|     .oXB = o    |
|      ++S= = o   |
|     + .+ = .   E|
|    o .  + = o o.|
|   . .    o + .  |
|    .      .     |
+----[SHA256]-----+
[root@rhel77 ~]# cd .ssh/
[root@rhel77 .ssh]# ls
id_rsa  id_rsa.pub  known_hosts
[root@rhel77 .ssh]# ssh-copy-id 192.168.10.135
/usr/bin/ssh-copy-id: INFO: Source of key(s) to be installed: "/root/.ssh/id_rsa.pub"
/usr/bin/ssh-copy-id: INFO: attempting to log in with the new key(s), to filter out any that are already installed
/usr/bin/ssh-copy-id: INFO: 1 key(s) remain to be installed -- if you are prompted now it is to install the new keys
root@192.168.10.135's password: 

Number of key(s) added: 1

Now try logging into the machine, with:   "ssh '192.168.10.135'"
and check to make sure that only the key(s) you wanted were added.

[root@rhel77 .ssh]# 

-->CentOS7.9(ip:192.168.10.135)机器

命令:

cd ~

ssh-keygen

cd .ssh/

ls

ssh-copy-id 192.168.10.110

[root@centos79 ~]# ssh-keygen
Generating public/private rsa key pair.
Enter file in which to save the key (/root/.ssh/id_rsa): #同上
Enter passphrase (empty for no passphrase): #同上
Enter same passphrase again: #同上
Your identification has been saved in /root/.ssh/id_rsa.
Your public key has been saved in /root/.ssh/id_rsa.pub.
The key fingerprint is:
SHA256:nK6khtCnoJB2o1aVfqVlTNpJHMug4QQ/3orcPqAgda4 root@centos79
The key's randomart image is:
+---[RSA 2048]----+
|   ..o ....      |
|    + o o+.      |
|     =. *o.      |
|  . oooo O       |
| + oo. .S        |
|B.o==..+         |
|*o=Booo .        |
|.+E o+ .         |
|.  ...o          |
+----[SHA256]-----+
[root@centos79 ~]# cd .ssh/
[root@centos79 .ssh]# ls
authorized_keys  id_rsa  id_rsa.pub  known_hosts
[root@centos79 .ssh]# ssh-copy-id 192.168.10.110
/usr/bin/ssh-copy-id: INFO: Source of key(s) to be installed: "/root/.ssh/id_rsa.pub"
/usr/bin/ssh-copy-id: INFO: attempting to log in with the new key(s), to filter out any that are already installed
/usr/bin/ssh-copy-id: INFO: 1 key(s) remain to be installed -- if you are prompted now it is to install the new keys
root@192.168.10.110's password: 

Number of key(s) added: 1

Now try logging into the machine, with:   "ssh '192.168.10.110'"
and check to make sure that only the key(s) you wanted were added.

[root@centos79 .ssh]# 

4.ssh互信验证-问题重现

-->从RHEL7.7(ip:192.168.10.110)机器 ssh 到 CentOS7.9(ip:192.168.10.135)机器

ssh免密设置后仍然需要密码,linux小常识,服务器,linux,ssh

-->从CentOS7.9(ip:192.168.10.135)机器 ssh 到 RHEL7.7(ip:192.168.10.110)机器

ssh免密设置后仍然需要密码,linux小常识,服务器,linux,ssh

三. 问题解决梳理

1.两台机器文件权限验证(id_rsa,id_rsa.pub,authorized_keys,known_hosts)

-->RHEL7.7(ip:192.168.10.110)机器

[root@rhel77 /]# cd 
[root@rhel77 ~]# cd .ssh/
[root@rhel77 .ssh]# pwd
/root/.ssh
[root@rhel77 .ssh]# ls -l
total 16
-rw------- 1 root root  395 Jun  9 09:26 authorized_keys
-rw------- 1 root root 1679 Jun  9 09:26 id_rsa
-rw-r--r-- 1 root root  393 Jun  9 09:26 id_rsa.pub
-rw-r--r-- 1 root root  176 Jun  9 09:27 known_hosts
[root@rhel77 .ssh]# 

结论:文件权限无误

-->CentOS7.9(ip:192.168.10.135)机器

[root@centos79 .ssh]# cd
[root@centos79 ~]# cd .ssh/
[root@centos79 .ssh]# pwd
/root/.ssh
[root@centos79 .ssh]# ls -l
总用量 16
-rw------- 1 root root  393 6月   9 09:27 authorized_keys
-rw------- 1 root root 1679 6月   9 09:23 id_rsa
-rw-r--r-- 1 root root  395 6月   9 09:23 id_rsa.pub
-rw-r--r-- 1 root root  176 6月   9 09:26 known_hosts
[root@centos79 .ssh]# 

结论:文件权限无误

2.两台机器.ssh目录权限验证

-->RHEL7.7(ip:192.168.10.110)机器

[root@rhel77 ~]# pwd
/root
[root@rhel77 ~]# ls -ld .ssh/
drwx------ 2 root root 80 Jun  9 09:27 .ssh/
[root@rhel77 ~]# 

结论:.ssh目录权限为700,权限无误

-->CentOS7.9(ip:192.168.10.135)机器

[root@centos79 ~]# pwd
/root
[root@centos79 ~]# ls -ld .ssh/
drwx------ 2 root root 80 6月   9 09:27 .ssh/
[root@centos79 ~]# 

结论:.ssh目录权限为700,权限无误

3.两台机器更改/etc/ssh/sshd_config文件配置

添加如下信息:

RSAAuthentication yes               #允许RSA密钥
PubkeyAuthentication yes          #启用公告密钥配对认证方式

################################################

添加位置:

RSAAuthentication yes
PubkeyAuthentication yes

# The default is to check both .ssh/authorized_keys and .ssh/authorized_keys2
# but this is overridden so installations will only check .ssh/authorized_keys
AuthorizedKeysFile      .ssh/authorized_keys

################################################

重启sshd,发现问题仍旧存在。

命令:

systemctl restart sshd

systemctl status sshd

4.问题点定位

最后,通过查看/var/log/secure,发现了问题的点

命令:

tail /var/log/secure -n 20

-->RHEL7.7(ip:192.168.10.110)机器

Jun  9 10:17:28 rhel77 sshd[12271]: Server listening on :: port 22.
Jun  9 10:17:28 rhel77 polkitd[948]: Unregistered Authentication Agent for unix-process:12264:668614 (system bus name :1.316, object path /org/freedesktop/PolicyKit1/AuthenticationAgent, locale en_US.UTF-8) (disconnected from bus)
Jun  9 10:24:34 rhel77 sshd[12868]: Authentication refused: bad ownership or modes for directory /root
Jun  9 10:24:36 rhel77 sshd[12868]: Connection closed by 192.168.10.135 port 36168 [preauth]
[root@rhel77 ~]# 

-->CentOS7.9(ip:192.168.10.135)机器

Jun  9 10:16:58 centos79 polkitd[728]: Unregistered Authentication Agent for unix-process:5517:669130 (system bus name :1.203, object path /org/freedesktop/PolicyKit1/AuthenticationAgent, locale zh_CN.UTF-8) (disconnected from bus)
Jun  9 10:17:33 centos79 sshd[5534]: Authentication refused: bad ownership or modes for directory /root
Jun  9 10:17:37 centos79 sshd[5534]: Connection closed by 192.168.10.110 port 38882 [preauth]
Jun  9 10:24:02 centos79 sshd[5599]: Authentication refused: bad ownership or modes for directory /root
Jun  9 10:24:03 centos79 sshd[5599]: Connection closed by 192.168.10.110 port 38884 [preauth]
[root@centos79 ~]# 

问题点:

-->RHEL7.7(ip:192.168.10.110)机器

Jun  9 10:24:34 rhel77 sshd[12868]: Authentication refused: bad ownership or modes for directory /root


-->CentOS7.9(ip:192.168.10.135)机器

Jun  9 10:24:02 centos79 sshd[5599]: Authentication refused: bad ownership or modes for directory /root

通过google搜索排查定位,被告知:/root目录权限过大(排查发现root目录权限为777),最多(建议)设置为700权限

/root目录权限

更改前:

-->RHEL7.7(ip:192.168.10.110)机器

[root@rhel77 ~]# cd /
[root@rhel77 /]# pwd
/
[root@rhel77 /]# ls -ld root
drwxrwxrwx. 17 root root 8192 Jun  9 08:33 root
[root@rhel77 /]# 

-->CentOS7.9(ip:192.168.10.135)机器

[root@centos79 ~]# cd /
[root@centos79 /]# pwd
/
[root@centos79 /]# ls -ld root
drwxrwxrwx. 25 root root 4096 6月   9 09:37 root
[root@centos79 /]# 

权限更改,更改后:

-->RHEL7.7(ip:192.168.10.110)机器

[root@rhel77 /]# pwd
/
[root@rhel77 /]# chmod 700 root/
[root@rhel77 /]# ls -ld root
drwx------. 17 root root 8192 Jun  9 08:33 root
[root@rhel77 /]# 

-->CentOS7.9(ip:192.168.10.135)机器

[root@centos79 /]# pwd
/
[root@centos79 /]# chmod 700 root
[root@centos79 /]# ls -ld root
drwx------. 25 root root 4096 6月   9 09:37 root
[root@centos79 /]# 

5.ssh互信登录验证

-->RHEL7.7(ip:192.168.10.110)机器

[root@rhel77 /]# ssh 192.168.10.135
Last login: Fri Jun  9 09:55:34 2023 from rhel77

IPAddress: 	172.17.0.1
Memory Used: 	17.9%
Swap Used: 	0%
Disk Used: 	27%
Disk Size: 	38G
Services: 	46
系统内核: 	3.10.0-1160.90.1.el7.x86_64
yum源已配置,能正常使用
[root@centos79 ~]# hostname
centos79
[root@centos79 ~]# exit
logout
Connection to 192.168.10.135 closed.
[root@rhel77 /]# 

-->CentOS7.9(ip:192.168.10.135)机器

[root@centos79 ~]# ssh 192.168.10.110
Last login: Fri Jun  9 10:33:32 2023 from gateway

IPAddress: 	192.168.10.110
Cpu Used: 	1.00%
Memory Used: 	5.3%
Swap Used: 	0%
Disk Used: 	8%
Disk Size: 	69G
Services: 	40
system core: 	3.10.0-1062.el7.x86_64
yum already installation
[root@rhel77 ~]# hostname
rhel77
[root@rhel77 ~]# exit
登出
Connection to 192.168.10.110 closed.
[root@centos79 ~]# 

至此,问题解决。

四.总结梳理

Linux服务器之前进行ssh互信免密登录时,文件及目录的权限有严格控制,不能过渡授权,主要点:

1./root目录权限为:700

2..ssh目录权限为:700

3.文件权限(id_rsa,id_rsa.pug,authorized_keys,known_hosts):

-->id_rsa:私钥,相当于"锁"。文件权限:600,不能更改。

-->id_rsa.pub:公钥,相当于"钥匙"。文件权限:644,不能更改。

-->authorized_keys:认证文件,记录"别人"(即:对端)给你的公钥“钥匙”。文件权限:600,不能更改。

-->known_hosts:“指纹”文件,记录首次SSH互信认证"别人"(即:对端)留给你的“指纹”信息。文件权限:600,不能更改。

4.养成看ssh服务日志/var/log/secure的习惯

以上是我的一次真实的Linux服务器配置SSH免密码登录后,登录仍提示输入密码的问题排查解决记录。希望各位有所帮助。

创作不易,如果对你有所帮助或喜欢,请一键三连!

谢谢!文章来源地址https://www.toymoban.com/news/detail-697781.html

到了这里,关于Linux服务器配置SSH免密码登录后,登录仍提示输入密码(一次真实的问题排查解决记录)的文章就介绍完了。如果您还想了解更多内容,请在右上角搜索TOY模板网以前的文章或继续浏览下面的相关文章,希望大家以后多多支持TOY模板网!

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处: 如若内容造成侵权/违法违规/事实不符,请点击违法举报进行投诉反馈,一经查实,立即删除!

领支付宝红包 赞助服务器费用

相关文章

  • 输入正确但提示ssh服务器拒绝了该密码

    通过mobaxterm输入正确密码但无法登录情况 以及Xshell显示拒绝了该密码     若出现能够ping通,且sshd服务正常运行,防火墙,selinux设置无误但无法访问的情况则需要检查该配置文件并查看以下参数(大小写无误) sshd 配置文件为/etc/ssh/sshd_config AllowUsers AllowGroups DenyUsers DenyGro

    2024年02月15日
    浏览(33)
  • 简单几步学会Linux用户使用SSH远程免密登录,LinuxSSH服务器配置允许/禁止某些用户远程登录

    本文基于Linux上CentOS 7版本配合Xshell 7进行演示 目录 一.SSH简介 1.介绍 2.工作流程 二.具体配置免密步骤 1.配置前准备工作 2.正式配置过程 三.在服务器端配置SSH远程黑白名单 1.配置文件/etc/ssh/sshd_config部分参数解析 2.配置远程登录黑白名单 SSH用于计算机之间的加密登录,是一类

    2024年02月22日
    浏览(37)
  • vscode SSH 保存密码自动登录服务器vs code

    先在win local /mac 上拿到公钥和私钥,然后再把这公钥copy 进服务器。让ssh 身份认证转化为秘钥认证 (mac也是一样的) 第一步是在客户端机器(通常是您的计算机 win 10)上创建密钥对:打开powershell, 输入 默认情况下ssh-keygen将创建一个 2048 位 RSA 密钥对,这对于大多数用例来说

    2024年02月05日
    浏览(40)
  • vscode 配置ssh 免密登录 多台服务器

    Visual Studio Code - Code Editing. Redefined 之前一直用pycharm 但是好像社区免费版本不能连接服务器,还要本地同步代码,比较繁琐,因此改用vscode。 添加后可以尝试登录,确认下账号密码,vpn是否正常 ssh name@ip -22 输入密码即可 win+r 打开运行 cmd 弹出terminal ssh-keygen 三次回车 默认地

    2024年02月17日
    浏览(45)
  • VSCode配置SSH远程免密登录服务器

    VScode远程开发时,每次都需要输入密码,其实同理可以和其他应用类似配置免密登录,流程也类似。 1.在本地主机生成公钥和秘钥         ssh-keygen 2.将公钥内容添加至服务器         将生成钥对时会给出其保存路径,找到公钥,复制内容,添加到如下文件。         s

    2024年02月13日
    浏览(36)
  • 阿里云服务器环境配置,ssh免密登录和配置docker

    此文章适合ubuntu20.04 64位和ubuntu22.04 64位版本 一.登陆服务器 租完服务器后,首选需要使用本地gitbash或者cmd进入服务器, 命令: ssh root@xxx   xxx为服务器公网ip,然后yes,然后输入密码就会进入自己的服务器, 二.创建用户 阿里云服务器默认权限是root用户,权限太高,所以需

    2024年02月04日
    浏览(34)
  • CentOS Linux服务器无法远程 SSH 登录故障处理

    在管理 CentOS Linux 服务器时,远程 SSH 登录是一项关键功能。然而,有时候你可能会遇到无法通过 SSH 远程登录到服务器的问题。这篇文章将为你提供一些故障处理的步骤,帮助你解决这个问题。 以下是一些可能导致无法远程 SSH 登录的常见问题和相应的解决方法: 确认 SSH 服

    2024年02月05日
    浏览(47)
  • (Windows )本地连接远程服务器(Linux),免密码登录设置

    在使用VScode连接远程服务器时,每次打开都要输入密码,以及使用ssh登录或其它方法登录,都要本地输入密码,这大大降低了使用感受,下面总结了免密码登录的方法,用起来巴适得很,起飞。 本地必须在PowerShell终端操控,如下: 打开终端后在终端输入以下命令: 回车再回

    2024年02月11日
    浏览(40)
  • Windows服务器管理技巧:多用户登录设置、开启防火墙与SSH远程登录配置指南

    WindowsServer服务器管理技巧:对于使用WindowsServer服务器开发人员或者运维人员初学者来说,可能会遇到很多问题,比如:如何设置允许多用户同时登录服务器?如何开启服务器防火墙?Windows如何配置SSH远程登录?等等,如果遇到了这些问题,来看看这篇文章就能解决啦! 如果

    2024年02月13日
    浏览(39)
  • 两台服务器上的两个docker容器之间配置ssh免密登录

    因为要在两台服务器的容器上使用ucx实现GPU的RDMA,所以需要两个容器之间ssh免密登录 步骤如下(所有步骤均在容器内部进行) 切换成root用户 在容器内部安装openssh: # apt-get install openssh-client openssh-server 编辑ssh的配置文件,更改ssh的端口: # vim /etc/ssh/sshd_config 在最后一行加

    2024年03月13日
    浏览(42)

觉得文章有用就打赏一下文章作者

支付宝扫一扫打赏

博客赞助

微信扫一扫打赏

请作者喝杯咖啡吧~博客赞助

支付宝扫一扫领取红包,优惠每天领

二维码1

领取红包

二维码2

领红包