Use multiple accounts with SSH

1. Generate keys

1
ssh-keygen -t rsa -C "YOUr_EMAIL_OR_YOUR_INFO" -f "YOUR_NAME_YOU_WANT_TO_SET_FOR_THIS_KEY"

eg.

1
ssh-keygen -t rsa -C "hduychinh@gmail.com" -f "primary_github"

2. Add key to ssh-agent

eg.

1
ssh-add ~/.ssh/your_file_ssh

what is ssh-agent?

eazy way, we can think it like a stack. When you access to server with ssh connection type.
So your device will send this stack to server. And if the valid key (private key) stored in this stack, you can access to server.

If you use ssh-agent. You can access like this:

1
ssh ubuntu@123.456.789

instead of:

1
ssh -i ~/.ssh/your_valid_private_key ubuntu@123.456.789

3. ssh-agent

Remove all entries in ssh-agent:

1
ssh-add -D

Show all entries in ssh-agent:

1
ssh-add -l

4. Create config file

~/.ssh/config

eg:

1
2
3
4
5
6
7
8
9
Host primary_github.github.com
HostName github.com
PreferredAuthentications publickey
IdentityFile ~/.ssh/primary_github

Host secondary_github.github.com
HostName github.com
PreferredAuthentications publickey
IdentityFile ~/.ssh/secondary_github

5. Add remote with prefix

eg:

1
git remote add origin git@primary_github.github.com:1312047/nus-overview.git