Permission denied when cloning git repository

98,290

Solution 1

Your first error is because your user does not have access to write to /var/www/html . You could give your user permissions to do so.

Your second error when running as root, is likely that you have your ssh keys in your user home directory, not in /root/.ssh/ , or that your .ssh directory or the ~/.ssh/id_rsa.pub key file have improber permissions. ~/.ssh/ should have the permission bits 0700 , and should have ~/.ssh/id_rsa.pub e.g. 0600

Solution 2

Note: this fix works for Mac users

Incase of macOS 10.12.2 or later, you will need to modify your ~/.ssh/config file to automatically load keys into the ssh-agent and store passphrases in your keychain.

Host *
   AddKeysToAgent yes
   UseKeychain yes
   IdentityFile ~/.ssh/<your_id_rsa>

Add your SSH private key to the ssh-agent and store your passphrase in the keychain. If you created your key with a different name, or if you are adding an existing key that has a different name, replace id_rsa in the command with the name of your private key file.

ssh-add -K ~/.ssh/<your_id_rsa>

For more information please review https://help.github.com/en/github/authenticating-to-github/generating-a-new-ssh-key-and-adding-it-to-the-ssh-agent

Solution 3

Have you tried this:

git: fatal: Could not read from remote repository

You can specify the username that SSH should send to the remote system as part of your remote's URL. Put the username, followed by an @, before the remote hostname.

git remote set-url website abc@***.com:path/to/repo
Share:
98,290
coryj
Author by

coryj

Web Developer, Student @ Rutgers

Updated on July 09, 2022

Comments

  • coryj
    coryj almost 2 years

    So I just setup an Amazon EC2 instance. And installed git..

    sudo yum install git
    

    I then set up my ssh key with github. Now when I try to clone my repo into /var/www/html folder i get this error..

    fatal: could not create work tree dir 'example.com'.: Permission denied
    

    and when I run as root...

    Cloning into 'example.com'...
    Permission denied (publickey).
    fatal: Could not read from remote repository.
    
    Please make sure you have the correct access rights
    and the repository exists.
    

    But I made sure that my github public key matches my ~/.ssh/id_rsa.pub key. Is there something that I'm missing here?

  • coryj
    coryj about 11 years
    so if I do not have a /.ssh directory in my /root (/root/.ssh) I should just create that and then ssh-keygen etc.. from there? As of now cd ~/.ssh brings me to my key
  • nos
    nos about 11 years
    @coryjacik You could do that, yes. Though best practices would say that you should not do these things as root. Better to alter the group of /var/www/html to be a group your user is a member off and give group access to that directory.