配置SSH無密碼登錄【原著】


環境:兩台Centos虛擬機,配置了靜態的ip。(詳見虛擬機如何配置靜態的IP地址的操作步驟)

192.168.75.21
192.168.75.22


第一步:為每台服務器配置靜態IP地址
參見: 虛擬機中CentOS7設置固定IP地址的方法

 

第二步:為每台服務器配置hostname

192.168.75.21上,配置/etc/hostname中,添加“master01”內容
192.168.75.22上,配置/etc/hostname中,添加“slave02”內容

 

在192.168.75.21上,執行下面命令,設置hostname

[root@master01 ~]# vim /etc/hostname
master01

在192.168.75.22上,執行下面命令,設置hostname

[root@slave02 ~]# vim /etc/hostname
slave02

 


第三步:配置hosts文件,新增兩條映射規則

192.168.75.21 master01
192.168.75.22 slave02


以下開始配置SSH無密碼登錄

第四步:分別在master01和slave02上,執行命令:

#ssh-keygen -t rsa,一路回車,在~/.ssh/目錄下產生2個文件,分別是id_rsa和id_rsa.pub

說明:id_rsa是私鑰,id_rsa.pub是公鑰
私鑰,需要各自機器妥善保管。
公鑰,需要添加到許可證文件authorized_keys中,保證每一台機器上的公鑰都在許可證文件authorized_keys中。

注意 許可證文件的路徑:~/.ssh/authorized_keys


第五步:將master01的公鑰文件內容,添加到許可證文件 ~/.ssh/authorized_keys 中
使用這個命令:
cat ~/.ssh/id_rsa.pub >> ~/.ssh/authorized_keys

說明:這個重定向符號“>>”會將命令執行后產生的標准輸出重定向附加到(目標文件)該文件之后。注意,是附加到文件后面。

 


第六步:在slave02上,將slave02的公鑰文件,添加到master01的許可證文件 ~/.ssh/authorized_keys中。.

ssh-copy-id -i ~/.ssh/id_rsa.pub root@master01

[root@slave02 ~]# ssh-copy-id -i ~/.ssh/id_rsa.pub root@master01
/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@master01's password:

Number of key(s) added: 1

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


暫時各個服務器不能免密操作,根據提示,輸入master01的登錄密碼,然后,完成上面命令。


第七步:在master01上,將master01上的許可證文件拷貝到 slave02上


[root@master01 ~]# scp ~/.ssh/authorized_keys root@slave02:~/.ssh/authorized_keys
root@slave02's password:
authorized_keys 100% 789 156.8KB/s 00:00

第八步.在master01上,使用ssh 'root@slave02'命令,測試可以直接免密直接切換到slave02上。

[root@master01 ~]# ssh 'root@slave02'
Last login: Mon Jun 24 19:24:33 2019 from master01
[root@slave02 ~]#

第九步,在slave02上,使用ssh 'root@master01'命令,測試可以直接免密直接切換到master01上。

[root@slave02 ~]# ssh 'root@master01'
Last login: Mon Jun 24 19:25:03 2019 from slave02
[root@master01 ~]#

完成!!!

 

 參考文章:【完全分布式Hadoop】(三)集群設置SSH無密碼登陸


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM