概要
GitLab や GitHub のアカウントが複数ある場合(例:仕事用アカウント、個人開発用アカウント)、それぞれのアカウントに対して、 プルやプッシュ、クローンの時に git@gitlab.com:username/prj-name.git
などのSSH 接続を行う場合は、SSH 鍵の登録をそれぞれ別の鍵で行う必要がある。
それには、SSH の Config ファイルを活用する。
その設定方法と、使用方法についての備忘録。
前提
- 今回は「GitLab」の例で記載する(GitHubはうまく読み替えてください)
- GitLab/GitHub の有効なアカウントを持っていること
- 以下のコマンドが実行可能な PC であること
-
ssh-keygen
,touch
,vi
(vim
) ,chmod
,cat
-
手順
全体的な手順は以下の通り
- SSH 鍵の生成
- 設定ファイルを作成
- GitLab に登録
- Git コマンドを使用する
SSH 鍵の生成
ssh-keygen
コマンドで公開鍵と秘密鍵を作成
その際、「C」オプションを指定して、GitLab アカウントに使用しているメールアドレスを指定
# ssh の隠しフォルダに移動
cd ~/.ssh
# ssh 鍵生成 (メールアドレスは GitLab/GitHub アカウントのもの)
ssh-keygen -t rsa -C "example_01@gmail.com"
# 実行結果
Generating public/private rsa key pair.
# 途中入力受付
Enter file in which to save the key ({ルートパス}/.ssh/id_rsa): {ルートパス}/.ssh/id_rsa_hoge # ◀︎◀︎◀︎ メアド別にファイル名を分けたいので、パスをそのまま入力し、わかりやすい任意のファイル名をつけて Enter キー
Enter passphrase (empty for no passphrase): # ◀︎◀︎◀︎ 何も入力せず Enter キー
Enter same passphrase again: # ◀︎◀︎◀︎ 何も入力せず Enter キー
# 以降、自動実行
Your identification has been saved in {ルートパス}/.ssh/id_rsa_hoge.
Your public key has been saved in {ルートパス}/.ssh/id_rsa_hoge.pub.
The key fingerprint is:
SHA256:xxxxxxxxxxxxxxxx example_01@gmail.com
The key's randomart image is:
+---[RSA 3072]----+
| |
| |
| + . |
| xxxxxx |
| xxxxxxxx |
| xxxxxxxxxx |
| xxxxxxxx |
| xxxxxxxxx |
| . xxxxxxxxx|
+----[SHA256]-----+
鍵の生成を確認
# 確認コマンド
tommy@ChihironoiMac-Pro .ssh % ls -l
total 40
-rw------- 1 user group 2610 7 13 13:44 id_rsa_hoge # ◀︎◀︎◀︎ 新しい秘密鍵
-rw-r--r-- 1 user group 573 7 13 13:44 id_rsa_hoge.pub # ◀︎◀︎◀︎ 新しい公開鍵
-rw-r--r-- 1 user group 186 4 9 04:47 known_hosts
別アカウントの SSH 鍵も作っておく
# もう一度 ssh 鍵生成 (別のメールアドレス)
ssh-keygen -t rsa -C "example_02@gmail.com"
# 実行結果
Generating public/private rsa key pair.
# 途中入力受付
Enter file in which to save the key ({ルートパス}/.ssh/id_rsa): {ルートパス}/.ssh/id_rsa_fuga # ◀︎◀︎◀︎ 別のファイル名をつけて Enter キー
# (以下、省略)
鍵の生成を確認
# 確認コマンド
tommy@ChihironoiMac-Pro .ssh % ls -l
# 実行結果
-rw------- 1 user group 2610 7 13 13:44 id_rsa_hoge
-rw-r--r-- 1 user group 573 7 13 13:44 id_rsa_hoge.pub
-rw------- 1 user group 2610 7 13 13:49 id_rsa_fuga # ◀︎◀︎◀︎ 今回の新しい秘密鍵
-rw-r--r-- 1 user group 573 7 13 13:49 id_rsa_fuga.pub # ◀︎◀︎◀︎ 今回の新しい公開鍵
-rw-r--r-- 1 user group 186 4 9 04:47 known_hosts
設定ファイルの作成
SSH 鍵を用意しただけでは、例え、公開鍵を GitLab/GitHub に用意しても利用できません。
ここで config ファイルを用意します。
# config ファイル作成
touch config
config ファイルは、ユーザ以外に「書き込み」権限以上があってはいけないので、念のため
パーミッション (権限) の確認
# 確認
ls -l
# 実行結果 (一番左の下6桁に、"w" が無いことを確認)
-rw-r--r-- 1 user group 0 7 13 15:04 config
(補足)パーミッションを変更する場合は、以下の様に変更する
# パーミッションを 644 (ユーザー/読み書き可, 他/読み取りのみ可) に変更
chmod 644 config
config に鍵の設定を追記する
以下のコマンドで、config ファイルの編集開始
vi ./config
編集内容は以下
# Personal GitLab account
Host hoge # ◀︎◀︎◀︎ Host {任意のホスト名}
HostName gitlab.com # ◀︎◀︎◀︎ GitLab/GitHub のドメイン
User git # ◀︎◀︎◀︎ Git 操作で使うなら "git"
AddKeysToAgent yes # ◀︎◀︎◀︎ 固定値
IdentityFile ~/.ssh/id_rsa_hoge # ◀︎◀︎◀︎ 先ほど作成した秘密鍵へのパス
# Work GitLab account
Host fuga
HostName gitlab.com
User git
AddKeysToAgent yes
IdentityFile ~/.ssh/id_rsa_fuga
GitLab に登録する
まず、GitLab のサインインし、SSH 鍵の管理画面に行きます。
プロフィールアイコン > 「Edit Profile」 > メニュー「SSH 鍵」、で開く
上記のページの「キー(key)」の空欄に、先ほどの SSH 鍵の「公開鍵」中身を表示して、 GitLab に登録します。
「タイトル」の部分は、"誰の何のマシン(PC/サーバ)か" がわかりやすい任意の名前をつけます。「有効期限 (Expires at)」は設定不要。
※ アカウントに使用していない方のメールアドレスの鍵と間違えないように注意
※ 「秘密鍵」の方を登録しないように注意
# 中身を確認するコマンド
cat id_rsa_hoge.pub # ◀︎◀︎◀︎ (注意) .pub の方を選ぶこと
# 実行結果 (↓↓↓これをコピペする)
ssh-rsa AAAAB3...(中略)...xxx= mychips4u@gmail.com
登録完了したら以下の様になります。
Git コマンドを使用する
通常 Git コマンドを使用する際は、 git@gitlab.com:{ユーザ名}/{プロジェクト名}.git
としますが、コロン前の 「gitlab.com
」部分を、 config に設定したホスト名 (hoge, fuga など) に置き換えて使用します。
通常の GitLab のクローンコマンド (例)
git clone git@gitlab.com:user-name/prj-name.git
config 設定後の GitLab のクローンコマンド (例)
git clone git@hoge:user-name/prj-name.git
VScode 等のエディタの機能でボタンで Git 操作を行う場合、ボタンプッシュで自動処理される時の .gitconfig 設定などが正常に動作する内容になっているかご注意ください。