Steps to add multiple ssh keys for different host accounts:
- Create .ssh folder under home directory if not exist, and navigate to .ssh folder
cd ~
mkdir .ssh
cd .ssh
2. To generate public private ssh key pair run the following command. It will ask for passphrase, press enter for no passphrase.
ssh-keygen -C “<comment like email>” -f “<file-name>”
Example:
ssh-keygen -C “abc@gmail.com” -f “id_rsa_github”The above example will create 2 files “id_rsa_github” (private key) and “id_rsa_github.pub” (public key). Note that you can choose any file name for -f parameter.
3. To create a new public-private ssh key for other host, say bitbucket.org.
ssh-keygen -C “abc@gmail.com” -f “id_rsa_bitbucket”
The above command will create 2 files “id_rsa_bitbucket” (private key) and “id_rsa_bitbucket.pub” (public key)
4. Add public key to respective host (github.com or bitbucket.org)
- Copy the content of public key (*.pub file).
- Add the content of public key under the manage ssh keys in the setting of the respective host.
- Navigate to profile->Settings->SSH keys.
- New SSH key/ Add key.
- Provide title/label of your choice.
- Paste the content of public key (*.pub) in key.
5. Add the git configuration for the generated public-private key pairs.
- Create a config file under ~/.ssh if do not exist.
- vi config
- Add config for above generated ssh keys for different hosts github.com and bitbucket.org
Host github.com
HostName github.com
PreferredAuthentications publickey
IdentityFile ~/.ssh/id_rsa_githubHost bitbucket.org
HostName bitbucket.org
PreferredAuthentications publickey
IdentityFile ~/.ssh/id_rsa_bitbucket
6. Clone your repo from the respective host at the desired folder location.
Example:
git clone <ssh git repo url>
git clone git@github.com:<repo-name>
7. Configure the user name and email in the repo.
Global Configuration (same for all repos and accounts)
git config –global user.name “abc”
git config –global user.email “abc@gmail.com”
Local Configuration (configuration specific to current git repo)
git config –local user.name “abc”
git config –local user.email “abc@gmail.com”