LoginSignup
0
2

More than 5 years have passed since last update.

GithubとBitbucketに別々にSSH鍵を設定

Last updated at Posted at 2018-06-19

 SSHを使わない時はどうしていたか

PCによってはSSHを設定していたりしたが、メモをきちんと残していなかったので端末によっては
netrcを設定したりしていた。

netrc使ってるとgithubとbitbucketでアカウント分けるのが面倒ということに気づいたのでSSH設定の手順を残します。

 鍵を作成する手順

SSH鍵を作る手順は以下の通り。このサイトの記述が丁寧。以下のコマンドを実行することで鍵を作成可能。

cd ~/.ssh
ssh-keygen -t rsa -C test@example.com

この際帰ってくるメッセージは次の通り。何も考えずにEnterを3回押せばid_rsaとid_rsa.pubという秘密鍵、公開鍵の2つができる。
以下では最初のメッセージで'bitbucket'とうってbitbucket用の鍵を生成している。

Generating public/private rsa key pair.
Enter file in which to save the key (/home/****/.ssh/id_rsa): bitbucket
Enter passphrase (empty for no passphrase): 
Enter same passphrase again: 
Your identification has been saved in bitbucket.
Your public key has been saved in bitbucket.pub.
...

chmod 600 bitbucketとうって所有者のみが変更可能にすると良いらしい。chmodのPermissionについてはこちら。 

 クライアント側の設定

~/.ssh/configを開き、次のような記述を追加する。
私の場合、bitbucketとgithubのように鍵に名前をつけているのでIdentityFileの箇所は適当に読み替えて書いてほしい。

Host bitbucket.org
  HostName bitbucket.org
  IdentityFile ~/.ssh/bitbucket
  User git

Host github
  HostName github.com
  IdentityFile ~/.ssh/github
  User git

 Github、Bitbucket側の設定

アカウント情報からSSH鍵の箇所をひらき、.pubの方を登録する
図説はこのサイトに任せる。
ちょっと表示が古いがなんとかしてやってください。

 接続確認

GithubとのSSH確認は以下の通り。

ssh -T git@github.com

ホントに繋いでOK?的なメッセージが出るが許可する。

The authenticity of host 'github.com (192.30.255.112)' can't be established.
RSA key fingerprint is <XXXX>.
Are you sure you want to continue connecting (yes/no)? yes
Warning: Permanently added 'github.com,192.30.255.112' (RSA) to the list of known hosts.
Hi <あなたの名前>! You've successfully authenticated, but GitHub does not provide shell access.

Bitbucketの場合は次のコマンドを実行して確認。

ssh -T git@bitbucket.org

 SSHへの移行

HTTPでCloneしたファイルはSSHでpushしたりできないので、移行が必要になる。

このサイトが丁寧。

一旦remoteを確認し

$ git remote -v
  origin  https://github.com/USERNAME/REPOSITORY.git (fetch)
  origin  https://github.com/USERNAME/REPOSITORY.git (push)

次のように変更。

$ git remote set-url origin git@github.com:USERNAME/REPOSITORY.git
0
2
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
2