Closed2

GitHubとBitbucketのSSHキー設定共存させるやつ

unsoluble_sugarunsoluble_sugar

Bitbucketリポジトリのクローンが何故かできん!!
と思ったらSSHキーの設定とかしてないマシンだった件

% git clone git@bitbucket.org:hogehogeinc/xxx-yyy-zzz.git
Cloning into 'xxx-yyy-zzz'...
The authenticity of host 'bitbucket.org (104.192.141.1)' can't be established.
RSA key fingerprint is SHA256:xxxxxxxxxxxxxxxxxxxx.
Are you sure you want to continue connecting (yes/no/[fingerprint])? yes
Warning: Permanently added 'bitbucket.org,104.192.141.1' (RSA) to the list of known hosts.
git@bitbucket.org: Permission denied (publickey).
fatal: Could not read from remote repository.

Please make sure you have the correct access rights
and the repository exists.

GitHubは設定済みなのでconfigで両方のアカウントを共存させるための手順メモ
環境はMacでzsh

BitbucketのSSH作成

ターミナルで以下実行
% ssh-keygen -t rsa -C hoge@fugo.com -f ~/.ssh/id_rsa_bitbucket

  • hoge@fugo.com 部分は対象アカウントのメールアドレス
  • パスフレーズ入力(エンターキー設定せずエンターキー押下でもおk)
  • パスフレーズ確認(同様)

id_rsa_bitbucket.pub が作成されていることを確認する。

% ssh-keygen -t rsa -C hoge@fugo.com -f ~/.ssh/id_rsa_bitbucket
Generating public/private rsa key pair.
Enter passphrase (empty for no passphrase): 
Enter same passphrase again: 
Your identification has been saved in /Users/unsoluble_sugar/.ssh/id_rsa_bitbucket.
Your public key has been saved in /Users/unsoluble_sugar/.ssh/id_rsa_bitbucket.pub.
The key fingerprint is:
SHA256:xxxxxxxxxxxxxxxxxxxxxxxxxxxxx hoge@fugo.com
The key's randomart image is:
+---[RSA 3072]----+
...

SSHキーを登録

作成したキーの中身をコピー

.pubの方のrsaキーをコピー

% cat ~/.ssh/id_rsa_bitbucket.pub | pbcopy

Bitbucketに登録


.ssh/configにBitbucketの設定追加

Host *
  AddKeysToAgent yes
  UseKeychain yes

Host github
  HostName github.com
  IdentityFile ~/.ssh/id_rsa
  Port 22
  User git

Host bitbucket.org
  User git
  HostName bitbucket.org
  IdentityFile ~/.ssh/id_rsa_bitbucket
  IdentitiesOnly yes

接続確認

% ssh -T git@bitbucket.org
logged in as unsoluble_sugar

You can use git to connect to Bitbucket. Shell access is disabled
このスクラップは2021/03/31にクローズされました