Skip to main content

Command Palette

Search for a command to run...

Manage multiple git accounts using SSH key authentication

Setting up and manage multiple git accounts as well as AWS CodeCommit using SSH key authentication

Published
4 min read
Manage multiple git accounts using SSH key authentication

Setting up and manage multiple git accounts as well as AWS CodeCommit using SSH key authentication

Introduction

You may have multiple git accounts, one for official and one for personal, or when you work on cloud like AWS you may have many repositories in different git accounts. At that time, You have to setup multiple usernames in the machine. But it is hard to change the settings every time.

You can solve the problem by setting the alias name for every git accounts. Yeah! Learn more on how to do that.

Generate SSH Key

First, we have to generate SSH key.For that, Open your gitbash terminal and navigate to required path/folder.Type the below command

ssh-keygen

Ssh-keygen is used to create authentication key pairs for SSH.Then provide the preferred file name for storing the key and press enter for remaining options.

$ ssh-keygen
Generating public/private rsa key pair.
Enter file in which to save the key (/c/Users/boopathikumar/.ssh/id_rsa): boopathi_rsa
Enter passphrase (empty for no passphrase):
Enter same passphrase again:
Your identification has been saved in boopathi_rsa.
Your public key has been saved in boopathi_rsa.pub.
The key fingerprint is:
SHA256:7Nr/m0cI+tw+RWUR4JF0slTzGCg78BDw+YhW9nUTN2s boopathikumar@LAPTOP-B9TJ4GJ
The keys randomart image is:
+---[RSA 3072]----+
|      ....  .B*B+|
|       .o.. +.==B|
|        =+ o.o+E.|
|       = +=. .o. |
|      o S.oo o   |
|     . ..   . o  |
|        .o . o   |
|       o  o o..  |
|      . ...o=+   |
+----[SHA256]-----+

Now You can see two keys created as below in the path you have navigated in gitbash.

boopathi_rsa
boopathi_rsa.pub

Adding generated SSH Key in Github

As a next step, You have to add SSH key into your Github account.Login to your Github account, navigate to settings page and click SSH and GPG Keys option and then press New SSH Key button.Provide a proper title and copy paste the content from boopathi_rsa file into the text box and save as shown below

github_profile.png

Once key is added, you can view the list of SSH keys in your accounts as below

github_profile_key_added.png

Adding SSH Key in AWS Code Commit

Now learn how to add the genrated SSH key into your AWS CodeCommit account.

Login to your AWS account and navigate to IAM service and open your IAM user. Under the Security Credentials tab you will find Upload SSH public key button. Click that and copy paste the content from boopathi_rsa file into the text box and save as shown below.

aws_ssh_key.png

Once key is added you can see the below screen.Copy the SSH key ID for later use in below steps.

aws_ssh_key_saved.png

Setup configuration file

Till now, We have generated a SSH Key and added it to git. Create a folder in the name of .ssh inside your user folder.

Inside the .ssh folder, create a file in the name config with no extension. Now you will have the folder structure like below.

C:\Users\<your-username>\.ssh\config

The content of the config file should be added in the below format.

Host my.github.com
    HostName github.com
    PreferredAuthentications publickey
    IdentityFile "C:/Users/boopathikumar/.ssh/boopathi_rsa"

Host office.github.com
    HostName github.com
    PreferredAuthentications publickey
    IdentityFile "C:/Users/boopathikumar/.ssh/boopathi_rsa2"

Host my_codecommit
    Hostname git-codecommit.us-east-1.amazonaws.com (only access to repo in us-east-1)
    User  APKAVDUCNF4JYDGHGTNQ (copied SSH key ID)
    PreferredAuthentications publickey
    IdentityFile "C:/Users/CloudKinetics/.ssh/boopathi_rsa"

Host office_codecommit
    Hostname git-codecommit.*.amazonaws.com (to access all region)
    User  XXXXXXXXXXXXXXXXXXXX  
    PreferredAuthentications publickey
    IdentityFile "C:/Users/CloudKinetics/.ssh/boopathi_rsa"

In the above configuration, meaning of each term is shown below

  • Host - Provide a alias name for your git account. This will be used instead of hostname.
  • Hostname - Actual git account hostname
  • User - This is needed only for AWS code commit
  • PreferredAuthentications - To authorize with SSH key, Provide the value as publickey
  • IdentityFile - Path of you pem key

Now all the configurations are completed. You can clone the repository from any account.

Clone the Repository

Now I am trying to clone the private repository from my personal github account.For that, get the SSH clone URL of the repository as show below

git_clone.png

The SSH Clone URL looks like the below

git clone git@github.com:boopathikumar018/sample_repo.git

In that,Change the host name (github.com) to configured Alias host name (my.github.com) now the you URL will be like

git clone git@my.github.com:boopathikumar018/sample_repo.git

Once you execute the clone command, repository will be automatically cloned using the credentials provided under the Alias name in config file

Now I am trying to clone the AWS code commit repository from my AWS account.For that get the SSH Clone URL of the repository as show below

aws_clone.png

you will get a clone SSH URL of the repository like below

git clone ssh://git-codecommit.us-east-1.amazonaws.com/v1/repos/sample-repo

In this also, Change the host name (git-codecommit.us-east-1.amazonaws.com) to configured Alias name (my_codecommit) now the you URL will be like

git clone ssh://my_codecommit/v1/repos/sample-repo

This will automatically clone the repo using the credentials configured under the Alias name in the config file

Like this, You can configure as many git account as you want and use easily by just changing to Alias name.