setting up ssh-keys for bitbucket account (with mac osx) - macos

I have a BitBucket account. Despite setting up the ssh key on the computer and then adding it on my bitbucket account, I fail at connecting:
$ git clone git#bitbucket.org:my-account/myrepo.git
Cloning into 'myrepo'...
conq: repository does not exist.
fatal: Could not read from remote repository.
Please make sure you have the correct access rights
and the repository exists.
I did the following:
$ cd ~/.ssh
$ ssh-keygen -f ~/.ssh/my-key -N ''
$ vim config
Host bitbucket.org
HostName bitbucket.org
IdentityFile ~/.ssh/my-key
$ ls -l
-rw------- 1 me ... 229 Jul 8 15:50 config
-rw------- 1 me ... 1679 Jul 8 14:07 my-key
-rw-r--r-- 1 me ... 411 Jul 8 14:07 my-key.pub
Then I added the content of my-key.pub in my bitbucket account's ssh keys. Then I retried cloning my repository:
$ git clone git#bitbucket.org:my-account/myrepo.git
Cloning into 'myrepo'...
conq: repository does not exist.
fatal: Could not read from remote repository.
Please make sure you have the correct access rights
and the repository exists.
I repeated the identical procedure on another machine, it worked:
$ git clone git#bitbucket.org:my-account/myrepo.git
Initialized empty Git repository in /home/me/.ssh/myrepo/.git/
remote: Counting objects: 3, done.
remote: Total 3 (delta 0), reused 0 (delta 0)
Receiving objects: 100% (3/3), done.
I need to do the same on my first machine now.
As a last resort, I followed some advises on the net using the ssh-agent:
$ eval `ssh-agent -s`
$ ssh-add ~/.ssh/my-key
But still no luck:
$ git clone git#bitbucket.org:my-account/myrepo.git
Cloning into 'myrepo'...
conq: repository does not exist.
fatal: Could not read from remote repository.
Please make sure you have the correct access rights
and the repository exists.
I have no idea what is different on the first machine, why does it reject my request?
EDIT 1
Of course I tried doing it the very basic way, as suggested by D.Samchuk:
Generating the id_rsa
$ ssh-keygen -t rsa
enterx3
Copied the exact content of the key
$ cat ~/.ssh/id_rsa.pub
And I added id_rsa.pub's content in my BitBucket account's ssh keys.
Git clone will still not allow me to access the repository, whit the same output error message as above.
EDIT 2
I have no idea maybe it is related to mac configurations? Since The other machine is a redhat linux I just wondered but couldn't find any reported similar issue.

just do following:
$ ssh-keygen -t rsa
name: id_rsa
passphrase: just press enter twice to avoid print keyword everytime you connect
$ cat ~/.ssh/id_rsa.pub
copy output
open bitbucket --> profile --> bitbucket settings --> add ssh key --> paste your output

If you are not using a passphrase for the ssh key you shouldn't need the ssh-agent
https://help.github.com/articles/working-with-ssh-key-passphrases/

Solved. As I delved deeper in the issue I found it is absolutely not related to ssh keys but to missing repository as the error message suggested. It was all about experimenting the global config using git config --global that messed up my git configuration on that computer. I detailed my steps in another thread here: https://unix.stackexchange.com/questions/294780/git-retains-remote-information-from-a-deleted-repository/294809#294809.

Related

Can't clone, can SSH. "Permission denied (publickey)."

I cannot clone or push to a repository on my server.
I have a bare repo that is located is a directory user#host in directory home/user/test.git that I am trying to access via git clone. I used ssh-add <pathtokey> to add my ssh key. It asked me for the passphrase. I can then ssh user#host successfully.
However if I then try to git clone ssh://user#host/~/test.git I get:
Cloning into 'test'...
user#host: Permission denied (publickey).
fatal: Could not read from remote repository.
Please make sure you have the correct access rights
and the repository exists.
also tried
git clone ssh://user#host/home/user/test.git
git clone user#host:home/user/test.git
git clone user#host:/home/user/test.git
with the same result
I am guessing the git credential manager isn't picking up the keys?
On the server /var/auth/log says
Feb 20 02:25:36 xxxxx sshd[24674]: Connection closed by authenticating user XXXX x.x.x.x port 56433 [preauth]
Git version: git version 2.30.1.windows.1
Git Credential Manager: Git Credential Manager version 2.0.318-beta+44acfafa98 (Windows, .NET Framework 4.0.30319.42000)
git config -l reports credential.helper=manager-core
Tried both PowerShell and git bash shells, same result
user has read, execute permissions to the repo
To add to #VonC's response
In git-bash things work as normal.
The normal flow of starting the ssh-agent (via eval 'ssh-agent'
), adding the key via ssh-add <path_to_key> enables git clone to work.
In PowerShell Core, or Cmd, via Windows-Terminal more work is required
the ssh-agent starts automatically (assuming you previously started the OpenSSH Authentication Agent service), adding the key works, and you can ssh after this, but git commands do NOT work, initially, but if you do
git config --global core.sshCommand C:/Windows/System32/OpenSSH/ssh.exe
This will replace the (default) ssh that comes with git-for-windows with the Windows10 implementation.
After this it should work fine in things other than git-bash. Confirmed in Powershell-Core, Command Prompt
See also:
https://gist.github.com/danieldogeanu/16c61e9b80345c5837b9e5045a701c99
The git credential manager is only involved for caching credentials (username/password) for HTTPS URL, not SSH.
Only the ssh-agent could be involved, for caching a possible passphrase, if the private key was defined with it.
I would try first using the full path, since ~ might not be interpreted by the remote shell, but the local (which has a different path for ~):
git clone ssh://user#host/home/user/test.git
# or
git clone user#host:/home/user/test.git
If not, in a git bash session, type:
export GIT_SSH_COMMAND='ssh -v'
git clone ...
The OP confirms in the discussion it works in a bash session:
In git bash, I started the ssh-agent,
added the key there, then it worked.

Jenkins pipeline Git error: “Host Key Verification Failed” when connecting to remote repository

I am using my PC as a server to build my Jenkins script with declarative pipeline.
when running a git clone on the git bash, it works well:
$ git clone ssh://guillaumed#myserver.com:29418/myProjects/thisProject.git ThisFirmware
Cloning into 'ThisFirmware'...
remote: Counting objects: 889, done
remote: Finding sources: 100% (203/203)
Receiving objects: 79% (121769/152721), 48.11 MiB | 10.35 MiB/s
but running it on the same machine through pipelining :
sh "git clone ssh://guillaumed#myserver.com:29418/myProjects/thisProject.git ThisFirmware"
gives me the following error
[Pipeline] sh
+ git clone ssh://guillaumed#myserver.com:29418/myProjects/thisProject.git ThisFirmware
Cloning into 'ThisFirmware'...
Host key verification failed.
fatal: Could not read from remote repository.
Please make sure you have the correct access rights
and the repository exists.
any hints?
To be sure I was using the same key, thanks to Jens, the pipeline script couldn't find the key.
Doing cat ~/.ssh/id_rsa.pub on the pipeline, resulted in + cat /c/windows/system32/config/systemprofile/.ssh/id_rsa.pub
cat: /c/windows/system32/config/systemprofile/.ssh/id_rsa.pub: No such file or directory
So I copied the ssh file from my guillaumed user ssh path to the SYSTEM ssh path but I can't use it if i'm logged in with SYSTEM user instead of guillaumed user.
what do I have to do?
this link didn't help.
Indeed, you can't checkout with ssh if you are logged as SYSTEM.
To checkout as "thisUser":
You'll need to configure your Jenkins.
1/ add a user "thisUser"
2/ add user's ssh credentials "thisUser-ssh-credentials"
3/ use ssh-agent plugin https://wiki.jenkins.io/display/JENKINS/SSH+Agent+Plugin
Then configure your pipeline
dir(server_workspace)
{
sshagent(credentials: ['thisUser-ssh-credentials'] )
{
sh "git clone ssh://thisUser#domain.com:port/yourProjects/thisProject.git"
}
}

Why do Git commands fail via Win32-OpenSSH on Windows 10?

I have set up Win32-OpenSSH on a Windows 10 system, and I am using Git for Windows 2.16.1.
I have created a bare test repository, from which I can clone just fine via the file URL:
git clone file:///c:/test.git
Cloning into 'test'...
remote: Counting objects: 33, done.
remote: Compressing objects: 100% (32/32), done.
remote: Total 33 (delta 11), reused 0 (delta 0)
Receiving objects: 100% (33/33), done.
Resolving deltas: 100% (11/11), done.
SSHD is also running fine, I can open a shell from my tablet, and access the repository directory via SFTP:
> sftp oli#localhost
oli#localhost's password:
Connected to oli#localhost.
sftp> pwd
Remote working directory: /C:/Users/Oli
sftp> cd /c:/test.git
sftp> pwd
Remote working directory: /c:/test.git
sftp> ls
HEAD config description hooks info objects packed-refs refs
sftp> bye
But cloning via SSH fails for some reason:
> git clone ssh://oli#localhost:22/c:/test.git
Cloning into 'test'...
oli#localhost's password:
fatal: ''/c:/test.git'' does not appear to be a git repository
fatal: Could not read from remote repository.
Please make sure you have the correct access rights
and the repository exists.
User "Oli" definitely has the correct access rights, and we have seen above that the repository exists and is a valid Git repository. I also tried many different syntax variants on the path.
I have read every Q/A here on Git/SSH/Windows, and the Git Pro book, and the Git reference, and the OpenSSH manuals, and the Win32-OpenSSH wiki.
What am I missing?
This has been answered by Konstantin in this question (Thanks for the link, Aurel):
The problem is - cmd.exe doesn't understand single-quoted parameters.
Git passes the path to the repository enclosed in single quotes, which does not work in cmd.exe.
This can actually be seen in the Git error message above, if you look close enough:
fatal: ''/c:/test.git'' does not appear to be a git repository
So I have set up the workaround that Konstantin suggested:
create shell scripts gup.sh and grp.sh in my home directory on the server, each containing the single line git-upload-pack.exe $* resp. git-receive-pack.exe $*
use parameter -u 'sh gup.sh' in the initial clone operation (when doing this in cmd.exe, use double quotes...)
configuring the cloned repository with commands git config remote.origin.uploadpack 'sh gup.sh'and git config remote.origin.receivepack 'sh grp.sh'
...and it works.
Sadly, Konstantin's answer is not the accepted one, and the least upvoted one. So if this helps you, too, upvote his answer!

Multiple git account doesn't work with Xcode

Due to my customer's privacy requirement, I've got create multiple account to access different git. With some helps, I've created the file config and multiple ssh keys in dir ~/.ssh. Using terminal, I've been able to perform all kind of action to the git. However, if I use XCode to perform those action, it stated that failed to authenticate.
This is the content of ~/.ssh/config:
Host bitbucket.org
HostName bitbucket.org
User git
IdentityFile ~/.ssh/id_rsa
Host bitbucket-myAccount2
HostName bitbucket.org
User git
IdentityFile ~/.ssh/acc2ssh
Inside ~/.ssh/, I've already had id_rsa, id_rsa.pub, acc2ssh, acc2ssh.pub. id_rsa is my usual git account, and acc2ssh is the other private account.
If I use terminal, it's alright, but it ask for passphrase everytime.
$ git remote -v
origin git#bitbucket-myAccount2:privateTeam/project.git (fetch)
origin git#bitbucket-myAccount2:privateTeam/project.git (push)
$ git pull
Enter passphrase for key '/Users/$myUser/.ssh/acc2ssh':
Already up-to-date.
How do I config this to work in both xcode and terminal?
Additional question: How do I make the terminal to remember my passphrase?
UPDATE
Due to the confusion, I'll add more details about the git remote:
I'm checkout out the git via:
$ git clone git#bitbucket-myAccount2:privateTeam/project.git
Because if I use the ssh provided from bitbucket, it won't work (access denied):
$ git clone git#bitbucket.org:privateTeam/project.git
repository access denied.
fatal: Could not read from remote repository.
Please make sure you have the correct access rights
and the repository exists.
But Xcode seems to doesn't recognize the host bitbucket-myAccount2 as #Jakuje pointed out. Maybe I'm missing something here?
XCode (most probably) does not read your ~/.ssh/config because it is not based on OpenSSH. You should use just the hostname as it is and provide the correct private key.
If the other one (in default location) is still picked up, move it away/rename from default location (~/.ssh/id_rsa) and update the ~/.ssh/config to reflect this change. For example:
Host bitbucket.org
HostName bitbucket.org
User git
IdentityFile ~/.ssh/id_rsa2

github who am I? (OSX)

I think I'm github USER_A, but github thinks I'm USER_B. First, I check who I am:
$ git config --list
color.ui=auto
push.default=simple
user.name=USER_A
user.email=USER_A#gmail.com
core.repositoryformatversion=0
core.filemode=true
core.bare=false
core.logallrefupdates=true
core.ignorecase=true
core.precomposeunicode=true
remote.origin.url=https://github.com/USER_A/MY_REPO.git
remote.origin.fetch=+refs/heads/*:refs/remotes/origin/*
branch.master.remote=origin
branch.master.merge=refs/heads/master
Then, I git clone my repo:
$ git clone https://github.com/USER_A/MY_REPO.git
Cloning into 'MY_REPO'...
remote: Counting objects: 9, done.
remote: Compressing objects: 100% (3/3), done.
remote: Total 9 (delta 0), reused 0 (delta 0), pack-reused 6
Unpacking objects: 100% (9/9), done.
Checking connectivity... done.
Then I change something, commit, and push:
$ git push
remote: Permission to USER_A/MY_REPO.git denied to USER_B.
fatal: unable to access 'https://github.com/USER_A/MY_REPO.git/': The requested URL returned error: 403
This the only thing Github says about this error.
Why does git/github think I'm pushing as USER_B?!
EDIT: based on comments, I ran this test:
$ ssh -T git#github.com
The authenticity of host 'github.com (192.30.252.131)' can't be established.
RSA key fingerprint is 16:27:ac:a5:76:28:2d:36:63:1b:56:4d:eb:df:a6:48.
Are you sure you want to continue connecting (yes/no)? yes
Warning: Permanently added 'github.com,192.30.252.131' (RSA) to the list of
known hosts.
Hi **USER_A**! You've successfully authenticated, but GitHub does not provide
shell access.
I don't have a ~/.ssh/config or /etc/ssh/ssh_config file. I'm on a Mac FWIW.
I found the problem, my Mac keychain had USER_B. I found it when I searched for "github" in the Keychain app. I've updated the question to reflect this is a Mac-specific problem/solution.
I had the same problem. I was able to find out what key has been used by git using following command:
ssh -v -T git#github.com
Then, I saw another key is being used by github; So, I added my default key to SSH Agent:
ssh-add ~/.ssh/id_rsa
Then, problem solved :-)
Here are a couple things I would do if your having trouble:
Run
Vics-iMac:ansible vic$ git config --list
Explanation: When you configure git typically for the first time there are a lot of things that are done during the setup phase. One of them is configuring who you are, and what email address you use. If you examine the following output from the command above. You will see the user.name being used.
user.name = BlahBlah Name
user.email = blahadklhf#gmail.com

Resources