Can't connect to Gitlab repositories with multiple keys - windows

I have two Gitlab repositories, owned by two different users. Let's call them
personal: git#gitlab.com:user1/personal.git
work: git#gitlab.com:user2/work.git
I got two RSA keys: with key1 I can usually connect to personal repo; with key2 to work repo.
I have to push/pull from different workstations (Mac OS / Windows); on every machine, I can connect to the first or the second repo with no problems.
From a single Windows 10 workstation I need to connect to both repositories and here start the troubles.
On that workstation, I use Git Bash.
According to different guides (e.g. https://coderwall.com/p/7smjkq/multiple-ssh-keys-for-different-accounts-on-github-or-gitlab), I've setup a ~/.ssh/config file:
Host gitlab-personal
HostName gitlab.com
User git
LogLevel DEBUG3
PreferredAuthentications publickey
IdentityFile ~/.ssh/personal
#IdentitiesOnly yes
Host gitlab-work
HostName gitlab.com
User git
LogLevel DEBUG3
PreferredAuthentications publickey
IdentityFile ~/.ssh/work
#IdentitiesOnly yes
If I run
ssh -Tv git#gitlab-personal
I got:
...
Welcome to Gitlab, User1!
...
And if I run
ssh -Tv git#gitlab-work
I got:
...
Welcome to Gitlab, User2!
...
This makes me think that RSA keys are setup correctly (even in Putty / Pageant).
But if I try to clone the repositories, for example
git clone git#gitlab-personal:user1/personal.git
or
git clone git#gitlab-work:user2/work.git
..in both cases I got this response:
Unable to open connection:
Host does not existfatal: Could not read from remote repository.
Please make sure you have the correct access rights
and the repository exists.
What am I doing wrong?
Thanks to anyone who can help me.

Related

How to avoid github ssh authentication problem that happens every time?

I used the vscode and install git in the system on mac. And registered the ssh key in GitHub and the device. By the way, when I git clone or git push, pull I have to register every time like this
ssh-add ~/.ssh/id_rsa even though I pull the public repository. When I work in the windows system in the past I don't get this status. I hope someone helps me to avoid this. Here id_rsa is the ssh key file for github.
Have you configured GitHub in ~/.ssh/config?
The config should include something like this:
Host github.com
Hostname github.com
User <your-github-username>
IdentityFile ~/.ssh/id_rsa
IdentitiesOnly yes
You can also use the AddKeysToAgent yes setting for the host, if you want the SSH Agent to remember the password of your SSH key, so you only have to enter it when using it the first time after every login.
If you want to pull from a public repository, you can avoid working with SSH keys, if you use HTTPS instead of SSH (note that this would require password authentication if you want to push).

Adding ssh-keys in windows for private git

might be a dumb question.... but where do we add the ssh keys of the workstation to the git server both being purely on windows 2012 R2... ..Without the use of github
i had created a remote in the git server repo as
git remote add origin edscs-npe\bzky7x#IP:/c/temp/repo.git
In the workstation i am trying to clone the link as ..
$ git clone edscs-npe\bzky7x#IP:/c/temp/repo.git
Cloning into 'repo'...
ssh: connect to host "IP" port 22: Connection refused
fatal: Could not read from remote repository.
Please make sure you have the correct access rights
and the repository exists.
Now the problem I think here is on the authentication between both the workstation and the git server... where do I add the ssh keys of the workstation to the git server both being windows machine...
Much help would be appreciated
Now the problem i think here is on the authentication between both the workstation and the git server...
To test this, just ssh from the workstation to the server. If it prompts you for a password and you are able to type it in and connect, then yes, you need to set up your ssh keys. If not, then there is more setup you need to do on the server end that is outside the scope of this question.
First, on your workstation you need to set up your public/private keys. You can do this with a command like:
ssh-keygen -t rsa
After hitting enter to accept the defaults, you should get some files in your ~/.ssh folder.
Now you need to add your public key to the authorized_keys on the server account you use for your git operations (You will need to enter a password for this operation)
cat ~/.ssh/id_rsa.pub | ssh user#server 'cat >> .ssh/authorized_keys'
You should be able to both interactively ssh into the server and use git to clone from the server.

Using git with SSH-key on Windows

I am connecting to my virtual machine with ssh like this:
ssh -i keyfile.key user#server.com
I have created a git repository on the server which I want to push to, but I do nott know how I should use the keyfile.key with git.
I am supposed to execute the command:
git push live master
But I getting the error "permission denied (publickey)", so how do I specify the key? I am on Windows 8.
You need to:
use a, ~/.ssh/config (check where %HOME% is set, since you are on Windows).
The config file would look like (replace <hostkey> by any string you want)
Host <hostkey>
HostName myserver
User user
IdentityFile C:\path\to\.ssh\keyfile.key
IdentitiesOnly yes
change your remote url to one using the config Host key
git rmeote set-url list <hostkey>:myrepo

Git SSH Issue on Windows Server 2012

I have set up my ssh key in ~/.ssh and tested it on another PC (working)
ssh to git#bitbucket.org OK
ssh to bitbucket.org FAIL
git clone form git#bitbucket.org:account/gmc.git FAIL
Any idea what is happening in this case and I am not able to git clone ?
user#pc //server/users$/user
$ ssh git#bitbucket.org
Enter passphrase for key '/c/Users/user/.ssh/id_rsa':
logged in as thedev.
You can use git or hg to connect to Bitbucket. Shell access is disabled.
Connection to bitbucket.org closed.
user#pc //server/users$/user
$ ssh bitbucket.org
Permission denied (publickey).
user#pc //server/users$/user
$ git clone git#bitbucket.org:account/gmc.git
Cloning into 'gmc'...
The server's host key is not cached in the registry. You
have no guarantee that the server is the computer you
think it is.
The server's rsa2 key fingerprint is:
ssh-rsa 2048 97:8c:1b:f2:6f:14:6b:5c:3b:ec:aa:46:46:74:7c:40
Connection abandoned.
fatal: Could not read from remote repository.
Please make sure you have the correct access rights
and the repository exists.
know_hosts file
bitbucket.org,131.103.20.167 ssh-rsa abc...==
Seems like the problem was conflict between Open SSH and PLINK.exe (used by putty)
Details here:
Git server's host key not cached in registry - GitHub.com
Got it working by changing the GIT_SSH env var to /usr/bin/ssh.exe and now using openssh.

SSH error on push to an existing project Permission denied (publickey)

Made all necessary steps to use the GitLab, these settings are:
Created a rsa as described in this link
Copied the code generated in {my key}.pub and added this code in GitLab
In my existent repository added to url with command git remote add gitlab git#gitlab.com:ridermansb/breezenhibernateproblem.git
I tried to make the push git push -u gitlab master
My .ssh/config
Host gitlab.com
HostName gitlab.com
IdentityFile C:\Users\Riderman\.ssh\gitlab_rsa
IdentitiesOnly yes
Error below:
Warning: Permanently added 'gitlab.com,54.243.197.170' (RSA) to the list of known hosts.
Permission denied (publickey).
fatal: Could not read from remote repository.
Please make sure you have the correct access rights
and the repository exists.
I made a video showing all the steps: http://www.screenr.com/euVH
What am I doing wrong?
After seeing your screencast, I see that you don't have a %HOME%\.ssh\config file.
This is important because:
by default, ssh will look for the public/private key in %HOME%\.ssh\id_rsa(.pub)
With an scp-like syntax (git#gitlab.com:yourRepo, with the ':' as separator), ssh could look in a .ssh/config file for the location of the actual public/private key, using gitlab.com as an entry in said config file.
Add %HOME%\.ssh\config with:
Host gitlab.com
HostName gitlab.com
IdentityFile C:\path\to\.ssh\gitlab_rsa
IdentitiesOnly yes
and your git push -u gitlab master will work.
It turned out it was also about setting HOME correctly:
%HOME% was not correct.
I configured the variable %HOME% to point to %USERPROFILE% locally and it worked
Mysygit does set HOME, but if you are using git outside of a git-cmd session, then it is your responsibility to set HOME correctly.

Resources