Mac 简单配置不同 host 的 ssh-key

1. 生成新 SSH 密鑰

ssh-keygen -t id_ed25519 -C "your_email@example.com"

如果不命名,默認名是 id_ed25519,一路回車。

如果你用的是舊系統,可以使用 rsa 替代 ed25519

這時候會自動創建 .ssh 文件夾。

2. 進入文件夾

cd ~/.ssh

3. 將新的 ssh-key 添加到 ssh-agent

ssh-add ~/.ssh/id_ed25519

4. 保存公鑰到托管服務,譬如 GitHub

pbcopy < ~/.ssh/id_ed25519.pub

粘貼到 Github 的這個位置:

頭像 -> Settings -> SSH and GPG keys -> New SSH key

5. 在不同的 host 中配置

vi config

點擊鍵盤 i 鍵入以下資料

# github
Host github.com
  HostName github.com
  PreferredAuthentications publickey
  IdentityFile ~/.ssh/id_ed25519
# bitbucket
Host bitbucket.org
  HostName bitbucket.org
  PreferredAuthentications publickey
  IdentityFile ~/.ssh/bitbucket_ id_ed25519

esc 編輯模式,鍵入 :wq 保存。

6. 測試一下

ssh -T git@github.com

如果顯示下面的提示就是成功了,如果沒有可能就要看一下上面的步驟有沒有出錯

Hi <user_name>! You've successfully authenticated, but GitHub does not provide shell access.

祝編程愉悅!