我們知道兩臺Linux服務(wù)器機(jī)器之間如果使用ssh命令登錄或scp/rsync命令傳輸文件每一次都需要輸入用戶名相對應(yīng)的密碼,如果要免密碼,則需要對兩臺Linux服務(wù)器機(jī)器之間進(jìn)行SSH互信。
一.SSH介紹
1.SSH互信原理
雖然這是廢話,也希望大家了解一下。
SSH(Secure Shell)是一種安全的傳輸協(xié)議,它可以讓Linux系統(tǒng)中的服務(wù)器和客戶端之間進(jìn)行安全可靠的通訊,它常被用于在本地網(wǎng)絡(luò)中的多臺計(jì)算機(jī)之間實(shí)現(xiàn)遠(yuǎn)程登錄,文件傳輸和系統(tǒng)管理。
SSH使用“加密”的傳輸方式,以保證客戶端和服務(wù)器之間的通訊安全。具體而言,SSH使用加密技術(shù)(默認(rèn)加密技術(shù):rsa,加密位:2048位)將用戶數(shù)據(jù)和控制指令加密,以保護(hù)數(shù)據(jù)不被第三方攔截。
SSH可以驗(yàn)證客戶端的身份,確保只有授權(quán)的用戶才能訪問服務(wù)器。
要在Linux系統(tǒng)中運(yùn)行SSH,需要兩個軟件:一個是服務(wù)器端的軟件,另一個是客戶端的軟件。服務(wù)器端的軟件叫做OpenSSH-Server,主要實(shí)現(xiàn)SSH服務(wù)器功能。它可以處理SSH消息并執(zhí)行用戶指令,以實(shí)現(xiàn)遠(yuǎn)程登錄功能??蛻舳说能浖凶鯯SH-Client,會根據(jù)服務(wù)器端提供的信息將用戶指令加密,并且可以認(rèn)證服務(wù)器的身份。
2.SSH RPM包
OpenSSH所對應(yīng)的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
說明如下:
Packages名 | 說明 |
openssh-7.4p1-21.el7.x86_64.rpm | openssh核心文件 |
openssh-askpass-7.4p1-21.el7.x86_64.rpm | ?持對話框窗? 顯示 X系統(tǒng) |
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 | 服務(wù)器端軟件包 |
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:私鑰,相當(dāng)于"鎖"。文件權(quán)限:600,不能更改。
id_rsa.pub:公鑰,相當(dāng)于"鑰匙"。文件權(quán)限:644,不能更改。
authorized_keys:認(rèn)證文件,記錄"別人"(即:對端)給你的公鑰“鑰匙”。文件權(quán)限:600,不能更改。
known_hosts:“指紋”文件,記錄首次SSH互信認(rèn)證"別人"(即:對端)留給你的“指紋”信息。文件權(quán)限:600,不能更改。
4.ssh配置文件sshd_config
目錄路徑:/etc/ssh
sshd日志默認(rèn)保存在/var/log/secure中
(cat /etc/ssh/sshd_config):
SyslogFacility AUTHPRIV
(cat /etc/rsyslog.conf):
authpriv.* ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ?/var/log/secure
二.問題重現(xiàn)
1.環(huán)境信息
VMware CentOS7.9(IP:192.168.10.135)、RHEL7.7(IP:192.168.10.110)
防火墻及selinux關(guān)閉,參考(Chapter1):
Linux常規(guī)基礎(chǔ)配置_小黑要上天的博客-CSDN博客
2.兩臺機(jī)器實(shí)現(xiàn)openssh rpm安裝
-->RHEL7.7(ip:192.168.10.110)機(jī)器
[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)機(jī)器
[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.兩臺機(jī)器機(jī)器實(shí)現(xiàn)ssh互信
-->RHEL7.7(ip:192.168.10.110)機(jī)器
命令:
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): #強(qiáng)烈建議直接回車使用默認(rèn)路徑
/root/.ssh/id_rsa already exists.
Overwrite (y/n)? y
Enter passphrase (empty for no passphrase): #密鑰的密碼短語(建議留空則直接回車)
Enter same passphrase again: #密鑰的密碼短語確認(rèn)(建議留空則直接回車)
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)機(jī)器
命令:
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互信驗(yàn)證-問題重現(xiàn)
-->從RHEL7.7(ip:192.168.10.110)機(jī)器 ssh 到 CentOS7.9(ip:192.168.10.135)機(jī)器
-->從CentOS7.9(ip:192.168.10.135)機(jī)器 ssh 到 RHEL7.7(ip:192.168.10.110)機(jī)器
三. 問題解決梳理
1.兩臺機(jī)器文件權(quán)限驗(yàn)證(id_rsa,id_rsa.pub,authorized_keys,known_hosts)
-->RHEL7.7(ip:192.168.10.110)機(jī)器
[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]#
結(jié)論:文件權(quán)限無誤
-->CentOS7.9(ip:192.168.10.135)機(jī)器
[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]#
結(jié)論:文件權(quán)限無誤
2.兩臺機(jī)器.ssh目錄權(quán)限驗(yàn)證
-->RHEL7.7(ip:192.168.10.110)機(jī)器
[root@rhel77 ~]# pwd
/root
[root@rhel77 ~]# ls -ld .ssh/
drwx------ 2 root root 80 Jun 9 09:27 .ssh/
[root@rhel77 ~]#
結(jié)論:.ssh目錄權(quán)限為700,權(quán)限無誤
-->CentOS7.9(ip:192.168.10.135)機(jī)器
[root@centos79 ~]# pwd
/root
[root@centos79 ~]# ls -ld .ssh/
drwx------ 2 root root 80 6月 9 09:27 .ssh/
[root@centos79 ~]#
結(jié)論:.ssh目錄權(quán)限為700,權(quán)限無誤
3.兩臺機(jī)器更改/etc/ssh/sshd_config文件配置
添加如下信息:
RSAAuthentication yes? ? ? ? ? ? ???#允許RSA密鑰
PubkeyAuthentication yes? ? ? ? ??#啟用公告密鑰配對認(rèn)證方式
################################################
添加位置:
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,發(fā)現(xiàn)問題仍舊存在。
命令:
systemctl restart sshd
systemctl status sshd
4.問題點(diǎn)定位
最后,通過查看/var/log/secure,發(fā)現(xiàn)了問題的點(diǎn)
命令:
tail /var/log/secure -n 20
-->RHEL7.7(ip:192.168.10.110)機(jī)器
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)機(jī)器
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 ~]#
問題點(diǎn):
-->RHEL7.7(ip:192.168.10.110)機(jī)器
Jun ?9 10:24:34 rhel77 sshd[12868]: Authentication refused: bad ownership or modes for directory /root
-->CentOS7.9(ip:192.168.10.135)機(jī)器
Jun ?9 10:24:02 centos79 sshd[5599]: Authentication refused: bad ownership or modes for directory /root
通過google搜索排查定位,被告知:/root目錄權(quán)限過大(排查發(fā)現(xiàn)root目錄權(quán)限為777),最多(建議)設(shè)置為700權(quán)限
/root目錄權(quán)限
更改前:
-->RHEL7.7(ip:192.168.10.110)機(jī)器
[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)機(jī)器
[root@centos79 ~]# cd /
[root@centos79 /]# pwd
/
[root@centos79 /]# ls -ld root
drwxrwxrwx. 25 root root 4096 6月 9 09:37 root
[root@centos79 /]#
權(quán)限更改,更改后:
-->RHEL7.7(ip:192.168.10.110)機(jī)器
[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)機(jī)器
[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互信登錄驗(yàn)證
-->RHEL7.7(ip:192.168.10.110)機(jī)器
[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
系統(tǒng)內(nèi)核: 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)機(jī)器
[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 ~]#
至此,問題解決。
四.總結(jié)梳理
Linux服務(wù)器之前進(jìn)行ssh互信免密登錄時,文件及目錄的權(quán)限有嚴(yán)格控制,不能過渡授權(quán),主要點(diǎn):
1./root目錄權(quán)限為:700
2..ssh目錄權(quán)限為:700
3.文件權(quán)限(id_rsa,id_rsa.pug,authorized_keys,known_hosts):
-->id_rsa:私鑰,相當(dāng)于"鎖"。文件權(quán)限:600,不能更改。
-->id_rsa.pub:公鑰,相當(dāng)于"鑰匙"。文件權(quán)限:644,不能更改。
-->authorized_keys:認(rèn)證文件,記錄"別人"(即:對端)給你的公鑰“鑰匙”。文件權(quán)限:600,不能更改。
-->known_hosts:“指紋”文件,記錄首次SSH互信認(rèn)證"別人"(即:對端)留給你的“指紋”信息。文件權(quán)限:600,不能更改。
4.養(yǎng)成看ssh服務(wù)日志/var/log/secure的習(xí)慣
以上是我的一次真實(shí)的Linux服務(wù)器配置SSH免密碼登錄后,登錄仍提示輸入密碼的問題排查解決記錄。希望各位有所幫助。
創(chuàng)作不易,如果對你有所幫助或喜歡,請一鍵三連!文章來源:http://www.zghlxwxcb.cn/news/detail-697781.html
謝謝!文章來源地址http://www.zghlxwxcb.cn/news/detail-697781.html
到了這里,關(guān)于Linux服務(wù)器配置SSH免密碼登錄后,登錄仍提示輸入密碼(一次真實(shí)的問題排查解決記錄)的文章就介紹完了。如果您還想了解更多內(nèi)容,請?jiān)谟疑辖撬阉鱐OY模板網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關(guān)文章,希望大家以后多多支持TOY模板網(wǎng)!