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
$ cd ~/.ssh
Generating public/private rsa key pair.
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.
# Github: 'firstname.lastname@example.org' account
# Gitlab: 'email@example.com' account
# Bitbucket: 'firstname.lastname@example.org' account
# Amazon Web Services
# Password Based Auth
LocalForward 5433 localhost:5432
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`
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:
Confirm all your keys were added
Test connection using your keys
ssh -T <user>@<host> like;
ssh -T email@example.com
ssh -T firstname.lastname@example.org
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:
git remote add origin email@example.com:jdoe/my_repo.git
git remote add origin firstname.lastname@example.org:jdoe/my_repo.git
--global flag your git username or email when working with multiple accounts as these will override settings in other projects. For example:
- git config --global user.name "<username>"
- git config --global 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