简体   繁体   中英

Permission denied when cloning git repository

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?

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 eg 0600

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

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

Is the id_rsa private key in ~/.ssh/id_rsa the pair to you public key ( ~/.ssh/id_rsa.pub ) ?

If it's not (or you're not sure) I suggest you regenerate a new private/public key pair with ssh-keygen -t dsa .

My solution matches that of nos. Adding the public key of the root user fixes it. Another option would be changing the permission of the directory and executing the command as a regular user.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM