Home > Back-end >  multi git account management with ssh key
multi git account management with ssh key

Time:06-07

I have multiple Git accounts one is my personal use and one is for company use. Both accounts source need to be activated from my laptop.

1st, I generated two ssh keys:

% ssh-keygen -t ed25519 -C "[email protected]" -f ~/.ssh/my
% ssh-keygen -t ed25519 -C "[email protected]" -f ~/.ssh/work

2nd, use ssh-agent

% eval "$(ssh-agent -s)"

% ssh-add --apple-use-keychain ~/.ssh/my
% ssh-add --apple-use-keychain ~/.ssh/work

3rd, Edit the SSH config

touch ~/.ssh/config and edit contents like below:

Host *
  UseKeychain yes
  AddKeysToAgent yes
  IdentityFile ~/.ssh/my
  IdentityFile ~/.ssh/work

# account of myself, [email protected]
Host my
  HostName github.com
  User my
  IdentityFile ~/.ssh/my
  
# account of work, [email protected]  
Host work
  HostName github.com
  User work
  IdentityFile ~/.ssh/work

4th, Adding new SSH key to my GitHub account

as the reference: https://docs.github.com/en/authentication/connecting-to-github-with-ssh/generating-a-new-ssh-key-and-adding-it-to-the-ssh-agent

5th, Ensure the ssh-agent is running and loaded these two keys

% ps -e | grep ssh-agent

% ssh-add -l

256 SHA256:RnbFaLfrSIX4Al134lkjaleiur1SMIz7 OFwx5I9RHVMewwo9eq [email protected] (ED25519)

Now my question is : After I reboot the macOS, there is only one ssh key, I must activate another ssh key for work manually by the command below:

% ssh-add ~/.ssh/work

% ssh-add -l

256 SHA256:RnbFaLfrSIX4Al134lkjaleiur1SMIz7 OFwx5I9RHVMewwo9eq [email protected] (ED25519)
256 SHA256:QEWRrqpeowiufkndliuroqijr15u30491u3ojhjkrefaosdyflk [email protected] (ED25519)

and I cannot switch to the work ssh key when I working on the work repo (like ~/workcode), but I can found the % git remove -v response right, but got the wrong information when I run % git push as below:

ERROR: Permission to work/Test.git denied to my.
fatal: Could not read from remote repository.

Please make sure you have the correct access rights
and the repository exists.

Please tutor me how to switch to my or work ssh key when I working for myself or working for company.

Thank you.

CodePudding user response:

you can firstly revise the ~/.ssh/config as below:

Host *
  UseKeychain yes
  AddKeysToAgent yes

# account of [email protected]
Host mysec
  HostName github.com
  User git
  IdentityFile ~/.ssh/mysec
  
# account of [email protected]  
Host work
  HostName github.com
  User git
  IdentityFile ~/.ssh/work

and check the current ssh loaded by run % ssh-add -l if you want to push code by private account or work account, you can run

% ssh-add -D

This will Deletes all identities from the agent. Then you can load the ssh key you want by:

% ssh-add ~/.ssh/my or % ssh-add ~/.ssh/work

there is only one ssh key effect even if you load the two ssh key, so please only load one ssh key for you push.

CodePudding user response:

The ssh account to use should be git in both cases: User git.

As suggested in the "testing" sction of the docs: test your ssh configuration by running : ssh my / ssh work (or ssh git@my / ssh git@work to explicitly pass the user account to connect with).

AFAIK, you don't need to explicitly load the keys in ssh-agent at startup, the first ssh connection will do that.

  • Related