Offending RSA key in /u/.ssh/known_hosts during git clone on windows - windows

I am trying to git clone a repository on my windows machine, with:
git clone -b <branch> ssh://<host>
I have already added the keys, and deleted the existing known_hosts file in C:\Users<username>.ssh
But the git clone fails with
###########################################################
# WARNING: REMOTE HOST IDENTIFICATION HAS CHANGED! #
###########################################################
IT IS POSSIBLE THAT SOMEONE IS DOING SOMETHING NASTY!
Someone could be eavesdropping on you right now (man-in-the-middle attack)!
It is also possible that a host key has just been changed.
The fingerprint for the DSA key sent by the remote host is
SHA256:<fingerprint>.
Please contact your system administrator.
Add correct host key in /u/.ssh/known_hosts to get rid of this message.
Offending RSA key in /u/.ssh/known_hosts:6
Where is this known_hosts (/u/.ssh/known_hosts) file? C:\Users<username>\.ssh does not have this file, I was expecting for it to get created in the git clone command.
The same command works fine on my ubuntu PC.

The mentioned path has an linux-ish look, which indicates you were probably running your command from git-bash.
The simplest way to fix your issue is to edit the mentioned file (/u/.ssh/known_hosts) from a git-bash shell.

Check your environment variable HOME: in a CMD, type set HOME.
If it is set to U:\ (translated, in a bash session, to /u/), that would explain why SSH is lookig for information (keys, config file, known_hosts file, ...) in U:\.ssh
Change your HOME environment variable (back to, for instance, %USERPROFILE%) to make sure SSH looks for keys where you expect it.

Related

How to automatically use a specific SSH key when using Git through a JetBrains IDE (i.e. PyCharm) on Windows?

I would like to use a specific SSH key when using Git through PyCharm on Windows.
Things I have tried:
Tried configuring SSH by adjusting the ~/.ssh/config (and permissions) as mentioned in these answers:
https://stackoverflow.com/a/31044770/8488985
https://stackoverflow.com/a/11251797
https://superuser.com/a/232406 (See for comments about permissions)
This enabled Git when used in the Git Bash command line, however, it did not work when using the IDE.
Tried adding the SSH key to the ssh-agent as mentioned in here:
https://stackoverflow.com/a/4565746
Again, this enabled Git when used in the Git Bash command line, however, it did not work when using the IDE.
What else can I do?
Apparantly JetBrains IDEs (like PyCharm) use the Windows commandline to interact with git.
See here
So the first step is to startup the Windows Command Prompt and check if you can use Git with a specific SSH key that way.
In my case this was not possible, even though I had configured the config file located at ~/.ssh/config (when accessed through Git Windows) or C:\Users\<Me>\.ssh\config.
This is why the SO posts mentioned in my question did not solve my problem.
The second step is to figure out which config file instead is used by Git when accessed through the Windows Command Prompt.
This provided me the clue that Git on Windows can use an internal ssh config file called ssh_config (instead of config) and located near the git.exe (instead of in a subdir of your homedir).
I found Git's location using where git which indicated C:\Program Files\Git-2.16.1\cmd\git.exe.
So the config I was looking for was located at C:\Program Files\Git-2.16.1\etc\ssh\ssh_config.
The third step is adjusting that ssh_config file similar to the config file.
For me this looked like this:
Host <some-name>
HostName gitlab.com
IdentityFile C:\Users\<Me>\.ssh\<my-non-default-key>
# Prevents usage of the default IdentityFile
# See: https://stackoverflow.com/a/11251797
IdentitiesOnly yes
# Prevents warning when pulling from Gitlab.
# See: https://stackoverflow.com/a/67556915
UpdateHostKeys no
The fourth and last step is to change the remote in the Git repository such that it uses the name <some-name> specified in the ssh_config file.
Get the original remote first using git remote -v.
This gives something like this:
git#gitlab.com:some/subdirs/to/the/repository.git
Now swap out the gitlab.com part for <some-name> specified in the ssh_config file, like this:
git remote set-url origin git#<some-name>:some/subdirs/to/the/repository.git
Summary:
When using a JetBrains IDE like PyCharm it uses Git over the Windows Command Prompt
Find out the location of Git in the Windows Command Prompt
Adjust the ssh_config file located at this Git location
Set the remote url of the repository such that it uses the host name specified in the ssh_config file

Git configuration on windows for ssh access to Github

I have a Github account with multiple projects.
I have cloned these projects to my windows 10 computer using ssh key.
some time later, it appear my ssh keys were compromise so i delete my ssh on Github and locally, then regenerate new ones :
4096 bits
rsa2
saved the pub as ~/.ssh/git_id_rsa.pub
add the pub key to my Github ssh keys
saved the priv as ~/.ssh/git/git_id_rsa.ppk
converted to Openssh format as ~/.ssh/git_id_rsa
the fact is I want different ssh keys for my different web tools (one for GitHub, one for DigitalOcean, one for OVH cloud, etc...) and I want to specify witch key to use when connecting to each host. thats why I change the default name for the ssh generated with PuttyGen (and converted with the same).
I don't have passphrase on the git_id_rsa.ppk (nor git_id_rsa) keys, as it seems that GitHub dont like them (saw long time ago).
I've configure my (projecytdir)/.git/config like bellow :
[remote "origin"]
url = git#github.com:hdGuild/JenkinsServerOnDO.git
fetch = +refs/heads/*:refs/remotes/origin/*
identityfile= ~/.ssh/git_id_rsa
But when I try a Git Pull, I've got the following error :
git#github.com: Permission denied (publickey).
fatal: Could not read from remote repository.
Please make sure you have the correct access rights
and the repository exists.
Following the following answers I've created the ~/.ssh/config file with the following (using OpenSSH key for GitHub), and it works :
Host github.com
IdentityFile ~/.ssh/git_id_rsa
The fact is that if I need the ~/.ssh/config file, why would I need to configure my local git with (projecytdir)/.git/config as I describe it above ?
I would like to configure my local git to use the right ssh key for Github, without having to explain it in my ~/.ssh/config file.
I'm sure it is (again) a configuration problem and I miss something somewhere, but I can't find where.
Please help :)
thank-you
Okay,
As previously, I answer my own question :D
After some more research, I found this post that explain how to specify in Git local config file, the ssh key to use for git sh connection.
The command is simply, in the above explained case, the following while in project directory :
git config core.sshCommand "ssh -i ~/.ssh/git_id_rsa -F /dev/null"
This way, the git_id-rsa will be used by git for ssh connections on this particular project (as it is a local configuration).
Thank-you for reading.

Git bash can't access home directory on fresh machine

I have just got my new laptop (Windows 7) and wanted to clone first repository. I'm using ssh connection and private key. To my surprise, git bash can't see my home directory to work with .ssh default directory:
$ git clone git#bitbucket.org:<blablabla>
Cloning into 'XXX'...
Could not create directory '/u//.ssh'.
The authenticity of host 'bitbucket.org (104.192.143.3)' can't be established.
RSA key fingerprint is SHA256:zzXQOXSRBEiUtuE8AikJYKwbHaxvSc0ojez9YXaGp1A.
Are you sure you want to continue connecting (yes/no)? yes
Failed to add the host to the list of known hosts (/u/.ssh/known_hosts).
Instead of standard /C/Users/my_name i see /u in the paths - where did that come from? Looks like outside of Git Bash everything works ok, I also checked %USERPROFILE% env variable and it is set correctly.
putting more details to clarify some questions
I'm not using Cygwin. I only installed Git-2.10.1-64, entered empty directory C:\Workspace, right clicked and chose Git Bash Here.
$HOME directory is set to /u/ - so I'm guessing this is the root of the problem. Should I forcefully change this variable in some of Git Bash's initial scripts?
Current directory is C:\Workspace.
I have added HOME environment variable in Windows which points to my home directory - Git Bash can resolve it correctly now. I'm not sure whether it's best solution or some kind of workaround, though.

Unable to clone git repository from siteground

I'm trying to set up MS WebMatrix to use a Git repository from my siteground hosting account. I created the repository using their cpanel plugin and it tells me that I can clone it using this command
git clone ssh://username#sm3.siteground.biz:18765/home/username/public_html/
I replaced username of course and I created an rsa key using ssh-keygen. In the Webmatrix GUI it just opens a window saying "Clone is in progress" but it doesn't to anything.
And when I run that command in PowerShell, this is the output:
Cloning into 'public_html'...
Permission denied (publickey).
fatal: Could not read from remote repository.
Please make sure you have the correct access rights
and the repository exists.
Any help is highly appreciated!
EDIT:
I haven't used github before but I'm pretty sure I'm not connecting to it. The repository is on siteground's server I think. Anyway, I couldn't figure it out in PowerShell so now I'm using putty to load the appropriate key and connect using an external git tool (source tree) that doesn't use the same ssh client as PowerShell. That is the solution that's working for me now.
I'll leave this question open as maybe someone comes around and can help with how to set this up using PowerShell.
The missing piece to the Siteground guide is:
Create a blank file in ~/.ssh/ or C:\Users\username\.ssh on your computer. It does not matter what you name it. I named it siteground_dsa. You could also name it id_dsa_siteground.
Copy the private ssh key that you get from siteground.com and paste the whole of it in the this newly created file.
Open Git Bash locally on your computer and run the following command
$ eval ssh-agent -s
Then run the following. Remember to use the filename that you gave it.
$ ssh-add ~/.ssh/siteground_dsa
Now you need to enter the passphrase for the ssh key. You will have defined it when creating the ssh key.
Now you should be logged in and you can run git clone the directory of your wish.
git clone ssh://username#ams14.siteground.eu:18765/home/username/public_html/
To permanently add the SSH key extend ~/.ssh/config with the following and updating server_name and username.
Host server_name
User username
Port 18765
IdentityFile ~/.ssh/siteground_dsa
keep in mind that for Windows operators, you should write eval $(ssh-agent)
eval $(ssh-agent)
chmod 600 file_name
ssh-add C:\Users\username\.ssh\siteground
Then you can easily clone your file into your local server following inserting your passphrase of the SSH key.
GitHub isn't able to authenticate you. Probably your key isn't associated with your GitHub account.
Take a look to GitHub's recommended method

Permission denied (publickey) errors on Windows when using Moovweb

I'm able to authenticate, generate, push etc just fine with my SSH keys and Moovweb credentials on my Mac and Linux machines.
However, on my Windows machine, using Git Bash, I get an SSH Permission denied (publickey) error. The error message is below:
$> moov generate 123dsfsdsf nytimes.com
Running environment checks.
Verifying that git is installed...OK
Checking that current 123dsfsdsf directory doesn't exist...OK
Registering project with MoovCloud.
Authenticating with MoovCloud.
Checking for git access...Enter passphrase for key '/Users/firstname.lastname/.ssh/id_rsa':
Enter passphrase for key '/Users/firstname.lastname/.ssh/id_rsa':
FAILED
> Need to upload an ssh key in order to generate a project...
Found the following SSH public keys:
1 ) id_rsa.pub
2 ) new_rsa.pub
Which would you like to use with your Moovweb account? 2
Uploading public key...
Successfully uploaded public key new_rsa.pub as 'firstname.lastname#GGT.local'
You are now ready to push projects to MoovCloud!
Creating project in MoovCloud...OK
Generating files...OK
Cloning project locally.
Enter passphrase for key '/Users/firstname.lastname/.ssh/id_rsa':
Enter passphrase for key '/Users/firstname.lastname/.ssh/id_rsa':
Cloning into '123dsfsdsf'...
Permission denied (publickey).
fatal: Could not read from remote repository.
Please make sure you have the correct access rights
and the repository exists.
ERROR: Error cloning git repo: exit status 128
Please try cloning the repository (git clone moov#git.moovweb.com:firstnameglastname/123dsfsdsf.git) again later.
Try 'moov help generate' to find out details.
Seems like a Windows-specific SSH error. Any workarounds?
So as mentioned in prior answers, the Permission denied error in Windows is because you are trying to use a key other than id_rsa.
Windows lacks the bells and whistles that Linux and Mac have to try out all your public keys when trying to connect to a server via SSH. If you're using the ssh command, you can tell it which key to use by passing the -i flag followed by the path to the key to use:
ssh -i ~/.ssh/moovweb_rsa moov#git.moovweb.com
The above command should work just fine if you've uploaded moovweb_rsa.pub to the console (either via the moov login command or the console UI). However, trying any git related commands should fail because Git doesn't give you the ability to chose which key to use when connecting to the git remote. Because of this, SSH is forced to use the default key, id_rsa, and if that key doesn't work (or doesn't exist), then the connection fails with a permission denied error.
One possible solution, as suggested in other answers, is to simply rename your key to id_rsa. For most people, this is a fine solution. However, if you already have an id_rsa key and you would prefer to use a different key with Moovweb, you can edit your ~/.ssh/config file by adding the following contents:
Host git.moovweb.com
IdentityFile ~/.ssh/moovweb_rsa
If you append the above lines to your ~/.ssh/config file (create it if it doesn't exist), you should be able to successfully get Git to communicate with the Moovweb remote git server. The config basically tells SSH that for the given host (git.moovweb.com), SSH should use the given key rather than the default.
It's worth nothing that this happens to all Git remotes; interactions with Github, Heroku, etc... also suffer through this problem in Windows. You could easily extend your ~/.ssh/config file to use separate SSH keys for each one of those services if you so desired:
Host git.moovweb.com
IdentityFile ~/.ssh/moovweb_rsa
Host github.com
IdentityFile ~/.ssh/github_rsa
Host heroku.com
IdentityFile ~/.ssh/heroku_rsa
Quick & dirty solution: use only the default id_rsa.pub key
Some notes:
make sure you enter the right passphrase to id_rsa.pub
do not use your other key, new_rsa.pub
It turns out that Windows Git Bash doesn't quite come with all the cool utilities Mac/Linux users are used to. Specifically, you don't have ssh-agent running to help handle multiple keys. Without ssh-agent, the git command only seems to use the default id_rsa.pub key.
You can verify this is an SSH/Windows issue following Github's awesome SSH troubleshooting guide. You'll get a Permission denied (publickey) no matter which SSH/Git server you try to connect to.

Resources