Enter your keyword

Setup Multiple SSH Accounts

Setup Multiple SSH Accounts

Sometimes you may be using multiple accounts for different service as GithubGitlabBitBucketDigitalOceanAWS; 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

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.

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:

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