Try   HackMD

如何設置從 Windows 到 Linux 的無密碼 SSH 連接

此文章會教導如何利用公開金鑰加密使 SSH 連線到遠端裝置時更加的安全與快速
John @ 崑山科技大學 光達實驗室 KSU Eilidar LabFriday, March 30, 2023

公開金鑰加密 (Public Key Authentication) 是一種能安全登入 SSH 的方式,該方式使用了公開金鑰密鑰 (Public-key cryptography) 來進行驗證。雖然設定使用強度很高的密碼可以有效防止暴力破解,但使用公開金鑰驗證不僅可以提供高強度的加密,還可讓使用者無密碼(Passwordless)的自動登入遠端裝置,省去輸入繁瑣且複雜的密碼所消耗的時間。

在 Windows 中產生 SSH 金鑰

在 Windows 中產生一組金鑰

  1. 開啟 Windows Terminal 或 Powershell 並輸入下面的指令來開始產生金鑰

    ​​​​ssh-keygen
    
  2. 一開始終端機會問要把產生出來的金鑰放在哪裡,這邊可以使用預設值就可,直接按下 Enter 執行下一步

    Image Not Showing Possible Reasons
    • The image file may be corrupted
    • The server hosting the image is unavailable
    • The image path is incorrect
    • The image format is not supported
    Learn More →

    • 用來儲存金鑰的預設位置是在使用者家目錄下的 .ssh 資料夾中 C:\User\<UserName>\.ssh\
      如果該資料夾裡面已經有一組鑰匙檔案了,可以在這邊輸入其他名稱來避免新的金鑰把舊的金鑰檔案覆蓋掉。
  3. 接下來終端機會要求你輸入密碼,這個密碼可以不用設定,可以直接按下 Enter 來越過。按下第一次 Enter 後終端機會再問你一次密碼來確保兩次密碼的輸入都是正確的,如果沒有設定密碼,直接按下 Enter 越過即可

    Image Not Showing Possible Reasons
    • The image file may be corrupted
    • The server hosting the image is unavailable
    • The image path is incorrect
    • The image format is not supported
    Learn More →

  4. 密碼設定完後,終端機會列印出有關金鑰的訊息,例如儲存位置、SHA256 演算法的指紋以及以圖形表式的金鑰。

    Image Not Showing Possible Reasons
    • The image file may be corrupted
    • The server hosting the image is unavailable
    • The image path is incorrect
    • The image format is not supported
    Learn More →

  5. 在終端機輸入以下指令來確認金鑰是否有儲存成功

    ​​​​ls ~\.ssh\
    

    Image Not Showing Possible Reasons
    • The image file may be corrupted
    • The server hosting the image is unavailable
    • The image path is incorrect
    • The image format is not supported
    Learn More →

    目錄下面應該會有兩個檔案

    • id_rsa 是私人金鑰
    • id_rsa.pub 是公開金鑰

將公開金鑰複製到遠端 Linux 裝置中

在我們產生好一組金鑰之後,必須要在想連線到的遠端 Linux 作業系統中複製剛剛產生的公開金鑰至該遠端裝置中

  1. 在遠端 Linux 裝置中開啟終端機,並在家目錄下創一個 .ssh 資料夾,並將該資料夾的權限設定為 700,也就是擁有者可以讀、寫與執行,群組與其他人皆不允許讀、寫與執行

    ​​​​mkdir ~/.ssh && chmod 700 ~/.ssh
    
  2. 回到 Windows 中,使用指令把 C:\User\<UserName>\.ssh\ 資料夾中的 id_rsa.pub 檔案複製到遠端的 Linux 電腦中,並將該檔案的權限設定為 600,也就是擁有者可以讀、寫,但不能執行,群組與其他人皆不允許讀、寫與執行

    ​​​​type $env:USERPROFILE\.ssh\id_rsa.pub | ssh <遠端電腦位置> "cat >> .ssh/authorized_keys && chmod 600 ~/.ssh/authorized_keys"
    
    • 指令中的 <遠端電腦位置> 需要改成該電腦在你這個網絡上的名稱或是 IP 位置
  3. 按下 Enter 後,終端機會顯示無法建立連線,因為你的電腦還不知道這台遠端裝置,是否要繼續連線,在這邊請輸入 yes 來繼續

    Image Not Showing Possible Reasons
    • The image file may be corrupted
    • The server hosting the image is unavailable
    • The image path is incorrect
    • The image format is not supported
    Learn More →

  1. 輸入 yes 確認連線後,終端機會問你這個使用者在這台遠端裝置的密碼,輸入密碼後公開金鑰就會被複製到遠端裝置中了
    Image Not Showing Possible Reasons
    • The image file may be corrupted
    • The server hosting the image is unavailable
    • The image path is incorrect
    • The image format is not supported
    Learn More →
  1. 再 Windows 的終端機中再輸入一次連線到遠端電腦的指令,看看是否還會需要輸入密碼
    Image Not Showing Possible Reasons
    • The image file may be corrupted
    • The server hosting the image is unavailable
    • The image path is incorrect
    • The image format is not supported
    Learn More →
  • 如果沒有作用,Windows 終端機還是要你輸入密碼
    • 在遠端電腦上的終端機用指令來查看檔案是否有被成功複製,且該檔案的權限為-rw-------
      ​​​​​​​​ll ./.ssh/
      
      Image Not Showing Possible Reasons
      • The image file may be corrupted
      • The server hosting the image is unavailable
      • The image path is incorrect
      • The image format is not supported
      Learn More →
  • 重新啟動 ssh 服務,開啟終端機並輸入以下指令
    bash sudo systemctl restart ssh.service

參考資料

tags: SSH Linux/Unix Ubuntu Passwordless