2
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 1 year has passed since last update.

SSHの鍵を複数管理するメモ

Last updated at Posted at 2024-04-28

背景・目的

複数のSSHキーを管理する機会があったので、メモします
【GitHub】複数GitHubアカウントのSSHキーを管理する。を参考に試してみます。助かります。ありがとうございます。

実践

鍵の作成

  1. 1つ目の鍵を作成します

    ssh-keygen -t rsa -b 4096 -C "mailadress" -f "id_rsa_abc"
    
  2. 2つ目の鍵を作成します

    ssh-keygen -t rsa -b 4096 -C "mailadress" -f "id_rsa_xyz"
    

ssh configの修正

  1. ~/.ssh/configを修正します
    Host github.com.abc
      HostName github.com
      User git
      Port 22
      IdentityFile ~/.ssh/id_rsa_abc #1つ目のssh鍵を指定
      TCPKeepAlive yes
      IdentitiesOnly yes
    
    Host github.com.xyz
      HostName github.com
      User git
      Port 22
      IdentityFile ~/.ssh/id_rsa_github_xyz # #2つ目のssh鍵を指定
      TCPKeepAlive yes
      IdentitiesOnly yes
    

公開鍵の設定

  1. 接続先であるリモート側の鍵をコピーして設定します。 pbcopyによりクリップボードにコピーできます
    pbcopy < ~/.ssh/id_rsa_abc.pub
    

疎通

  1. 1つ目の確認

    ssh -T git@github.com.abc
    Hi XXXXX! You've successfully authenticated, but GitHub does not provide shell access.
    
  2. 2つ目の確認

    ssh -T git@github.com.xyz
    Hi XXXXX! You've successfully authenticated, but GitHub does not provide shell access.
    

git cloneする場合

  1. 下記の{}内をConfig内で設定した内容に書き換えます
    git clone git@github.com.{abc}:username/repository-name.git
    

参考

今回、複数のSSH鍵を管理する方法をまとめてみました。

2
1
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
2
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?