acspock
By:
acspock

Unable to git pull with SSH - Works with HTTPS

March 23, 2017 693 views
DigitalOcean Git Ubuntu 16.04 Ubuntu

I'm stuck using https for git pulls... I have tried to switch to ssh but every time I try to add my identity, I get a "Could not open a connection to your authentication agent".

I tried creating many keys and adding them to git but to no avail. I'm turning to the forums to get some guidance

sudo git pull
Permission denied (publickey).
fatal: Could not read from remote repository.

Please make sure you have the correct access rights
and the repository exists.

I'm on ubuntu 16.04

These are the contents of my ~/.ssh/

-rw------- 1 macrandle macrandle  744 Nov  8 07:40 authorized_keys
-rw-rw-r-- 1 macrandle macrandle  120 Mar 23 06:18 config
-rw------- 1 macrandle macrandle 3326 Nov  8 08:02 id_rsa
-rw-r--r-- 1 macrandle macrandle  743 Jan 13 22:19 id_rsa.pub
-rw------- 1 root   root   3326 Mar 23 00:17 id_rsa_work
-rw-r--r-- 1 root   root    743 Mar 23 00:17 id_rsa_work.pub
-rw-r--r-- 1 macrandle macrandle 2652 Jan 13 22:29 known_hosts

I'm trying to add idrsawork and my keys are added to my github account

I have tried

eval $(ssh-agent)

and then

sudo ssh-add ~/.ssh/id_rsa_work

this leads to "Could not open a connection to your authentication agent"

Any help would be great.

Cheers

2 Answers

I saw some possible solutions in a forum at Github, so I credit this thread. but since it directly pertains to Digital Ocean customers, I thought it'd be useful to have the content here.

This error happens more. It is most likely caused by errors in your gitlab-shell database (specificly the ssh database) and actualy happens on most preïnstalled digitalocean packages. Fixing this issue is very easy in most cases. Just follow the steps bellow (all commands are performed server-side):

Step 1 - Clean your SSH configuration

The shell command below will reset all gitLab-keys that could still be hanging around in the basic Linux Environment (if you are not on root add sudo to the second command bellow)

cd /bin
./bin/gitlab-keys clear

Step 2 - Clean your gitlab-shell database

Then if your problem is not fixed, the command below will drop the gitlab-shell database, create it again and populate it with the right migrations (don't forget the sudo command here even if you are on root since we're going to give it parameters). please note after these commands all website users will need to re-add their ssh keys to their accounts..

cd /home/git/gitlab

sudo -u git -H bundle exec rake gitlab:shell:setup RAILS_ENV=production
Now restart the server to reload all scripts, environments and stuff. You could just restart the gitlab application, but In my experience a full server restart triggers more environment details (like the ssh configuration and stuff) again if you are not on root use sudo to shutdown.. restarting servers is a sudo thingy...

shutdown -r 0
Broadcast a message on your website (see admin area -> messages or /admin/broadcast_messages) explaining that you removed all public keys from all users on your gitlab server. Users will freak out and think that their accounts/keys are stolen/hacked/whatever... It will save you the trouble of having people spamming you.

Problem not fixed?

You could experience other issues. try installing htop, a monitor application for system usage to review your computer's presentation-status.

sudo apt-get install htop
htop

Used memory and swap ridiculously high? Try expanding them ;)

@acspock

Do not use sudo.

Make sure your id_rsa.pub is added to your github account.

  • My id_rsa.pub is added to my github. I've also confirmed the md5 hash fingerprint.

    I first do eval $(ssh-agent) and then ssh-add ~/.ssh/id_rsa

    I tried without sudo and still

    Permission denied (publickey).
    fatal: Could not read from remote repository.
    
    Please make sure you have the correct access rights
    
    • Are you sure that git uses your private key?

      Show me the output of git remote -v.

      Does your public key works on other sites?

      • git remote -v

        origin  git@github.com:acspock/spockRepo.git (fetch)
        origin  git@github.com:acspock/spockRepo.git (push)
        

        I generated this key on the machine so ive never used it anywhere else.

      • @acspock This means your ssh keys are setup correctly, I am wondering what could be wrong??!!!! Can you post a screenshot of the command and the output? I am becoming very curious about this problem, it is probably some simple but we are missing it.

Have another answer? Share your knowledge.