Unable to clone Github repo via SSH protocol. HTTPS works.

March 18, 2014 102.8k views
Hi, I have a very basic droplet setup. No firewalls or any sort of security measures are set up. It's virtually open. I wanted to clone a git repo but had some issues. Let me illustrate with an example: ----- $ cd /var/www $ mkdir test $ cd test $ git clone git@github.com:housni/li3_uploadable.git ----- The above gives me: ----- Cloning into 'li3_uploadable'... Permission denied (publickey). fatal: Could not read from remote repository. Please make sure you have the correct access rights and the repository exists. ----- 'li3_uploadable' is a public repo. I know this because I own it. Besides, I have this issue with all the other Github repos. However, I noticed that cloning via HTTPS works: ----- git clone https://github.com/housni/li3_uploadable.git ----- I asked Digitalocean support but they were unable to help me. Any ideas on what's going on? Thanks.
  • You need to add your public key to GitHub in order to clone through SSH -- even if it is a public repo. Why? Because git needs to authenticate though SSH to GitHub's servers in order to do the clone. And in order to do that, they need to have your public key.

  • Thanks, It worked for me thanks a lot

13 Answers
Try specifying the private key with -i
git -i /path/to/your/private/key git@github.com:repository/location

Or you can edit ~/.ssh/config and add these lines:

Host github.com
User git
Hostname github.com
PreferredAuthentications publickey
Port 22
IdentityFile /path/to/your/private/key

[any other host you want to add]

After that you can just type:
git clone github.com:repository/location
You need to make sure that you have added the public key from your server to your Github repo first.

I was getting this message, even though I had followed the guide at GitHub to the letter - including adding the public SSH key to their website https://help.github.com/articles/generating-an-ssh-key/

The issue was, out of habit I had been using using sudo in front of git commands i.e. sudo git pull. The SSH that was previously generated was in my home directory but putting sudo in front of it makes it look elsewhere for the key. So using just git pull makes it look within my home directory - it works great now!

I recall `i` being an option for `ssh` but it doesn't appear to be a part of `git` according to man.

So, I created the file `/.ssh/config` and wrote the contents to it, changing the path to my key.

Still no luck.

Why does this happen only with DigitalOcean? I don't recall having this issue on Amazon, Hostgator, Linode and others.
Check the path,
The path should be /home/username/.ssh/config or ~/.ssh/config
but not /.ssh/config
Tony Tsang,
Sorry, I meant to type `~/.ssh/config`.
By the way, I even tried adding that info to `/etc/ssh/ssh_config`, no dice.
In any case, if `id_rsa.pub` resides in `~/.ssh`, git via SSH should pick the key up automatically.

Emilio Castro,
That's not necessary since these are all public repos. I only own that one repo, so if necessary, I can add the key but there are others I'm trying to clone but it doesn't work and I can't add my key to them because they are not my repositories and these are public repositories such as Twitter Bootstrap, for example (http://github.com/twbs/bootstrap):

$ cd /var/www
$ mkdir test
$ cd test
$ git clone git@github.com:twbs/bootstrap.git
Cloning into 'bootstrap'...
Permission denied (publickey).
fatal: Could not read from remote repository.

Please make sure you have the correct access rights
and the repository exists.
You have to add the public key in order to access github through ssh.
I am experiencing this same issue and I have added my public key on Github.
I observed the same issue : Below steps worked for me From Terminal - > Type ssh-add -l If the output is :The agent has no identities. Enter: ssh-add ~/.ssh/id_rsa This command should add your keys to the local ssh-agent. Once it is been added , Try git clone

I encountered this issue too. But after I disconnected my VPN, it worked.

After you generated your ssh keys, there are only two files under ~/.ssh folder which are id_dsa and id_dsa.pub, but after you successfully connected to a server, another file known_hosts will be added.

Maybe you can add that file manually. But I am sorry I don't know how to generate that file.


I had the exactly same problem! I tried to clone a public git repo (using ssh) from two different computers (macs). In one computer it worked but it didn't work in another. The only difference was: in one computer I was using zsh (in this one, the clone didn't work) and in bash in another (it worked on this). When I changed from zsh to bash in the machine that didn't work and tried cloning, it worked!! :)

I don't know why. Maybe is some problem with zsh.

I had SSH setup correctly and still got this error. The problem was not SSH, but simply folder permissions. The folder I was trying to clone into was owned by the root user (check this using ls -l), and using sudo is not valid when cloning a git repo. I had to change the folder to be owned by my user, like this: sudo chown -R $USER:$USER <path_to_folder>

Have another answer? Share your knowledge.