Setup Multiple SSH Accounts
Sometimes you may be using multiple accounts for different service as Github, Gitlab, BitBucket, DigitalOcean, AWS; mention them on the same computer.
Implying that you will need to use different ssh keys for all the different accounts. I want to show you how to go about setting up multiple ssh accounts on the same host machine.
Generating SSH keys
There are two things that might come in handy here…
Email: associate each key-pair with the email you signed up with for that particular service
Name: associate each key-pair with the name of the service it will be deployed to
1 2 3 4 5 |
$ cd ~/.ssh $ ssh-keygen -t rsa -C "your_email@example.com" Generating public/private rsa key pair. Enter file in which to save the key (/c/Users/Admin/.ssh/id_rsa): id_rsa_github ... |
Notice the naming conversion I used for the key name; id_rsa_SERVICE
. You don’t have to use the same, but be sure to use one that won’t complicate life for you trying to remember which key is associated with which account.
Now you can repeat the above setup for all your other accounts using the appropriate email and name as described above.
Confirm that your keys were created
$ ls ~/.ssh
Create SSH configuration file
You you need to create your ssh client configuration file at ~/.ssh/config
. This will enable you to store your different connection configurations and process them automatically on connection.
It’s still possible to override the values in your configuration file at run-time via ssh command-line options.
$ vim config
Add configurations to your keys to the file. Below are a couple of different configurations you can borrow from.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 |
# Github: 'jdoe@example.com' account Host jdoe.github.com HostName github.com User git PreferredAuthentications publickey IdentityFile ~/.ssh/id_rsa_github # Gitlab: 'jdoe@example.com' account Host jdoe.gitlab.com HostName gitlab.com User git PreferredAuthentications publickey IdentityFile ~/.ssh/id_rsa_gitlab # Bitbucket: 'jdoe@example.com' account Host jdoe.bitbucket.com HostName bitbucket.com User git PreferredAuthentications publickey IdentityFile ~/.ssh/id_rsa_bitbucket # DigitalOcean Host jdoe-do HostName XXX.XXX.XXX.XXX User jdoe PreferredAuthentications publickey IdentityFile ~/.ssh/id_rsa_digitalocean # Amazon Web Services Host jdoe-aws HostName XXX.XXX.XXX.XXX User jdoe PreferredAuthentications publickey IdentityFile ~/.ssh/aws.pem # Password Based Auth Host dev HostName XXX.XXX.XXX.XXX Port 22 LocalForward 5433 localhost:5432 User root |
Some of the client configuration options used in this file.
Host: Restricts the following declarations to be only for those hosts that match one of the patterns given after the keyword.
HostName: Specifies the real host name to log into.
User: Username to the service
PreferredAuthentications: Specifies the order in which the client should try protocol 2 authentication methods.
IdentityFile: Specifies a file from which the user’s identity key is read when using public key authentication.
Find the complete list of client configuration options here.
Delete all cached keys
$ ssh-add -D
Start SSH agent
$ eval `ssh-agent -s`
Using eval
instead of just ssh-agent
will start an agent automatically for each new command prompt window that you open. Source: Robin Green
Add your keys to the agent
New keys need to be registered before usage, so lets add them like:
ssh-add ~/.ssh/id_rsa_gitlab
Confirm all your keys were added
ssh-add -l
Test connection using your keys
Using ssh -T <user>@<host>
like;
ssh -T git@jdoe.github.com
ssh -T git@jdoe.gitlab.com
Git Repository URL
For your Version Control Tools; Github, Gitlab, or Bitbucket you will need to change the git repository urls to use the host
defined in our ssh config file. This will ensure that the correct SSH key is used for each repository. For example;
Git Repo Url format: <user>@<host>:<username>/<repo>.git
like;
Default: git remote add origin git@github.com:jdoe/my_repo.git
New: git remote add origin git@github.jdoe.com:jdoe/my_repo.git
Differentiate accounts
Avoid using --global
flag your git username or email when working with multiple accounts as these will override settings in other projects. For example:
1 2 3 4 5 |
- git config --global user.name "<username>" + git config user.name "<username>" - git config --global user.email "<username>@<vcs-domain>" + git config user.email "<username>@<vcs-domain>" |
That marks the end of this tutorial; feel free to leave me a comment regarding this topic, or a request for a particular tutorial. Cheers