產生 ssh key 和將 ssh key 加入 gitlab 或其他平台 [TOC] > 新增產生 shh key 及平台增加公鑰 [ssh key generated & add to gitlab or other](https://docs.gitlab.com/ee/user/ssh.html#see-if-you-have-an-existing-ssh-key-pair)\ > 設定本機私鑰 [Configure ssh key](https:// "Configure ssh key") ## ssh key generated & add to gitlab or other 產生 ssh key 和將 ssh key 加入 gitlab 或其他平台 > 可協助其他人產生 ssh key > 區分為 public 和 private key\ > public 新增上版控平台,private key 給予使用者 ### Generate a new SSH Key 產生新的 ssh key 使用 ed25519 > 預使用其他算法,可至平台查詢 \ > [See if you have an existing SSH key pair](https://docs.gitlab.com/ee/user/ssh.html#see-if-you-have-an-existing-ssh-key-pair) 1. Open Git Bash or Terminal. 2. Run the following command: ```shell ssh-keygen -t ed25519 -C "<email address>" ``` 3. Press Enter. Output similar to the following is displayed: ```shell Generating public/private ed25519 key pair. Enter file in which to save the key (/home/user/.ssh/id_ed25519): ``` 4. Next all press Enter to accept the default location and file name. ```shell Enter passphrase (empty for no passphrase): Enter same passphrase again: ``` ### Add an SSH key to your GitLab account 將 public key 新增至平台,以下 gitlab 舉例 To use SSH with GitLab, copy your public key to your GitLab account: 1. Copy the contents of your public key file. You can do this manually or use a script. For example, to copy an ED25519 key to the clipboard:\ > 複製 public key \ a. mac ```shell tr -d '\n' < ~/.ssh/id_ed25519.pub | pbcopy ``` b. Linux (requies the xclip package) ```shell xclip -sel clip < ~/.ssh/id_ed25519.pub ``` c. Git Bash on Windows ```shell cat ~/.ssh/id_ed25519.pub | clip ``` 2. Sign in to GitLab. \ 登入你的 gitlab 3. On the left sidebar, select your avatar.\ 尋找側邊欄,或任一位置的頭像 4. Select Edit profile.\ 點擊頭像後,尋找編輯設定 5. On the left sidebar, select SSH Keys.\ 進入設定,側邊欄會出現 SSH Keys 6. Select Add public key. \ 選擇後新增 public key ## Configure ssh key > unix 有權限問題,參考以下指令 ```shell chmod +x <directory to private SSH key> ``` ### Configure 1. Run the following command: \ unix 需要使用該指令,將 ssh-agent 設定至環境變數。 ```shell eval $(ssh-agent -s) ``` 2. ssh-add adds private key identities to the authentication agent. ```shell ssh-add <directory to private SSH key> ``` 3. Save these settings in the ~/.ssh/config file. For example: ```shell vim ~/.ssh/config ``` ``` ## 請將路徑更改為私鑰 # User1 Account Identity Host <user_1>.gitlab.com HostName gitlab.com PreferredAuthentications publickey StrictHostKeyChecking no IdentityFile ~/.ssh/<example_ssh_key1> ## 假設你有多個帳號不同 key,請將 Host 做異動 # User2 Account Identity Host <user_2>.gitlab.com HostName gitlab.com PreferredAuthentications publickey StrictHostKeyChecking no IdentityFile ~/.ssh/<example_ssh_key> ``` Now, to clone a repository for `user_1`, use `user_1.gitlab.com` in the `git clone` command: ```shell git clone git@<user_1.gitlab.com>:gitlab-org/gitlab.git ``` To update a previously-cloned repository that is aliased as `origin`: ```shell git remote set-url origin git@<user_1.gitlab.com>:gitlab-org/gitlab.git ``` > [Use different accounts on a single GitLab instance](https://docs.gitlab.com/ee/user/ssh.html#use-different-accounts-on-a-single-gitlab-instance) > [go get different key](https://stackoverflow.com/questions/65569280/how-can-you-specify-which-ssh-key-go-get-will-use/65569281#65569281) \ > Windows >`SET GIT_SSH_COMMAND=ssh -i C:\path\to\your\key` ### Account Identity > Verify that you can connect to GitLab \ > 驗證你的帳戶是否可連接 ```shell ssh -T git@gitlab.com ``` 成功後,你應該會接收到該訊息 `Welcome to GitLab, @username!` 後面為你的 username > 失敗後,可以使用以下指令看到更資訊除錯 ```shell ssh -Tvvv git@gitlab.com ``` ## Use SSH on Microsoft Windows * Git for Windows: C:\Users\<user> --- ## 參考 https://docs.gitlab.com/ee/user/ssh.html