Linux的ssh免密登陸
第一種情況兩臺主機連結:
Linux主機1 ipxxxxxx1
win10主機2或者Linux主機2 ipxxxxxx2
在Linux主機1中操作
第一步:選擇目錄
cd .ssh
第二步:生成金鑰,冒號直接空格或者填寫密碼,此處未填
ssh-keygen
[[email protected] .ssh]# 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:xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
The key's randomart image is:
+---[RSA 2048]----+
|*BX*o*++*o+ |
|Bo.o+.++.+ + |
|+o ..o. * |
|. .o. * o . |
| o. . = S . |
| o . o o |
| . . . |
| . |
| E |
+----[SHA256]-----+
ls檢視會有三個檔案authorized_keys id_rsa id_rsa.pub
第三步:公鑰寫入authorized_keys;替換命令是> 追加命令是>>
cat id_rsa.pub > authorized_keys
第四步: 配置 vim /etc/ssh/sshd_config把下面三項或四項寫入
RSAAuthentication yes
PubkeyAuthentication yes
PermitRootLogin yes
AuthorizedKeysFile .ssh/authorized_keys #公鑰檔案路徑
第5步:重啟sshd,centos7以下命令service sshd restart
centos7以上systemctl restart sshd.service
第6步:情況1:把Linux主機1公私auxxxkeys3個檔案拷貝到Linux主機2裡的cd .ssh下面
authorized_keys
id_rsa
id_rsa.pub
Linux主機2就可以訪問Linux主機1,已經實踐。
情況2:在主機2中重複第四,第五步,可以互相訪問。
情況3:如果是win10主機2新建ssh或者.ssh目錄把Linux主機1的公私鑰三個檔案放在下面,win10主機2下裝有ssh客服端就可以訪問Linux主機1了,已經實踐。
叢集:三臺機器或以上
方案一:一臺主機一個生成金鑰 ,如以上步驟第六步用情況2,把三個檔案分別複製三臺主機.ssh下面,互通。未實踐
方案二:每臺主機都生成金鑰,如以上步驟做到第五步。
第六步:使三臺主機公鑰都寫入一個主機1檔案的authorized_keys用命令>>追加
主機2 cat id_rsa.pub >> authorized_keys
主機3 cat id_rsa.pub >> authorized_keys
然後把主機1檔案authorized_keys替換到主機2,主機3 互通。未實踐