1. 程式人生 > >Linux多臺伺服器間SSH免密碼登入配置

Linux多臺伺服器間SSH免密碼登入配置

SSH實現各個伺服器間的檔案相互備份,如執行scp命令,可以實現免密碼登入,從而可以使用SHELL指令碼實現一些自動化的處理。

假如A機要免密碼登入B機,具體方法如下:

1、在A機執行:"ssh-keygen -t rsa" 命令,建立公鑰資訊

#ssh-keygen -t rsa 
Generating public/private rsa key pair. 
Enter file in which to save the key (<UseHome>/.ssh/id_rsa): 
Enter passphrase (empty for no passphrase): 
Enter same passphrase again: 
Your identification has
been saved in <UseHome>/.ssh/id_rsa. Your public key has been saved in <UseHome>.ssh/id_rsa.pub. The key fingerprint is: ba:2c:77:eb:6d:aa:aa:d8:37:08:2f:b1:b9:e8:5a:2f <UseName>@<HostName>

輸入後,會提示建立.ssh/id_rsa、id_rsa.pub的檔案,其中第一個為金鑰,第二個為公鑰。過程中會要求輸入密碼,為了ssh訪問過程無須密碼,可以直接回車

2、公鑰部署

拷貝id_rsa.pub中的內容到需要訪問的主機,並建立~/.ssh/authorized_keys檔案,將先前的id_rsa.pub拷貝到目標機B,如果.ssh目錄不存在,則建立資料夾,並將許可權設定為700,同時將authorized_keys檔案許可權設為644,.ssh檔案下及下的authorized_keys檔案都屬於root。

#mkdir -m=700 .ssh
#cp id_rsa.pub .ssh/authorized_keys
#chown root .ssh/authorized_keys
#chmod 644 .ssh/authorized_keys

3. ssh訪問

使用ssh <IP/HOSTNAME>進行訪問,第一次需要儲存ssh認證資訊,以後則可以自動登入,其他相關ssh相關程式諸如scp等也可以無需密碼 

# ssh <TargetHost> 
The authenticity of host '<TargetHost> (<TargetIP>)' can't be established. 
RSA key fingerprint is 34:b9:92:06:53:e6:91:4d:47:92:73:57:78:6a:5d:09. 
Are you sure you want to continue connecting (yes/no)?yes 
Warning: Permanently added '<TargetHost>,<TargetIP>' (RSA) to the list of known hosts.

4.多機部署

如果C機也需要無密碼訪問B機,則將C的公鑰id_rsa.pub複製到B機中檔案~/.ssh/authorized_keys的內容末端

#cat id_dsa.pub >> .ssh/authorized_keys