最新SSH自動登入方案(RSA)

來源:互聯網
上載者:User

最新SSH自動登入方案(RSA) 一. 前言    直接使用ssh串連到伺服器時,需要手動輸入密碼。如果能夠不用手動輸入密碼,且自動登入到SSH伺服器的話,那就爽了。    經過一晚上的研究,終於搞定了。(原因在於舊的配置方式已不能正確使用了)    在進行配置前,先假定ssh用戶端為ssh-client,ssh伺服器為ssh-server(運行ssh服務的一邊)。 二. 配置方法    1. 在ssh-client一邊使用ssh-keygen產生一對rsa key       $ssh-keygen -t rsa  Generating public/private rsa key pair.  Enter file in which to save the key (/home/abee/.ssh/id_rsa): <斷行符號>  Enter passphrase (empty for no passphrase): <斷行符號>  Enter same passphrase again: <斷行符號>       執行過程中連續斷行符號即可,成功產生key後。private key 儲存在~/.ssh/id_rsa,而public key儲存在~/.ssh/id_rsa.pub     2. 在ssh-client一邊使用ssh-add將剛產生的private key加入到ssh agent中(舊配置方式沒有這步喲)       $ssh-add ~/.ssh/id_rsa       Identity added: id_rsa (id_rsa)     3. 將上述的public key(id_rsa.pub)的內容附加到ssh-server端的.ssh/authorized_keys中       $cat ~/.ssh/id_rsa.pub | ssh username@ssh-server "cat - >> authorized_keys"     4. 此時,配置完成啦。當再次使用ssh username@ssh-server登入時,不再使用密碼啦。  三. 重啟ssh-client    重啟ssh-client後,需要重新使用ssh-add將上面的private key加入到ssh-agent中。因為ssh-agent重啟後,其沒有保留上次記錄的private key,因此需要重新使用ssh-add將private key加入。    經過測試,對同一個private key進行多次ssh-add是沒有問題的,總體感覺ssh-add或者ssh-agent會過濾重複的private key。    為了實現一次配置,重複使用。可以考慮將ssh-add的執行放到~/.profile下,每次重新登入X或者Console後,系統就會自動從~/.profile中的ssh-add命令將private key加到ssh-agent中。    $echo "ssh-add ~/.ssh/id_rsa" >> ~/.profile    這個方法在ubuntu10.04測試通過。 四. 單個ssh-client與多個ssh-server    經過測試,在不止一個ssh-server的情況下,每一個ssh-server可以共用上述的publice key。而ssh-client僅用一個pirvate key.  五. 特別注意    1. 上述使用RSA方式進行ssh身分識別驗證,具體可以查看man ssh。從上面兩種環境可以看出,ssh-keygen產生的private key是關鍵檔案,可以說這個就是ssh的登入密碼。因此在ssh-client一側,該檔案的許可權一般設定為600。    2. 在ssh-server側儲存public key的authorized_keys檔案的許可權至少是400,即最少也要使用者可讀。另外由於該public key可以計算出private key,且一般還會寫入其他public key,所以其許可權一般設定為700。    3. 一般情況下,對於多個ssh-server,最好的方案是針對每一個ssh-server使用唯一一對RSA key。這樣可以防止一個private key泄漏後,只有一個ssh-server的ssh安全受影響,其他ssh-server不受影響(因為使用不同的RSA key)。  六. 與舊配置的區別    1. .ssh/目錄在ssh-server/ssh-client端都沒有許可權要求,所以預設的755即可。    2. 上述配置均不需使用root許可權,也不需要配置/etc/ssh/sshd_config(預設即可),更不需要重啟ssh伺服器。 最後,希望對大家有用哈。

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.