Try   HackMD

產生 ssh key 和將 ssh key 加入 gitlab 或其他平台

新增產生 shh key 及平台增加公鑰 ssh key generated & add to gitlab or other
設定本機私鑰 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

  1. Open Git Bash or Terminal.
  2. Run the following command:
ssh-keygen -t ed25519 -C "<email address>"
  1. Press Enter. Output similar to the following is displayed:
Generating public/private ed25519 key pair.
Enter file in which to save the key (/home/user/.ssh/id_ed25519):
  1. Next all press Enter to accept the default location and file name.
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

tr -d '\n' < ~/.ssh/id_ed25519.pub | pbcopy

b. Linux (requies the xclip package)

xclip -sel clip < ~/.ssh/id_ed25519.pub

c. Git Bash on Windows

cat ~/.ssh/id_ed25519.pub | clip
  1. Sign in to GitLab.
    登入你的 gitlab
  2. On the left sidebar, select your avatar.
    尋找側邊欄,或任一位置的頭像
  3. Select Edit profile.
    點擊頭像後,尋找編輯設定
  4. On the left sidebar, select SSH Keys.
    進入設定,側邊欄會出現 SSH Keys
  5. Select Add public key.
    選擇後新增 public key

Configure ssh key

unix 有權限問題,參考以下指令

chmod +x <directory to private SSH key>

Configure

  1. Run the following command:
    unix 需要使用該指令,將 ssh-agent 設定至環境變數。
eval $(ssh-agent -s)
  1. ssh-add adds private key identities to the authentication agent.
ssh-add <directory to private SSH key>
  1. Save these settings in the ~/.ssh/config file. For example:
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:

git clone git@<user_1.gitlab.com>:gitlab-org/gitlab.git

To update a previously-cloned repository that is aliased as origin:

git remote set-url origin git@<user_1.gitlab.com>:gitlab-org/gitlab.git

Use different accounts on a single GitLab instance

go get different key
Windows SET GIT_SSH_COMMAND=ssh -i C:\path\to\your\key

Account Identity

Verify that you can connect to GitLab
驗證你的帳戶是否可連接

ssh -T git@gitlab.com

成功後,你應該會接收到該訊息 Welcome to GitLab, @username! 後面為你的 username

失敗後,可以使用以下指令看到更資訊除錯

ssh -Tvvv git@gitlab.com

Use SSH on Microsoft Windows

  • Git for Windows: C:\Users<user>

參考

https://docs.gitlab.com/ee/user/ssh.html