How to Specify SSH key for Git repository - TechvBlogs

How to Specify SSH key for Git repository

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

Suresh Ramani - Author - TechvBlogs
Suresh Ramani

1 year ago

TechvBlogs - Google News

You may need to handle multiple SSH keys if you use SSH keys with Git to clone and pull your repositories. For instance, it's typical to set up a "deploy key" in GitHub with read-only access. When you have numerous repositories, you must create distinct SSH deploy keys since GitHub requires you to utilize separate keys for each repository.

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

How to Specify SSH key for Git repository

Step 1: Generate SSH keypair

There are numerous ways to create an SSH keypair. You can skip this step if you already have them.

# Generate public and private SSH keys

ssh-keygen -f /home/{user}/.ssh/

If the config file is new, you might need to do chmod 600 ~/.ssh/config.

NOTE: On Linux and macOS, verify that the permissions on your IdentityFile are 400. SSH will reject, in a not clearly explicit manner, SSH keys that are too readable. It will just look like a credential rejection. The solution, in this case, is:

chmod 400 ~/.ssh/{privateKey}

Step 2: Configure SSH to use the IdentityFile

You must instruct the Git Client to utilize the private SSH key when attempting to perform git operations with the remote repository once you have produced your public and private SSH keys and the remote server has your public key configured.

Open the SSH config file using your favorite text editor (I prefer nano, but You can use VI or any other of your favorite.):

nano /home/{user}/.ssh/config

And include the custom host.

# Here you can give any name of a host.
Host my-personal-repo

    # The host that has the remote Git repository

    # Username for remote SSH user (For GitHub, everyone uses the name `git`)
    User git

    # Path to your private SSH key
    IdentityFile /home/{user}/.ssh/{privateKey}

If you want to use this SSH host with Git, format your file like this:

git clone my-personal-repo:client-a/laravel-api.git

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

git remote add origin my-personal-repo:client-a/laravel-api.git

Thank you for reading this article.

Read Also: How to change the URI (URL) for a remote Git repository

If you want to manage your VPS / VM Server without touching the command line go and  Checkout this linkServerAvatar allows you to quickly set up WordPress or Custom PHP websites on VPS / VM in a  matter of minutes.  You can host multiple websites on a single VPS / VM, configure SSL certificates, and monitor the health of your server without ever touching the command line interface.

Comments (0)


Note: All Input Fields are required.