Clone a Git Repository Using Private Key on Ubuntu

Have you ever received this error message?

$ git clone git@bitbucket.org:mike/web-app.git
Cloning into 'web-app'...
Permission denied (publickey).
fatal: The remote end hung up unexpectedly

Most likely you need to add private key to the ssh authentication agent. Here is how to do it.

Method #1 (Temporary, need to add for every restart)

Make sure ssh-agent is running

$ ps -e | grep sh-agent
 1525 ?        00:00:00 ssh-agent

If not, then start it by using the following command:

$ ssh-agent /bin/bash

Add private key to ssh-agent .

$ ssh-add /home/mike/id_rsa.key

Check if the private key is successfully added

$ ssh-add -l
2048 34:a1:98:38:1b:d5:12:98:19:42:b9:8b:14:19:17:63 id_rsa (RSA)

Clone your git repository

$ git clone git@github.com:mike/macosx-app.git

Method #2 (Permanently)

Create a config file in .ssh folder

$ cd /home/mike/.ssh
$ touch config

Add this value to the config file and save.

IdentityFile /home/mike/id_rsa.key

Clone your git repository

$ git clone git@github.com:mike/macosx-app.git

comments powered by Disqus