Multiple SSH Keys settings for different git account

Kingyinma
1 min readJun 24, 2021

In terminal, generate ssh key

ssh-keygen -t rsa -C "your_username@your_email.com@bitbucket"

Will ask you file name to for generate this ssh key, and this example we are going to generate ssh for different git, e.g. bitbucket and github which using different ssh key

$ ssh-keygen -t rsa -C "your_username@your_email.com@github1"
$ ssh-keygen -t rsa -C "your_username@your_email.com@github2"

Next, setup your ssh config file, which located in your ~/.ssh folder

$ mkdir .ssh
$ cd .ssh
$ touch config

Here is an example of the config file

# GitHub michaelmaky
Host github-michaelmaky
HostName github.com
IdentityFile ~/.ssh/id_rsa_github_michaelmaky
# GitHub michaelmaky
Host github-michaelmaky2
HostName github.com
IdentityFile ~/.ssh/id_rsa_github_michaelmaky2
# bitbucket yourUserName1
Host bitbucket.org-yourUserName1
HostName bitbucket.org
IdentityFile ~/.ssh/id_rsa_bitbucket_yourUserName1
# bitbucket yourUserName2
Host bitbucket.org-yourUserName2
HostName bitbucket.org
IdentityFile ~/.ssh/id_rsa_bitbucket_yourUserName2
# gitlab yourUserName1
Host gitlab.com-yourUserName1
HostName gitlab.com
IdentityFile ~/.ssh/id_rsa_gitlab_yourUserName1
# gitlab yourUserName2
Host gitlab.com-yourUserName2
HostName gitlab.com
IdentityFile ~/.ssh/id_rsa_gitlab_yourUserName2

For clone your existing repository, use command below

git clone git@{host_name_or_alias}:{workspace}/{your-repo}.git

i.e.

# for github example
git clone git@github-michaelmaky:michaelmaky/repo1.git
# for bitbucket example
git clone git@bitbucket.org-yourUserName1:yourUserName1/repo1.git

For creating repository, (github as example)

  1. Create your repository in github page
  2. Then following command below
cd {your_folder}
git init
git add .
git commit -m "first commit"
git branch -M main
git remote add origin git@github-michaelmaky:michaelmaky/repo1.git
git push -u origin main

--

--