Azure Linux VM密鑰登陸

Azure  Linux  VM密鑰登陸

前提條件:

Azure 上建立兩臺Linux虛擬機,其它一臺已經使用使用"ssh-keygen -t rsa"命令來建立公鑰。linux

(若是不須要修改,直接回車兩次便可,默認保存路徑爲"~/.ssh/")vim

 

blob.png

操做步驟:

"~/.ssh/id_rsa.pub"這個文件拷貝到"40.125.167.182"服務器的"~/.ssh/"目錄中並更名爲"authorized_keys"centos

[wangruifeng@centos .ssh]$ scp id_rsa.pub wangruifeng@40.125.167.182:/home/wangruifeng/.ssh/authorized_keys緩存

The authenticity of host '40.125.167.182 (40.125.167.182)' can't be established.安全

ECDSA key fingerprint is SHA256:utVm2XGMADXER1jJKS33wX/NGx5Izy0DjL8mXxXqUsE.服務器

ECDSA key fingerprint is MD5:d5:3f:dd:ff:3a:dc:44:15:dd:37:fc:31:04:6a:8c:7e.ssh

Are you sure you want to continue connecting (yes/no)? yeside

Warning: Permanently added '40.125.167.182' (ECDSA) to the list of known hosts.ui

wangruifeng@40.125.167.182's password:this

id_rsa.pub                                                                                                     100%  400   408.3KB/s   00:00   

 

修改"40.125.167.182"服務器的"~/.ssh/"目錄權限爲700"~/.ssh/authorized_keys"文件權限爲600

(這是linux的安全要求,若是權限不對,自動登陸將不會生效。)

 

blob.png

 

再次使用ssh鏈接,已經實現密鑰匙登陸:

blob.png

 

故障排查:

可能還會提示輸入密碼的解決方法:

 

1) 若是出現報警:"Address X.X.X.X maps to localhost, but this does not map back to the address - POSSIBLE BREAK-IN ATTEMPT!"

 

"XXXXX"(鏈接端)服務器上執行以下命令:

echo "GSSAPIAuthentication no" >> ~/.ssh/config

 

"XXXXX"(被鏈接端)服務器上執行"vi /etc/ssh/sshd_config"命令,修改下面兩項值爲"no"

"GSSAPIAuthentication no"

"UseDNS no"

 

2) 若是出現報警:"Agent admitted failure to sign using the key."

 

執行命令:"ssh-add"(把專用密鑰添加到ssh-agent的高速緩存中)

 

若是還不行,執行命令:"ps -Af | grep agent "

(檢查ssh代理是否開啓,若是有開啓的話,kill掉該代理)

而後執行"ssh-agent"(從新打開一個ssh代理)

 

若是仍是不行,繼續執行命令:"sudo service sshd restart"(重啓一下ssh服務)

 

3) 經過命令"/usr/sbin/sestatus -v" 查看SELinux狀態,若是"SELinux status"參數爲"enabled"(開啓狀態),則關閉SELinux

 

臨時關閉方法(不用重啓機器):"setenforce 0"

 

修改配置文件關閉方法(須要重啓機器):執行命令"/etc/selinux/config",將"SELINUX=enforcing"改成"SELINUX=disabled"

4) 執行命令"vim /etc/ssh/sshd_config"去掉下面三行的註釋:

 

"RSAAuthentication yes"

"PubkeyAuthentication yes"

"AuthorizedKeysFile      .ssh/authorized_keys"

 

SSH免密碼登陸原理:

 

這種方式你須要在客戶端服務器上爲本身建立一對密匙,並把公匙放在須要登陸的服務器上。

當你要鏈接到服務器上時,客戶端就會向服務器請求使用密匙進行安全驗證。

服務器收到請求以後,會在該服務器上你所請求登陸的用戶的家目錄下尋找你的公匙,

而後與你發送過來的公匙進行比較。

若是兩個密匙一致,服務器就用該公匙加密「質詢」並把它發送給客戶端。

客戶端收到「質詢」以後用本身的私匙解密再把它發送給服務器。

 

使用場景:

 

1) rsync自動備份時免密碼登陸;

2) 集羣環境中須要主機間互相通訊;

3) 自動部署項目,將編譯好的war包放到正確的服務器上。

相關文章
相關標籤/搜索