81

I have the following and i need to clone the repository in either windows terminal command prompt or linux.

  • URL: git@xxxxx.com:xxx/xxx/git
  • username: xxx@xxx.in
  • SSH key: ssh-rsa AAAAB3NzaC1yxxxxxxxxxxxx....xxx@xxx.in

I tried as :

git clone git@xxxxx.com:xxx/xxx/git

I get

Permission denied(public key)
Couldn't read from remote repository

Also tried to change the URL as:

git clone https://xxxxx.com:xxx/xxx/git
papanito
  • 2,349
  • 2
  • 32
  • 60
Sushivam
  • 2,537
  • 4
  • 15
  • 25
  • Have you added your `~/.ssh/id_rsa.pub` in your account profile (e.g. GitHub, BitBucket)? – Sajib Khan Jan 18 '17 at 09:02
  • We dont have access to the account profile...The details are shared from the client and we just need to clone our code into their github – Sushivam Jan 18 '17 at 11:34

4 Answers4

229

Always coming late to answer anything, it may be possible that you have more than one ssh keys and if not specified git will try to use id_rsa but if you need a different one you could use

git clone git@provider.com:userName/projectName.git --config core.sshCommand="ssh -i ~/location/to/private_ssh_key"

This way it will apply this config and use a key different than id_rsa before actually fetching any data from the git repository.

subsequent fetch or push will use the specified key to authenticate for the cloned repository.

Hope this is helpful to anyone.

Wiston Coronell
  • 3,087
  • 3
  • 20
  • 27
  • 4
    But it makes problems with recursive submodules – rundekugel Sep 08 '20 at 13:57
  • after cloning, initializing submodules does not work. is there any workaround? – wrapperapps Oct 26 '20 at 19:30
  • 2
    well, the problem with sub-module is a bit different, that the one I was initially trying to tackle, but basically you would need to override the `.git/config` file inside of each submodule and correct the sshCommand to whatever the submodule requires. you may get a better explanation reading the documentation here https://git-scm.com/docs/git-config#_configuration_file – Wiston Coronell Oct 27 '20 at 19:10
  • You can add this as a function to your ~/.profile and have git "aliases" specific for each repo/organization. `function git-company(){git ${@:1} --config core.sshCommand="ssh -i ~/.ssh/company_private_key"}` This allows use of git-company instead of git for repos that need company_private_key. All parameters are being passed transparently. – Draco Dec 17 '20 at 15:46
  • I've found out (this is maybe specific to BitBucket) that the repo cannot be private in order to clone it even if the private SSH key is used. – jaques-sam Mar 09 '23 at 08:55
60

I suggest that you follow those steps:

Step 1: Check for existing SSH keys

$> ls -al ~/.ssh

Do you see any files named id_rsa and id_rsa.pub?

If yes go to Step 3

If no, you need to generate them

Step 2: Generate a new SSH key

$> ssh-keygen -t rsa -b 4096 -C "yourEmail"

Add your SSH key to the ssh-agent

$> eval "$(ssh-agent -s)"

$> ssh-add ~/.ssh/id_rsa

Step 3.1: Add the SSH key to your GIT account.

Get your public key

$> cat ~/.ssh/id_rsa.pub

Go to your GIT project -> Settings -> SSH keys

Then past the content of your public key in SSH keys

Step 3.2: Force SSH Client To Use Given Private Key

This is an alternative solution when you can't set keys on your Git account

$> sudo nano ~/.ssh/config

Then change this line

IdentityFile <yourPrivateKey>

Step 4: Clone the project

$> git clone git@xxxxx.com:xxx/xxx/git

Community
  • 1
  • 1
Ala Eddine JEBALI
  • 7,033
  • 6
  • 46
  • 65
  • Is there alternate way to Step3 , cause we dont have access to GIT account, having only the specified details from my question , i guess i cant access the Git account... Also i have the ssh key content (i guess its id_rsa.pub contents)....so i created a folder, with the file name id_rsa.pub and pasted the contents i have... – Sushivam Jan 18 '17 at 11:36
  • When you tape in a command `cat ~/.ssh/config` Do you see `IdentityFile` line? – Ala Eddine JEBALI Jan 18 '17 at 11:51
  • Here is the structure, i have a folder in my windows local drive as : C/Users/me/Test/.ssh in My ssh folder , i have .id_rsa file, and i when i type cat ~/.ssh/config i get no such file – Sushivam Jan 18 '17 at 11:57
  • @Sachins I updated my answer: See *Step 3.2: Force SSH Client To Use Given Private Key* – Ala Eddine JEBALI Jan 18 '17 at 11:58
  • @Sachins Since you're working on Windows take a look at this question http://stackoverflow.com/questions/9513712/git-ssh-client-for-windows-and-wrong-path-for-ssh-config-file – Ala Eddine JEBALI Jan 18 '17 at 12:01
  • What does config file contain? by the way im running all these commands in cygwin terminal or babun and since i dont have config file, i get file not found, Also in the 1st step itself, when i do ls -al ~/.ssh i dont see id_rsa, even though they physically exist in the folder – Sushivam Jan 18 '17 at 12:18
  • 1
    Please note that while `id_rsa` is the default given to ssh keys, they *need not* be named this. – Nathaniel Ford Mar 30 '20 at 16:43
  • This does not start where the question is located. The user already has an ssh key provided. You haven't really answered his question. – Kevin Buchs Apr 05 '21 at 03:08
  • If `~/.ssh/config` is empty, simply adding `IdentityFile id_rsa` will not work, you need the syntax, for ex: `Host github.com` `User git` `Hostname github.com` `IdentityFile id_rsa` ... [reference](https://linuxize.com/post/using-the-ssh-config-file/) I'd edit the answer but Edit queue is full... – ForeverLearning Feb 22 '22 at 21:33
  • Step 3.2 super thanks. Note; it's important to specify correct host. For instance `Host ssh.dev.azure.com ` – vintagexav May 10 '23 at 13:37
9

test ssh with GitHub to get actionable feedback.

in the command line run ssh -T git@github.com
(see https://help.github.com/articles/testing-your-ssh-connection/)


if that works, bother with git next. but likely there will, be your issue.

to clone, THEN use git clone git@github.com:$USERNAME/$REPONAME.git

Community
  • 1
  • 1
Daij-Djan
  • 49,552
  • 17
  • 113
  • 135
-12

To clone source code without using ssh, we can use http url with following format:

http://usernameWITHOUTdomain:password@the_rest_httpURL

ex: http://myname:password@192.110.110.10/projectname/project.git

OneCricketeer
  • 179,855
  • 19
  • 132
  • 245
Thien Pham
  • 29
  • 1
  • 4