0%

Using a different SSH key for Git commands

I recently needed to add a tagging step to an automated build, but for some reason the default deploy key that had been configured before was no longer available, possibly due to a GitLab bug.

So I ended up creating a new SSH key pair on the build server and configuring Git-related SSH access to use that new key explicitly.

In the examples below, assume the Git server hostname is gitlab.knktc.com.

First, create a new SSH key pair:

1
2
mkdir -p /home/knktc/gitlab_ssh_keys
ssh-keygen -f /home/knktc/gitlab_ssh_keys/id_rsa

In this example, the new private key is stored at:

/home/knktc/gitlab_ssh_keys/id_rsa

Then create or edit the SSH config file under the current user’s home directory:

1
vim ~/.ssh/config

Add the following configuration:

1
2
Host gitlab.knktc.com
IdentityFile /home/knktc/gitlab_ssh_keys/id_rsa

This means that when connecting to gitlab.knktc.com over SSH, the newly created key will be used.

Finally, test cloning the repository:

1
git clone git@gitlab.knktc.com:blog/knktc-com.git

That should work.

For more details about SSH config, see:

https://linux.die.net/man/5/ssh_config

如果我的文字帮到了您,那么可不可以请我喝罐可乐?