Multi-account configuration for Git repositories is essentially an extension of multi-account configuration for SSH remote login. Let's first look at the basic SSH remote login multi-account configuration.
Create a file named config under ~/.ssh/. Write the following configuration in it:
Host testVM
HostName xxx.xxx.xxx.xxx
User username
Port 22
IdentityFile /path/to/my/private/key/testVM.pem
Note that all private key file permissions should be set to 400, and config file permissions should be set to 600.
chmod 400 /path/to/my/private/key/testVM.pem
chmod 600 ~/.ssh/config
Then you can use aliases to run simplified SSH remote login, for example: ssh testVM.
Building on this foundation, when we need to manage multiple Git repositories simultaneously, we still add Git server access configurations in the config file. For example:
# Github
Host github.com
User github_user_name
IdentityFile /path/to/my/private/key/github.pem
# Azure Repos, private repositories hosted on Azure
Host ssh.dev.azure.com
User git
IdentityFile /path/to/my/private/key/repos.pem
It's important to note that when configuring Git servers, don't write the HostName item. Instead, directly write the Git server domain name in the Host section. This allows ssh and git clients to properly find the relevant server configuration and load the required private key.
Afterwards, we can verify authorization with ssh -Tv git@github.com. The -T parameter indicates test verification, and -v indicates outputting debug information at level 1 detail.
The return result is quite long, so only the important parts are excerpted:
OpenSSH_7.5p1, OpenSSL 1.0.2o 27 Mar 2018
debug1: Reading configuration data /home/mobaxterm/.ssh/config
debug1: /home/mobaxterm/.ssh/config line 12: Applying options for github.com
...
debug1: Connecting to github.com [192.30.255.113] port 22.
debug1: Connection established.
...
debug1: Authentications that can continue: publickey
debug1: Next authentication method: publickey
debug1: Trying private key: /path/to/my/private/key/github.pem
debug1: Authentication succeeded (publickey).
Authenticated to github.com ([192.30.255.113]:22).
debug1: Requesting X11 forwarding with authentication spoofing.
debug1: Requesting authentication agent forwarding.
X11 forwarding request failed on channel 0
debug1: client_input_channel_req: channel 0 rtype exit-status reply 0
Hi github_user_name! You've successfully authenticated, but GitHub does not provide shell access.
When using Azure DevOps Repos as the repository, using ssh -Tv for verification is more important because the successful authentication information is in the debug output. Without -v, it may seem like the verification failed.
ssh -T git@ssh.dev.azure.com
X11 forwarding request failed on channel 0
shell request failed on channel 0
With the -v parameter, we can see:
ssh -Tv git@ssh.dev.azure.com
OpenSSH_7.5p1, OpenSSL 1.0.2o 27 Mar 2018
debug1: Reading configuration data /home/mobaxterm/.ssh/config
debug1: /home/mobaxterm/.ssh/config line 4: Applying options for ssh.dev.azure.com
...
debug1: Connecting to ssh.dev.azure.com [20.189.107.3] port 22.
debug1: Connection established.
...
debug1: Authenticating to ssh.dev.azure.com:22 as 'git'
debug1: Host 'ssh.dev.azure.com' is known and matches the RSA host key.
debug1: Found key in /home/mobaxterm/.ssh/known_hosts:3
debug1: Authentications that can continue: password,publickey
debug1: Next authentication method: publickey
debug1: Trying private key: /path/to/my/private/key/repos.pem
debug1: Authentication succeeded (publickey).
Authenticated to ssh.dev.azure.com ([20.189.107.3]:22).
...
X11 forwarding request failed on channel 0
shell request failed on channel 0
Now we can see the successful authentication information.
At this point, multi-account configuration for Git is complete. Now we can git clone and start coding happily.