Git for Windows: Cannot add host to known_hosts. Wrong user directory - windows

Git for Windows worked fine with SSH authentication. However, the SSH sever signature has changed. I deleted the ~/.ssh/known_hosts file in the hope that ssh will recreate it.
However, when I try to push to the remote via SSH, I get the following message:
Let's assume my Windows username is "abc".
$ git push
Could not create directory '/home/abc/.ssh' (No such file or directory).
Failed to add the host to the list of known hosts (/home/abc/.ssh/known_hosts).
git#my_bitbucket.hu: Permission denied (publickey).
fatal: Could not read from remote repository.
On Windows I don't have the /home/abc directory. $HOME is set to "/c/Users/abc".
How can I fix this? I tried several solutions from On windows, could not create directory '/home/<username>/.ssh', but nothing worked.
In the Wondows CMD %HOME% and %USERPROFILE% are set to "/c/Users/abc". Why is ssh trying to touch the file under "/home/abc/.ssh"?

Try the same from a CMD session (no git bash), with:
%HOME% and %USERPROFILE% are set to "C:\Users\abc"
%PATH% set to:
set PATH=C:\WINDOWS\system32;C:\WINDOWS;C:\WINDOWS\System32\Wbem;C:\WINDOWS\System32\WindowsPowerShell\v1.0\
set "GH=C:\Program Files\Git"
set "PATH=%GH%\bin;%GH%\cmd;%GH%\usr\bin;%GH%\mingw64\bin;%GH%\mingw64\libexec\git-core;%PATH%"
no SSH environment variable set ( check with set|grep -i ssh)
Then try again, from that same CMD session, again without bash.

Related

git clone with SSH only working in Git Bash not on Windows CMD

So, I've followed this tutorial on how to Setup SSH for github with Windows CMD and all was working fine until I went to clone a repo with
git clone git#github.com:{myusername}/{myrepo}.git
Where I get
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.
Even when I run ssh -T git#github.com I get the expected message telling me I'm authenticated.
After scratching my brain for a while, I decided to try it on git bash.
First thing I noticed was that running
ssh-add -l
in git bash, I was getting The agent has no identities. but when I run the same command on Windows CMD I get all my SSH keys?
So, after adding my ssh key in git bash I was able to clone my repository.
So, why is it only on git bash I can do this and not on the cmd or powershell? Is it something to do with what seems like they are using different ssh agents? How can I sync them together if that is the case?
Furthermore, when I run the following command
ssh -Tv git#github.com
with the cmd I get
debug1: identity file C:\\Users\\{myuserdirectory}/.ssh/id_rsa type 0
debug1: key_load_public: No such file or directory
but with git bash I get
debug1: identity file /c/Users/{myuserdirectory}/.ssh/id_rsa type 0
Another difference is that in windows cmd I don't get any instances of
debug1: Will attempt key: ....
When I exit git bash and open up another git bash terminal, running ssh-add -l again, it returns The agent has no identities. even after I added it before, it's like it only persists for each session, which also isn't desirable.
Any help with this would be greatly appreciated!
Probably you were right and they were using different ssh-agents. I had exactly the same problem and this answer helped me a lot:
https://stackoverflow.com/a/40720527/6486458
By default git refers to its own ssh in C:\Program Files\Git\usr\bin. I added GIT_SSH environment variable and set it to C:\Windows\System32\OpenSSH\ssh.exe. This prevents inconsistency between the versions of ssh. After that git started to work as expected from both Git Bash and Windows cmd.
From git documentation:
GIT_SSH, if specified, is a program that is invoked instead of ssh
when Git tries to connect to an SSH host. It is invoked like $GIT_SSH [username#]host [-p <port>] <command>.
See also this answer: https://stackoverflow.com/a/8713121/6486458
Looks like your ssh-agent is not running or not recognize your ssh key
try this:
# add the default ~/.ssh keys to the ssh-agent
ssh-add
# restart the ssh-agent
eval $(ssh-agent)
# On windows:
start-ssh-agent
ssh-add
ssh-add adds RSA or DSA identities to the authentication agent, ssh-agent.
When run without arguments, it adds the files ~/.ssh/id_rsa, ~/.ssh/id_dsa and ~/.ssh/identity.
Alternative file names can be given on the command line
There is a weird bug on Windows if you install Git bash. Open Command prompt, and do
ls ~/.ssh
if you find this folder already created, then copy the public and private key from your user folder to this path:
cp C:\Users\username\.ssh\id_* ~/.ssh/
For some reason, windows command prompt creates this path the first time you do a git clone, and after that it just requests for git#gitlab / git#github password.

On windows, could not create directory '/home/<username>/.ssh'

To configure git on windows with gitbash, (as explained here):
I generated a ssh key with ssh-keygen -t rsa -b 4096 -C "your_email#example.com" at /c/Users/my-username/.ssh.
then, added the ssh key to ssh-agent with ssh-add ~/.ssh/id_rsa
The problem is now, every-time I try to connect to github via ssh, there is an error message with a reference to a wrong location:
Could not create directory '/home/my-username/.ssh'.
How to fix this?
Things I tried:
I checked in the env variable and in C:/Program Files/Git/etc/ssh/ssh_config, but didn't find anything…
And I set the HOME environment variable pointing to %USERPROFILE% for ssh to pick it up with setx HOME %USERPROFILE%.
In this answer, someone having the same problem with cygwin, modified a file in cygwin to fix the problem. Is there something similar in my case?

Git clone / pull continually freezing at "Store key in cache?"

I'm attempting to clone a repo from my BitBucket account to my Windows 10 laptop (running GitBash). I've completed all of the steps necessary to connect (set up my SSH key, verified by successfully SSHing git#bitbucket.org, etc). However, whenever I attempt to clone a repo, the prompt continually hangs up after confirming that I want to cache Bitbucket's key.
User#Laptop MINGW64 /C/Repos
$ git clone git#bitbucket.org:mygbid/test.git
Cloning into 'test'...
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
If you trust this host, enter "y" to add the key to
PuTTY's cache and carry on connecting.
If you want to carry on connecting just once, without
adding the key to the cache, enter "n".
If you do not trust this host, press Return to abandon the
connection.
Store key in cache? (y/n) y
No files are cloned, and the result is an empty repo. Trying to initiate a git pull origin master from this repo also asks to cache the key, then hangs with no feedback. Despite not asking for the key to be cached when I do a test SSH, git operations always ask for the key every time before failing.
With no error messages to work with, I'm really at a loss as to what is wrong. I've tried multiple repos, including very small ones, with no success at all.
I had this problem when cloning a repo on Windows 10 too.
I got around it by using the Putty GUI to SSH to the server in question (in your case: bitbucket.org) then clicked 'Yes' when the prompt asks if you want to save the server key to the cache. Running the clone command again then worked for me!
Open Putty
Type in the Host Name (like bitbucket.org)
Click Open
Click yes in the popup to cache the host key
Close Putty
I managed to get it working by running plink directly, after pageant is running use the plink command directly - plink.exe -agent -v git#github.com then after this git works without hanging.
To do this from powershell open a powershell window and paste in the following:
echo y | & 'C:\Program Files (x86)\GitExtensions\PuTTY\plink.exe' -ssh git#github.com
echo y | & 'C:\Program Files (x86)\GitExtensions\PuTTY\plink.exe' -ssh git#gist.github.com
echo y | & 'C:\Program Files (x86)\GitExtensions\PuTTY\plink.exe' -ssh git#bitbucket.org
or with PuTTY standalone version:
echo y | & 'C:\Program Files (x86)\PuTTY\plink.exe' -ssh git#github.com
echo y | & 'C:\Program Files (x86)\PuTTY\plink.exe' -ssh git#gist.github.com
echo y | & 'C:\Program Files (x86)\PuTTY\plink.exe' -ssh git#bitbucket.org
Also worth knowing is that putty stores known hosts under a registry key:
HKEY_CURRENT_USER\SoftWare\SimonTatham\PuTTY\SshHostKeys
To shortcut the above you could put the following in a .reg file and run it:
Windows Registry Editor Version 5.00
[HKEY_CURRENT_USER\SOFTWARE\SimonTatham\PuTTY\SshHostKeys]
"rsa2#22:github.com"="0x23,0xab603b8511a67679bdb540db3bd2034b004ae936d06be3d760f08fcbaadb4eb4edc3b3c791c70aae9a74c95869e4774421c2abea92e554305f38b5fd414b3208e574c337e320936518462c7652c98b31e16e7da6523bd200742a6444d83fcd5e1732d03673c7b7811555487b55f0c4494f3829ece60f94255a95cb9af537d7fc8c7fe49ef318474ef2920992052265b0a06ea66d4a167fd9f3a48a1a4a307ec1eaaa5149a969a6ac5d56a5ef627e517d81fb644f5b745c4f478ecd082a9492f744aad326f76c8c4dc9100bc6ab79461d2657cb6f06dec92e6b64a6562ff0e32084ea06ce0ea9d35a583bfb00bad38c9d19703c549892e5aa78dc95e250514069"
"rsa2#22:gist.github.com"="0x23,0xab603b8511a67679bdb540db3bd2034b004ae936d06be3d760f08fcbaadb4eb4edc3b3c791c70aae9a74c95869e4774421c2abea92e554305f38b5fd414b3208e574c337e320936518462c7652c98b31e16e7da6523bd200742a6444d83fcd5e1732d03673c7b7811555487b55f0c4494f3829ece60f94255a95cb9af537d7fc8c7fe49ef318474ef2920992052265b0a06ea66d4a167fd9f3a48a1a4a307ec1eaaa5149a969a6ac5d56a5ef627e517d81fb644f5b745c4f478ecd082a9492f744aad326f76c8c4dc9100bc6ab79461d2657cb6f06dec92e6b64a6562ff0e32084ea06ce0ea9d35a583bfb00bad38c9d19703c549892e5aa78dc95e250514069"
"rsa2#22:bitbucket.org"="0x23,0xb9b88df3578371a7eb80c78bcda14fb30da436f11ca932a5fd5a8b6adfcc681df7a59cb4cb7ac966d9eac11daa38ebdbc0a6582a210ed4ee95a8d101c4abc925e942ab47535d64f9a5b3b68035c2ea1e900d709a1e8ea938718f532f9805a190446b92bac3040126225ae9d8374bc2008f106979d631734c7453f78c70091f4783b288869cb3c1941a784cd9baad823be27333833dc1f488a45b85952be75cf0a64965662302e3915378dcd5cfcd3ec903d804a29dff2fdf19df5deba4534b09e4dea6e44f152e339b3c43be98ddadfc56533192e216a3d673f00b4aa9cc9e7870acd8b6adb7e0feb77f2292fc2dede94819def3eb1e785541a06ab31ccf725f"
putty-hosts.reg gist
To workaround this problem I configured GitBash to use plink with -batch option. The option disables all prompts - the plink will terminate without hanging and won't add any key fingerprint to cache.
To add -batch parameter to plink command executed by GitBash you can set a git config option:
git config --global core.sshCommand "plink -batch"
Or set GIT_SSH_COMMAND environment variable.
The output when you cloning a repo from unknown host will be similar to this:
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.
After this message you can add a key to cache with command:
echo y | plink git#bitbucket.org
REMARK: Please check if plink is in your PATH. Alternatively use UNIX-like path in the GitBash config option, e.g.:
/c/Program\ Files/PuTTY/plink.exe -batch
Even after performing the workaround mentioned in other answers, you may encounter an error like:
FATAL ERROR: Disconnected: No supported authentication methods available (server sent: publickey)
To solve both problems at once, change git bash to use SSH instead of PuTTY by adding the following to your ~/.profile file (C:\Users\<Username>\.profile). If you don't already have this file, then create a new file with this line.
GIT_SSH="/usr/bin/ssh.exe"
Then open a new git bash window and try your git clone or git pull again.
Note that this may require you to create an SSH key if you don't already have one. To do this, follow the instructions on the Bitbucket site.
See this SO question for related info.
In your git bash shell, check for existence of GIT_SSH:
echo $GIT<tab><tab>
If it exists and is set to putty, execute:
unset GIT_SSH
You'll probably want to put this into one of the git bash startup scripts.
This is NOT a universal solution. It worked in our particular case.
It sounds a bit silly, but after trying all of the above, I decided to reinstall Git Bash with default options and it worked.
If you use KiTTY (instead of PuTTY), it has -auto-store-sshkey argument.
So, you can set GIT_SSH_COMMAND (or git config --global core.sshCommand) to something like c:/KiTTY/klink.exe -auto-store-sshkey.
The output still contains information about new key and the question, but it doesn't wait for the answer:
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 ssh-ed25519 key fingerprint is:
ssh-ed25519 255 2e:65:6a:c8:cf:bf:b2:8b:9a:bd:6d:9f:11:5c:12:16
If you trust this host, enter "y" to add the key to
PuTTY's cache and carry on connecting.
If you want to carry on connecting just once, without
adding the key to the cache, enter "n".
If you do not trust this host, press Return to abandon the
connection.
Store key in cache? (y/n)
Autostore key is on
Default SSH port to connect to is 7999

Windows 7. Git public key configuration

I have followed the instructions here: http://help.github.com/win-set-up-git/ to set up git on my windows 7 machine.
I can use "git bash" and run commands. I am prompted for my key's pass phrase and can successfully connect. E.g.:
$git push -u origin master
Enter passphrase for key '/c/Users/mbj/.ssh/id_rsa':
Everything up-to-date
Also this:
$ ssh -T git#github.com
Enter passphrase for key '/c/Users/mbj/.ssh/id_rsa':
Hi markbrenigjones! You've successfully authenticated, but GitHub does not provi
de shell access.
$ which ssh
/bin/ssh
However, when I open a regular windows command prompt and run the same command, I get this:
C:\util\meta_vimrc>git push
Permission denied (publickey).
fatal: The remote end hung up unexpectedly
Also this:
C:\util\meta_vimrc>ssh -T git#github.com
Permission denied (publickey).
C:\util\meta_vimrc>which ssh
C:\Program Files (x86)\Git\bin\ssh.exe
How do I get this working from the non "git bash" prompt?
Perhaps the program has no idea where your HOME directory is when running from the Windows cmd.exe interpreter. For this to work, the ssh configuration directory with your key has to be found, namely /c/Users/mbj/.ssh.
Set this by running:
set HOME=C:\Users\mbj\
What works for me is to run PuTTY's Pageant.exe and add your private key to it. Then plink.exe knows where to find the key no matter what.

Git SSH client for windows and wrong path for .ssh/config file

I installed Git-1.7.9-preview20120201.exe, and when I try to access my repository via SSH it failed.
I investigated and found that if I run SSH -T git#myrepo.com, SSH doesn't load ~/.ssh/config where a custom port is defined.
When I try SSH -F c:\users\tomas\.ssh\config git#myrepo.com everything is OK.
I used Process Monitor and found that SSH is trying to find \\\\.ssh\config here C:\Windows\CSC\v2.0.6\namespace\.ssh.
Why?
How I can force it to use normal path? %HOMEPATH%\.ssh\config?
set the HOME environment variable pointing to %USERPROFILE% for ssh to pick it up. This usually seems to be the problem for many unix solutions that run on windows.
Fast access to set user env variables:
Start > Type "env" > Select "Environmental variables for your account"
I found that windows msygit port store ssh config here <installPath>\Git\etc\ssh\ssh_config

Resources