Linux伺服器之間怎麼樣SSH不需密碼

來源:互聯網
上載者:User

以下步驟以root使用者為例,實際應用時換成執行指令碼的屬主使用者執行同樣的步驟即可。

1. 分別從A和B上登入作為root後運行ssh-keygen,目的是簡單地在/root下建立目錄.ssh並產生dsa的金鑰組,當提示輸入passphase時打入兩次斷行符號。這將產生/root/.ssh/id_dsa 和 /root/.ssh/id_dsa.pub。
ssh-keygen -t dsa 輸出:Generating public/private dsa key pair.
Enter file in which to save the key (/home/sj/.ssh/id_dsa):
Enter passphrase (empty for no passphrase):
Enter same passphrase again:
Your identification has been saved in /home/sj/.ssh/id_dsa.
Your public key has been saved in /home/sj/.ssh/id_dsa.pub.
The key fingerprint is:
1b:8c:3d:6d:eb:e3:10:c3:b7:c0:68:a3:5f:7e:b3:51 sj@192.168.0.1
2. 先測試一下,從A登入作為root,打入#ssh B,會要求提示root password。

3. 把A的公開金鑰加入到B的.ssh/authorized_keys檔案中。如果authorized_keys檔案不存在,直接將公開金鑰複製即可,比如:
運行scp /root/.ssh/id_dsa.pub B:/root/.ssh/authorized_keys。檔案名稱必須不能弄錯。

4. 現在再從A運行#ssh B則不要求輸入密碼了。

 

基本原理:
如果你希望從A作為使用者user1 SSH 到B 作為使用者user2,若要用RSA鍵對法認證且不要求輸入密碼,則B上的ssh背景程式拿出~user2/.ssh/authorized_keys中的與A有關的RSA公開金鑰來對A上的以user1身份啟動並執行ssh客戶程式進行挑戰,前面我們已經把A上的identity.pub拷貝到B上作為authorized_keys了。則A保持私密金鑰identity,而B上的sshd又拿出A的公開金鑰來挑戰,因此成功,你可以加很多行到authorized_keys以允許其它伺服器的公開金鑰可以加進來。


常見應用舉例:
1. 在A上以使用者rootSSH到B上作為普通使用者user而不是root,因為為了安全,有時不需要作為root,只需要作為普通使用者不要求輸入密碼以完成相應工作即可。

登入作為root在A上A# scp /root/.ssh/identity.pub href="mailto:user@B:/home/user/.ssh/authorized_keys">user@ B:/home/user/.ssh/authorized_keys
2. tar一批檔案從A到B。A# tar cvf - /etc | ( ssh B tar xvf -)
3. 運行A上的cronjob,對某檔案作改動後傳到B0 1 * * * cat /etc/shadow | awk -F: '{print $1":"$2}' | (ssh B 'cat >/home/passwd')

常見問題:
1. 這種方式的SSH需要B上的sshd_config中允許直接的root login (PermitRootLoginyes).
2. 我有伺服器A和其它伺服器B,C,D。我想從ASSH到這些伺服器但不要自動,最好輸入RSA passphase,但太麻煩,怎麼辦?
a. 要A上運行ssh-keygen時輸入passphase字。
b. 要登入到A上後運行A#ssh-agent bash
c. 再運行ssh-add加passphase進入記憶體
d. 再運行ssh B or SSH C or SSH D,則不再要求輸入密碼,密碼對只需輸一次
e. 最後退出shell前輸入ssh-agent -k 。 

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.