つのマシンで複数のSSHキーを追加する



写真でAnton Darius on Unsplash
このポストでは、1つのマシンで1つ以上のsshキーを追加する方法を説明します.私はGiHubのためにそれを説明しますが、手順はそこに任意のGitプロバイダのために適用することができます.

sshとは


SSH or Secure Shell is a network communication protocol that enables two computers to communicate (c.f http or hypertext transfer protocol, which is the protocol used to transfer hypertext such as web pages) and share data. An inherent feature of ssh is that the communication between the two computers is encrypted meaning that it is suitable for use on insecure networks.


SSHはしばしば「ログイン」に使用されて、リモートコンピュータで操作を実行するが、それはまたデータを転送するのに使用されるかもしれません.

どのように仕事ですか?


SSHキーを設定すると、プライベートキー(ローカルコンピューターに保存)とパブリックキー(GitHub、GitLabまたはBitbucketのようなGitプロバイダーにアップロード)が含まれているキーペアを作成します.Gitプロバイダーは関連するアカウントがアクセスできる何かを認証するためにキーペアを使用します.この双方向メカニズムは、中間の攻撃で人間を防ぎます.

どのようにセットアップを1つのマシンで?


簡単な手順で😊.

ビューが存在sshキー


$ ls -al ~/.ssh

新規sshキーの生成


このコマンドを実行した後、2つのファイル公開/秘密キーを生成します
$ cd ~/.ssh
$ ssh-keygen -t rsa -f "id_rsa_personal_github"    # for personal account
$ ssh-keygen -t rsa -f "id_rsa_work_github"         # for work account

新しいsshキーを対応する(github、bitbacktまたはgitlab)アカウントに追加する

  • コピーされたキーをクリップボードにコピーする
  • $ clip < ~/.ssh/id_rsa_personal_github.pub          # for github account
    $ clip < ~/.ssh/id_rsa_work_github.pub               # for gitlab account
    
  • Githubアカウントにクリップボードを追加します.
  • 設定に移動
  • メニューから左にsshとgpgキーを選択します.
  • 新しいSSHキーをクリックし、適切なタイトルを提供し、下のボックスにキーを貼り付けます
  • をクリックします — そして、あなたは完了です!
  • SSHエージェントを使用した新しいsshキーの登録


    # start the ssh-agent in the background
    $ eval $(ssh-agent -s)
    Agent pid 59566
    
    $ ssh-add ~/.ssh/id_rsa_personal_github    # for personal account
    $ ssh-add ~/.ssh/id_rsa_work_github        # for work account
    

    SSH設定ファイルの作成


    このファイルを使用して、マシンにインストールされているgitに、上流にプッシュするときに使用するキーを指定します.
    cd ~/.ssh/
    $ touch config           # Create the file if not exists
    $ code config            # Open the file in VS code or use any editor
    

    config file


    # Personal GitHub Account
    Host github.com
       HostName github.com
       User git
       IdentityFile ~/.ssh/id_rsa_personal_github
    # Work GitLab Account
    Host gitLab.com    
       HostName gitLab.com 
       User git
       IdentityFile ~/.ssh/id_rsa_work_github
    

    資源

  • Setup SSH Key - GitHub
  • Setup SSH Key - bitbucket
  • Setup SSH Key - Gitlab