5

I want to clone an open, public repo into a different directory. To do this from the command line I type:

git clone [email protected]:computersarecool/dotfiles.git documents/gitprojects/dotfiles

which works.

However, now I want to do it from a bash script. In the script I have literally the exact same code:

#!/bin/bash
git clone [email protected]:computersarecool/dotfiles.git documents/gitprojects/dotfiles

but now I get an error:

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

Can anyone explain the issue to me? I do not even understand why it is asking for a publickey in the first place as the repo is open.

4
  • Are you doing this with sudo or as a different user or from cron or somesuch? Commented Aug 1, 2016 at 19:49
  • I am running the bash script as sudo! Is there a way to account for this? Commented Aug 1, 2016 at 19:53
  • git clone [email protected]:... is connecting over SSH. It has to authenticate to the remote SSH daemon before it's able to talk to anything git-specific, so whether the repo is authenticated or not is completely irrelevant: it hasn't finished transport-layer negotiation and authentication at the point when it fails, so whether there's any kind of application-layer access control or not is moot. Commented Aug 1, 2016 at 20:08
  • 1
    Is there a reason for using sudo to clone it? Commented Aug 1, 2016 at 20:14

2 Answers 2

4

If it is indeed a public open repo, use https instead of ssh:

#!/bin/bash
git clone https://github.com/computersarecool/dotfiles.git documents/gitprojects/dotfiles

If you want to stick to ssh, you should let ssh-agent know about your id_rsa key:

#!/bin/bash
eval `ssh-agent -s`
ssh-add /home/<Your username>/.ssh/id_rsa
git clone [email protected]:computersarecool/dotfiles.git documents/gitprojects/dotfiles

Add your username (and check the contents of the ~/.ssh directory to verify that your private key is named id_rsa)

Sign up to request clarification or add additional context in comments.

1 Comment

You're still using https:// in the second snippet, making the SSH key setup rather moot.
0

You mention in your comments that you run the bash script as sudo.

I am going to guess that you cloned in the command line without sudo.

The error message Permission denied (publickey). is basically saying that the user trying to clone does not have the correct ssh public key. This is because EACH AND EVERY USER on a unix system will normally have their own ssh public key. Presumably your user has been configured to authenticate with github using your public key. But sudo runs as the user root. If you haven't configured root's public key to be used with a github account (either root's own github account or yours) then it will fail authentication.

You basically have 2 solutions for this:

  1. Copy your public key to root's .ssh directory. This is not recommended! But for a single user machine like your own laptop the security risk of doing this is minimal.

  2. Create a public key for root and register it with github.

Alternatively clone using https instead of ssh (pushing would be a pain though as it will require you to key in your password every time).

Yet another alternative is clone as yourself, not root.

3 Comments

You mention public key here but I am assuming the root user would need a public and private key correct?
Yes, that's how the key pair works. But never expose the private key to anyone else.
...re: "pushing will be a pain" -- there's no obligation to push via the same URL one cloned with.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.