Try   HackMD

❤️ ssh 遠端免密碼 (一般設備 或 Github 存取)

tags: ssh, linux
  • 本教學用途: "我" 無密碼 ssh 至遠端
    • 我 (本地): 需要 generate RSA private & public key
    • 遠端: 需要把我的 public key 加進 authorized_keys
  • 本文章乃修改此來源撰寫而成
  • 只要三步驟,輕鬆完成設定
  • 本教學本機端是 windows,遠端是 linux ubuntu,不同作業系統的命令可能不互通,請自行更換相應操作的命令

  1. 本機端先設定
    • 下方命令會自動生成 .ssh 資料夾並依回答生成 id_rsa (私鑰) 與 id_rsa.pub (公鑰)
      ​​​​​​​​ssh-keygen -t rsa
      
      or 用 -f 指定路徑,最後的目錄同時會是 ssh keys 的名字
      注意!就算沒有 .ssh/my_mac 整條目錄都會自動生成
      ​​​​​​​​ssh-keygen -t rsa -f ~/.ssh/my_mac
      
      詳細自訂 key 路徑名稱與加密方式
      ​​​​​​​​ssh-keygen -t rsa -b 4096 -C "your_email@example.com" -f /path/to/private/key -N ""
      
      • 其中:
        • -t rsa 指定生成 RSA 類型的密鑰。
        • -b 4096 指定密鑰的位數為 4096。
        • -C "your_email@example.com" 用於添加註釋,可以用您的電子郵件地址替換其中的 your_email@example.com
        • -f /path/to/private/key 指定私鑰文件的路徑和名稱,可以替換 /path/to/private/key 為您想要的路徑和名稱。
        • -N "" 指定空字符串作為密鑰的密碼,這樣您在使用密鑰時就不需要輸入密碼了。
      • 在運行此命令後,會在指定的路徑下生成包含私鑰和公鑰的兩個文件,例如:
        • 私鑰文件:/path/to/private/key
        • 公鑰文件:/path/to/private/key.pub
      • 請注意,在使用 SSH 密鑰進行身份驗證時,需要將公鑰文件複製到遠程服務器的 ~/.ssh/authorized_keys 文件中,以便服務器可以識別您的密鑰並授權您的訪問。
    • 接著看到下列訊息並執行後續操作:
      ​​​​​​​​Generating public/private rsa key pair.
      ​​​​​​​​Enter file in which to save the key (C:\Users\<localuser>/.ssh/id_rsa):
      

      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 連線時需要的私人密碼,可以啥也不打直接按 Enter key (連線時不用額外密碼)

      ​​​​​​​​Enter passphrase (empty for no passphrase):
      ​​​​​​​​Enter same passphrase again:
      

      接著看到以下訊息:

      • Your identification has been saved in C:\Users<localuser>/.ssh/id_rsa.
      • Your public key has been saved in C:\Users<localuser>/.ssh/id_rsa.pub.
      • The key fingerprint is:
        SHA256:nxxxxxxxxxxxxxxxxxxxxj/xxxxx <localuser>@Desktop-win10
      • The key's randomart image is (一個獨一無二的數位指紋圖案):
      ​​​​​​​​+---[RSA 3072]----+
      ​​​​​​​​|       ....      |
      ​​​​​​​​|      ......     |
      ​​​​​​​​|       .....     |
      ​​​​​​​​+----[SHA256]-----+
      

      前步驟的回應若直接按 Enter,則在 .ssh 底下會自動生成 id_rsa id_rsa.pub,若有打其他內容,名稱會變

  2. 確認您的 SSH 金鑰是否正確配置並已添加到 ssh-agent 中。可以使用以下命令來添加金鑰到 ssh-agent 中:
    ​​​​$ ssh-add ~/.ssh/id_rsa
    
    這裡假設您的 SSH 金鑰是 id_rsa。
    • [!] 若出現以下錯誤訊息
      ​​​​​​​​Could not open a connection to your authentication agent.
      
      請先開啟 ssh agent,輸入以下命令
      ​​​​​​​​eval `ssh-agent -s`
      
  3. 讓 "遠端" 獲得你的 id_rsa.pub
    • 一般的遠端設備 (linux)
      • 將方才本機生成的公鑰傳送至遠端主機的 .ssh 資料夾內
        • 可用 scp 複製 id_rsa.pub 到遠端的某個目錄,讓遠端暫存 id_rsa.pub,方便等一下登入遠端後複製 id_rsa.pub 內容
          ​​​​​​​​​​​​​​​​scp ~/.ssh/id_rsa.pub remoteuser@xxx.xxx.xxx.xxx:<path-of-your-temporary-storage-directory>
          
        • 當然,你可以再開一個 Terminal 登入遠端,把目前的 id_rsa.pub 內容手動複製,但比較麻煩,因為這表示你還要設法手動貼上 id_rsa.pub 內容至遠端的 authorized_keys。複製方法參考下面 Github 方法
    • Github
      1. 複製 public key 的內容
        • 可以用 cat 印出檔案內容並手動複製,也可以開文字編輯器 (vscode, nano, vim, vi) 再手動複製
          ​​​​​​​​​​​​​​​​cat ~/.ssh/id_rsa.pub
          
        • 登入遠端主機,將剛才傳來的 id_rsa.pub 加入 authorized_keys
          1. 先確定遠端裡面有 ~/.ssh,若無,你要 mkdir 一個 .ssh dir.。放心若 .ssh 已存在,不加特殊 argument 時,此命令會報錯。
            ​​​​​​​​​​​​​​​​​​​​mkdir ~/.ssh
            
          2. 將剛才傳來的 id_rsa.pub 加入 authorized_keys
            ​​​​​​​​​​​​​​​​​​​​touch ~/.ssh/authorized_keys
            ​​​​​​​​​​​​​​​​​​​​chmod 600 ~/.ssh/authorized_keys
            ​​​​​​​​​​​​​​​​​​​​cd <path-of-your-temporary-storage-directory>
            ​​​​​​​​​​​​​​​​​​​​cat id_rsa.pub >> ~/.ssh/authorized_keys
            
      2. 登入 Github
        • 若想要開放所有 repository 存取權,進入於帳號的設定頁面,“SSH & GPG keys” 於 SSH keys 加入剛才複製的 public key 內容
        • 若只開放特定 repository 存取權: 於特定 repository 的 setting 頁面能找到類似的輸入框
  4. 恭喜設定完成
    可嘗試免密碼 ssh login 遠端 (或使用 git clone <ssh-link-copy-from-repository>)
    ​​​​ssh remoteuser@xxx.xxx.xxx.xxx uname -a