How to Specify the Private Ssh-Key to Use When Executing Shell Command on Git?

Better Stack Team
Updated on July 25, 2024

When executing Git commands that require SSH authentication, such as cloning a repository or pushing changes, you may need to specify a private SSH key to use. Here's how you can do it:

The SSH agent is a program that runs in the background and stores your SSH keys. It can be used to manage your SSH keys securely, avoiding the need to explicitly specify them every time.

  1. Start the SSH Agent:

    Start the SSH agent in your terminal session:

     
    eval "$(ssh-agent -s)"
    
  2. Add Your SSH Private Key:

    Add your SSH private key to the SSH agent. If your private key is not in the default location (~/.ssh/id_rsa), specify the path to your key file:

     
    ssh-add /path/to/your/private/key
    

    For example, if your key is id_rsa_example:

     
    ssh-add ~/.ssh/id_rsa_example
    
  3. Execute Git Command:

    After adding your key to the SSH agent, you can execute Git commands that require authentication:

     
    git clone git@github.com:user/repo.git
    

    The SSH agent will automatically use the added private key for authentication.

Method 2: Using SSH Config File (For Specific Repositories)

If you want to specify a different SSH key for a specific repository or host, you can use the SSH config file (~/.ssh/config).

  1. Edit SSH Config File:

    Open or create the SSH config file:

     
    nano ~/.ssh/config
    
  2. Specify SSH Key for Host:

    Add an entry for the host (GitHub in this example) and specify the SSH private key to use:

     
    Host github.com
        IdentityFile /path/to/your/private/key
        # Optionally, specify other configurations like User
        User your_username
    

    Replace /path/to/your/private/key with the path to your SSH private key and your_username with your GitHub username.

  3. Execute Git Command:

    Now, when you execute Git commands for that host, the specified SSH key will be used:

     
    git clone git@github.com:user/repo.git
    

Method 3: Using Environment Variable (GIT_SSH_COMMAND)

You can also specify the SSH private key directly using the GIT_SSH_COMMAND environment variable. This method is less common but can be useful in certain scenarios:

 
GIT_SSH_COMMAND="ssh -i /path/to/your/private/key" git clone git@github.com:user/repo.git

Replace /path/to/your/private/key with the path to your SSH private key.

Notes:

  • SSH Keys Security: Ensure your SSH private keys (id_rsa) are kept secure and not shared publicly.
  • Permissions: Make sure your private key file (id_rsa) has the correct permissions (600 or 400), otherwise SSH may refuse to use it for security reasons.

By using one of these methods, you can specify the SSH private key to use when executing Git commands that require SSH authentication. This flexibility is particularly useful when managing multiple SSH keys or when dealing with repositories that require specific authentication configurations.

Got an article suggestion? Let us know
Explore more
Git
Licensed under CC-BY-NC-SA

This work is licensed under a Creative Commons Attribution-NonCommercial-ShareAlike 4.0 International License.

Make your mark

Join the writer's program

Are you a developer and love writing and sharing your knowledge with the world? Join our guest writing program and get paid for writing amazing technical guides. We'll get them to the right readers that will appreciate them.

Write for us
Writer of the month
Marin Bezhanov
Marin is a software engineer and architect with a broad range of experience working...
Build on top of Better Stack

Write a script, app or project on top of Better Stack and share it with the world. Make a public repository and share it with us at our email.

community@betterstack.com

or submit a pull request and help us build better products for everyone.

See the full list of amazing projects on github