How to Use a Custom SSH Identity with Git

By Andre Perunicic | August 24, 2017

This page describes how to specify which SSH profile to use with git. This can be handy if you have more than one GitHub account and don’t wish to reuse your default SSH key or type in a password every time you push or pull. The commands will be written from the perspective of a GitHub user, but should be easy to adapt to other scenarios as well.

Step-by-Step Instructions

  1. Generate a new SSH key:

    ssh-keygen -t rsa -b 4096 -C "name@domain.com"
    

    Let’s assume the identity file you created is in ~/.ssh/id_rsa_custom.

  2. Upload the key to GitHub (link leads to GitHub’s instructions).

  3. SSH will look for profiles in the user’s ~/.ssh/config file. Add something similar to this to that file:

    Host custom
        Hostname        github.com
        IdentityFile    ~/.ssh/id_rsa_custom
        IdentitiesOnly yes
    

    The IdentitiesOnly yes option is required to prevent the SSH sending the identity file matching the default filename, as is the default. If you have a file named ~/.ssh/id_rsa that will get tried BEFORE your ~/.ssh/id_rsa_custom without this option.

  4. Associate the remote git repository to your local repo:

    git remote add origin git@custom:user/myrepo.git
    

    If starting from scratch, you can just do:

    git clone git@custom:user/myrepo.git
    

    From then on, commands like git push -v origin master should work normally without any prompts.

If you got to this page from Google, I hope you found the answer you were looking for. Get in touch with us if you have a data sourcing or analysis task you want done quickly and reliably.

Suggested Articles

If you enjoyed this article, then you might also enjoy these related ones.

Extending CircleCI's API with a Custom Microservice on AWS Lambda

By Evan Sangaline
on February 20, 2018

A guide to setting up a practical proxy API on Amazon's Lambda using Node.js and Express.

Read more

It is *not* possible to detect and block Chrome headless

By Evan Sangaline
on January 18, 2018

An updated example of techniques to avoid detection.

Read more

Installing Google Chrome On CentOS, Amazon Linux, or RHEL

By Evan Sangaline
on September 6, 2017

A guide to getting Google Chrome running on RHEL variants.

Read more

Comments