githubとgitlabにsshアクセス設定するおさらい
過去にbitbucketでsshアクセスする記事を書いて、最近、新しい
ノートパソコンにgithubとgitlab用の設定をしたのだがブログに
記事をまとめてなかったことに気づいたw(ΦωΦ;)
それで、ノートにまとめた内容を備忘録にまとめてみる。
[手順]
1. ssh鍵を作成。
使用したいアカウント毎に以下コマンドを実行し、鍵ペアを
作成。
% mkdir .ssh
% cd ~/.ssh
% ssh-keygen -t rsa -C {github(gitlab)email-address} -f {作成する鍵の名前}
※パスフレーズを指定する場合、任意の文字列を2回入力する。
2. ssh設定ファイル(~/.ssh/config)の編集。
アカウント毎にssh鍵を使い分けるため、ssh設定ファイルにHost(接続元)
の登録を行う。以下の通り、書き換える。
(ex) github
Host haiju.github.com --- 任意の名前
User git
Port 22
Hostname github
IdentityFile ~/.ssh/id_rsa_github --- ssh-keygenの-fオプション後ろ
の文字列と同じ
TCPKeepAlive yes
IdentitiesOnly yes
(ex)gitlab
Host gitlab-haiju.com --- 任意の名前
User git
Port 22
HostName gitlab.com
IdentityFile ~/.ssh/config/id_rsa_gitlab
TCPKeepAlive yes
IdentitiesOnly yes
3. github, gitlabに公開鍵を設定
(ex)github
githubにログインし、[Settings] - [SSH keys] - [Add SSH key]
で作成した公開鍵(拡張子が.pub) を登録。
4. sshアクセスの接続確認。
(ex)github
% ssh -T git@github-haiju.com ---- configファイルのHostと一致
(ex)gitlab
% ssh -T git@gitlab-haiju.com ---- 同上
※クローンする場合もリモートリポジトリをローカルに登録する場合も、
git@github.com としているところをgit@github-haiju.comに書き換える。
=> ~.ssh/configファイルのHostと一致していることを確認。
(ex)リモートリポジトリ登録の基本書式
% git remote add origin git@[HostName]:[accountName]/[repositoryName].git
% git remote add origin git@github-haiju.com: haiju/repositoryName.git
(ex)git clone実行時の基本書式
% git clone git@[HostName]: [accountName]/[repositoryName].git
% git clone git@github-haiju.com:haiju/repositoryName.git
5. gitのURLが正しく設定されているか確認。
% git config remote.origin.url
git@github(gitlab)-haiju.comの@以降の部分が、~/.ssh/config
に設定したHostNameと一致していればOK。
[参考サイト]
GitLabやGitHubで複数アカウントを持つときのssh設定-『Git』 | webmanab.html/ウェブまなぶ