Add public key <fileName>.pub to the server eg. github.com
Test the connection with the server eg. github.com
(Optional) Make git use different keys based on hostname
ls –al ~/.ssh
# list all ssh keys if existing, one key pair can be use on multiple servers
ssh–keygen –t rsa –C “email@me.com“ –f <keyName>
# create a new ssh key pair using rsa algorithm, email as label
# and keyName as filename, id_rsa by default if not specified
# follow the prompt to enter file name(if not specified) and passphrase,
# at the end a fingerprint or id of the key would be given
- Ensure ssh-agent is enabled
eval “$(ssh–agent –s)” # start the ssh-agent in the background
- Add new private key to the agent
ssh–add ~/.ssh/<fileName>
-
Add public key <fileName>.pub to the server eg. github.com
-
Test the connection with the server eg. github.com
ssh -i <fileName> -T git@github.com
# i flag specifies the ssh file used, id_rsa would be used if not specified
# T flag disabled pseudo-tty allocation
Git does not have i flag like ssh to specify the key file to use, but we can configure git to use the corresponding key files based on the host we try to git.
- Create ~/.ssh/config file if not existing
touch ~/.ssh/config
- Add these to ~/.ssh/config file
Host GitLab
HostName gitlab.com
User git
IdentityFile ~/.ssh/<gitlab-filename>
IdentitiesOnly yes
Host github
HostName github.com
User git
IdentityFile ~/.ssh/<github-filename>
IdentitiesOnly yes
- Test git connection
Git should be able to pull/push if those keys are already added to the servers