# [GitHub] 設定 GitHub SSH 連線 ###### tags: `GitHub` ## 檢查是否有任何 SSH key 1. 在終端機輸入 `ls -al ~/.ssh` 2. 若有可用的 SSH key,執行 [將 SSH key 加到 GitHub account](#將-SSH-key-加到-GitHub-account) 3. 若無適合的檔案或看到 `No such file or directory` 之類的訊息(代表沒有任何 SSH key),執行 [產生新的 SSH key](#產生新的-SSH-key) ## 產生新的 SSH key 1. 在終端機輸入 `ssh-keygen -t ed25519 -C "你的 GitHub email"` 2. 會出現確認訊息,若不打算變更檔案儲存的位置或名稱,直接按 `Enter` 鍵 * 若要修改,輸入要儲存的位置及檔名,例:`/Users/xxxx/.ssh/github_xxxx_ed25519` ```shell Generating public/private ed25519 key pair. Enter file in which to save the key (/Users/xxxx/.ssh/id_ed25519): ``` 4. 設定密碼,若不打算設定密碼,不輸入任何字直接按 `Enter` 鍵 * 若有設定密碼,但不想每次使用時都要輸入密碼,可考慮將 key 加到 SSH agent,詳細步驟可參考 [GitHub Docs - Adding your SSH key to the ssh-agent](https://docs.github.com/en/authentication/connecting-to-github-with-ssh/generating-a-new-ssh-key-and-adding-it-to-the-ssh-agent#adding-your-ssh-key-to-the-ssh-agent) ```shell # 若沒有 .ssh 資料夾,會自動創建 Created directory '/Users/xxxx/.ssh'. Enter passphrase (empty for no passphrase): Enter same passphrase again: ``` 6. 產生 SSH key ```shell Your identification has been saved in /Users/xxxx/.ssh/id_ed25519 Your public key has been saved in /Users/xxxx/.ssh/id_ed25519.pub The key fingerprint is: SHA256:xxxxxxxxxxxxxxxxxxxxxxxxxxxxxx ``` ## 將 SSH key 加到 GitHub account 網址:[GitHub SSH and GPG keys](https://github.com/settings/keys) 1. 點 `New SSH Key` 按鈕 2. 輸入 `Title` 內容 3. 將公鑰 `.pub` 內容複製貼到 `Key` 欄位 4. 點 `Add SSH Key` 按鈕 ## 測試和 GitHub 的 SSH 連線 1. 在終端機輸入以下內容`ssh -T git@github.com` ### 失敗 #### 失敗訊息一 ```shell The authenticity of host 'github.com (IP ADDRESS)' can't be established. ED25519 key fingerprint is SHA256:+DiY3wvvV6TuJJhbpZisF/zLDA0zPMSvHdkr4UvCOqU. Are you sure you want to continue connecting (yes/no)? ``` * 檢查訊息中的 fingerprint 是否和 [GitHub's public key fingerprint](https://docs.github.com/en/authentication/keeping-your-account-and-data-secure/githubs-ssh-key-fingerprints) 匹配,是的話輸入 `yes` #### 失敗訊息二 ```shell git@github.com: Permission denied (publickey). ``` * 檢查是否有文件中提到的狀況 [Error: Permission denied (publickey) - GitHub Docs](https://docs.github.com/en/authentication/troubleshooting-ssh/error-permission-denied-publickey) * 若有變更 SSH key 檔名 * GitHub 支援的公鑰的檔名為 `id_rsa.pub` 或 `id_ecdsa.pub` 或 `id_ed25519.pub`,若檔名不同,要 [設定 config](#設定-config) * 設定 config 後,測試連線的指令要改成 `ssh -T git@設定的Host alias`,例:`ssh -T git@github-xxxx` ### 成功 #### 成功訊息 ```shell Hi USERNAME! You've successfully authenticated, but GitHub does not provide shell access. ``` ## 設定 config 在 `~/.ssh` 資料夾內新增 `config` 檔案,範例如下 ``` # Host alias # HostName domain 或 ip # Port port # IdentitiesOnly 指定key,值: yes # IdentityFile 指定的 key 路徑和檔名 # User 登入的 username # GitHub xxxx Host github-xxxx HostName github.com IdentitiesOnly yes IdentityFile ~/.ssh/github_xxxx_ed25519 ``` * 若有自訂的 Host 名稱,clone repository 時不能直接使用 `GitHub` > `Clone` > `SSH` 提供的 URL * 例:`git@github.com:帳號/repository-name.git` 要改成 `git@github-xxxx:帳號/repository-name.git` ## 參考資料 * [Checking for existing SSH keys - GitHub Docs](https://docs.github.com/en/authentication/connecting-to-github-with-ssh/checking-for-existing-ssh-keys) * [Generating a new SSH key and adding it to the ssh-agent - GitHub Docs](https://docs.github.com/en/authentication/connecting-to-github-with-ssh/generating-a-new-ssh-key-and-adding-it-to-the-ssh-agent) * [Adding a new SSH key to your GitHub account - GitHub Docs](https://docs.github.com/en/authentication/connecting-to-github-with-ssh/adding-a-new-ssh-key-to-your-github-account) * [Testing your SSH connection - GitHub Docs](https://docs.github.com/en/authentication/connecting-to-github-with-ssh/testing-your-ssh-connection) * [【Git】使用 SSH 金鑰與 GitHub 連線 by 辛西亞.Cynthia](https://cynthiachuang.github.io/Generating-a-Ssh-Key-and-Adding-It-to-the-Github/) --- :::info 建立日期:2024-01-21 更新日期:2024-01-22 :::