How to Specify SSH key for Git repository

Advertisement

Advertisement

Introduction

If you are using SSH keys with Git to clone and pull your repositories, you may have to manage several SSH keys. For example, it is common to setup a "deploy key" in GitHub (Repository | Settings | Deploy Keys) that has read-only rights. GitHub also forces you to use unique SSH deploy keys for each repository, so you have to create a unique SSH keys when you have multiple repositories.

This example shows you how to use specific SSH keys for each remote repository.

Setup

You will need to have Git installed.

You will also need the ssh-keygen application that comes with OpenSSH. You can generate SSH keys with other tools, but this is the one I typically use.

NOTE: that sudo has a big effect when running git clone or git pull. When you run it with sudo, it will use the root user's SSH config /root/.ssh/config and it will not use your personal $HOME/.ssh/config.

These examples assume you are running under the root user or you are using sudo for everything. If you need to get a root prompt you can use su.

# Switch to root user
sudo su
# or
su - root

Generate SSH keypair

There are several options for generating an SSH keypair. If you already have them, you can skip this step.

# Generate public and private SSH keys
ssh-keygen -f /root/.ssh/github-my-repo.id.rsa

The private file name one has no special suffix and commonly ends the algorithm like id.rsa. The public one will end with .pub.

Keep the private key private, always.

Take the public key /root/.ssh/github-my-repo.id.rsa.pub and set that up as a Deploy Key. For example, from a GitHub repository, go to Settings | Deploy Keys. If you're using a traditional SSH connection to another server, use ssh-copy-id or manually copy append your public key file contents in to the remote ~/.ssh/authorized_keys

Configure SSH to use the IdentityFile

Once you have your public and private SSH keys generated and the remote server has your public key configured, you need to tell the Git client to use the private SSH key when trying to perform git actions with the remote repository.

Open the SSH config file using your favorite text editor:

vi /root/.ssh/config

And include the custom host. I name it github-my-repo but you can name it anything.

Host github-my-repo
    # The host that has the remote Git repository
    Hostname github.com
    # Username for remote SSH user (For GitHub, everyone uses the name `git`)
    User git
    # Path to your private SSH key
    IdentityFile /root/.ssh/github-my-repo.id.rsa

To use this SSH host with git, you format your similar to this:

git clone github-my-repo:NanoDano/Example.git

Or if you already have the local repository and you want to add a new remote:

git remote add origin github-my-repo:NanoDano/Example.git

Conclusion

After reading this you should understand how to use specific SSH keys when working with remote git repositories using the SSH config file.

References

Advertisement

Advertisement