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

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?

Related

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

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.

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.

keep getting error /Users/newuser/.ssh/id_rsa: No such file or directory when trying to connect to ssh agent. Any idea what Im doing wrong?

I'm following the github instructions on how to set up ssh key and add it to the ssh agent. However after I input ssh-add -K ~/.ssh/id_rsa into the terminal, I receive this error: /Users/newuser/.ssh/id_rsa: No such file or directory
I set up my ssh key using Enter file in which to save the key (/Users/newuser/.ssh/id_rsa) so I believe its the correct name.
Any help would be appreciated!
I have been following the same instructions and meet the same problem. This is what worked for me:
cd ~/.ssh
ssh-add id_rsa
Enter file in which to save the key (/c/Users/YOURNAME/.ssh/id_rsa): id_rsa
Use the key in id_rsa.pub to add SSH Key
$ ssh -vT git#github.com
Screen will display:
You've successfully authenticated, but GitHub does not provide shell access.

Can't ssh to remote repository from command line

I'm trying to connect to a remote repository under a specific IP address.
I received the private key through email and I'm on windows using Putty. The .ppk file is in Pageant.
I also created a GIT_SSH environment variable.
After these steps my personal IP was then listed in the firewall, so I guess I was able to connect.
However, when I use git bash to connect to the server, I get the message "permission denied (public key)".
The commands I used are:
$ ssh [IP]
$ ssh [username]#IP
$ ssh -T [username]#IP
I looked up online exhaustively but with no success so far and I really need to access the repo as soon as possible.
Thanks in advance!
Obrigada :)
Two ways to go about it
Using Pageant directly inside Git Bash
This requires setting the GIT_SSH environment variable to the full path of plink.exe (which is part of the putty suite).
Also, I would highly recommend checking if plink.exe works inside GIT Bash as follows:
Full-path-to-plink.exe -v -P port sshusername#sshserver
Another thing to check is the version of Putty suite that you are using. ( I can dig out the details later but) version 0.60 had a bug due to which processes inside cmd could not access Pageant. I believe the issue was fixed in v 0.62 but feel free to use the latest (v 0.67)
Convert putty key to OpenSSH format
Another possibility is exporting the putty key to the OpenSSH format by using puttygen.exe and the using the exported key directly inside GIT Bash:
Open PuttyGen
Click Load
Load your private key
Go to Conversions->Export OpenSSH and export your private key
Copy your private key to ~/.ssh/id_dsa (or id_rsa).
Create the RFC 4716 version of the public key using ssh-keygen
ssh-keygen -e -f ~/.ssh/id_dsa > ~/.ssh/id_dsa_com.pub
Convert the RFC 4716 version of the public key to the OpenSSH format:
ssh-keygen -i -f ~/.ssh/id_dsa_com.pub > ~/.ssh/id_dsa.pub
Then you can do something like this in GIT Bash to test the ssh connectivity:
GIT_SSH=ssh
ssh -v sshusername#sshserver -i ~/.ssh/id_rsa

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