Git bash can't access home directory on fresh machine - windows

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.

Related

Offending RSA key in /u/.ssh/known_hosts during git clone on 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.

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 prompting for password when using SSH, even when SSH keys are present (but running as admin works). What is the issue?

In a corporate environment running Windows I have the following issue:
Using git via SSH via
git clone git#server:repo.git
I am prompted for a password:
git clone git#server:repo.git
Cloning into 'repo'...
git#server's password:
The SSH Key is not found, although my SSH key resides in C:\Users\[USERNAME]\.ssh.
However, running the command shell (CMD) as administrator, everything works just fine.
What is the issue and how can it be fixed?
(Note: I am answering my own question since the issue wasted hours of my time and I did not see it in other issues having similar "access denied" symptoms).
Getting "access denied" or haven SSH keys not recognised may have a variety of reasons, see also
https://serverfault.com/questions/194567/how-do-i-tell-git-for-windows-where-to-find-my-private-rsa-key
The issue above - where git works when run as local admin, but not as user - is possibly related to a specific setup in a corporate environment. In my case the machine had a folder C:\Users\[USERNAME] but also a profile on a network mounted drive (say N). The .ssh folder with my key resided in C:\Users\[USERNAME] but git was looking in N:
Running as (Local) Administrator on the Machine changed the behaviour and used the profile from C:\Users\[USERNAME].
To find your home folder:
open Git Bash on Windows
type cd ~;pwd
The issue is ultimately related to different settings of the environment variables HOMEDRIVE, HOMESHARE or USERPROFILE, see also Git bash home directory different from Git extension than Git Bash

IntelliJ IDEA git Permission denied (publickey)

I got a strange problem with using git on IntelliJ IDEA on Windows 10.
If I want to access the remote repo on GitLab, I always get
Permission denied (publickey).
Everything I found here or via Google didn't solve the problem.
I tried:
Setting the ssh executable to Native
Converting my public key from Putty to ssh-keygen style
Removing known_hosts
I also tried to access the repo with Git Bash, it works fine like that.
Another interesting fact is that after removing known_hosts, I got "Host key verification failed". I had to use Git Bash once to recreate the known_hosts entry, after that I got the Permission denied error again. Somehow I don't get asked for any user input.
I faced this problem while connecting bitbucket cloud from my Intellij 2019.2 , which thankfully got solved with the following steps. Please note that these steps are to be carried out after you successfully generate and add ssh-public key to your bitbucket/github/gitlab profile.
Open Git-SCM ssh_config file present in Git-SCM installation directory.It's default location in Windows (for 64 bit) is in C:\Program Files\Git\etc\ssh\
Add the following lines into ssh_config
Host *your-gitlab-or-github-or-bitbucket-hostname*
IdentityFile *your-ssh-rsa-privatekey-file-with-absolute-path*
for e.g
Host bitbucket.org
IdentityFile C:/Users/Sahil/.ssh/id_rsa
Source:
https://intellij-support.jetbrains.com/hc/en-us/community/posts/360004124959-IntelliJ-2019-1-3-Can-t-access-git-using-SSH-keys
Update IDEA 2021.2.3
The bug in Sahil's answer has been fixed and Christian's solution is not needed with the correct entry in your ssh config file for the most common case of one key for one or more hosts. Additional options for complex configurations, such as multiple keys to the same or multiple hosts, can be found in Maddes comprehensive answer on superuser.
ANY inconsistency between ssh-agent and ssh-add leads to a situation where some things work and some don't.
For example, I had the Windows OpenSSH agent running but my path pointed first to the Git for Windows ssh-add. Many such failures are possible given the many ssh implementations out there, so know that you know where things are running from.
To get ssh working in IDEA...
Git for Windows
Install Git for Windows. I put it in C:\Git since, as a standard user without an elevated install, I couldn't write to C:\Program Files.
In Control Panel | User Accounts | Change my environment variables add the Git usr\bin folder to the path after the cmd folder entry already there (e.g. C:\Git\usr\bin for my installation).
Unset environment variable HOME -- it still breaks ssh in this version of IDEA.
In a Windows cmd.exe console, run start-ssh-agent.cmd to start the agent
In IDEA in Settings | SSH Configurations | Authentication type: set it to Key pair OpenSSH or PuTTY and test your connection. Then, make a small change and test Commit and push....
Windows OpenSSH
In Control Panel | User Accounts | Change my environment variables add C:\Windows\System32\OpenSSH to your path. Make sure no other ssh implementation is ahead of this in the path!
Start the OpenSSH agent in Task Manager | Services (or any number of other ways)
Set your IDEA SSH Configuration as #5 above
Keep in mind that, on Windows, ssh in IDEA uses the environment available in a standard Windows command-line console (cmd.exe). If git operations work there, then with the above steps in place, they'll work in IntelliJ.
It has been really long since OP but here is my solution on an execution basis:
You need to open a prompt and set the ENVVAR GIT_SSH_COMMAND.
You need to open IntelliJ from that same prompt.
Example:
> set GIT_SSH_COMMAND=ssh -i C:\\path\\to\\not\\default\\key
> idea
Also the path to not-default-key should use ~ instead of %userprofile% or paths unix-like using /.
I was having this same issue, and while it did have to do with the public key, my issue was concerned with WSL2/Linux and windows .ssh folders. My keys were in my WSL2/linux folder system, but intelliJ was looking in my windows folder system.
I copied my rsa keys from WSL2 to windows, and it worked automatically. In fact, I attempted to have it fail again by removing the keys from the Windows folders, but intelliJ must have it's own keylocker solution, because even without the keys in the Windows .ssh folder intelliJ continued to work.
For a screen capture explaining it you can see it here https://vimeo.com/558267383/74d55415c4
If you're using wsl2 in Clion like me, just add
Host github.com
IdentityFile /home/yieatn/.ssh/github
to /home/user/.ssh/config (create if it doesn't exit). You don't even have to restart IDE.
Adding to Cristian Torres's answer:
For all of you folks struggling with a similar problem on Mac OSX:
In IntelliJ, PyCharm, etc. click Tools -> Create Command-line Launcher...
Open your terminal and execute:
$ export GIT_SSH_COMMAND="ssh -i ~/.ssh/your-custom-private-key"
$ idea
For PyCharm the executable name is charm or /usr/local/bin/charm
Goland: goland or /usr/local/bin/goland
WebStorm: webstorm or /usr/local/bin/webstorm

Cannot get git extensions to push something to github SSH problems

Im trying to use git extensions and I really like it so far, but I don't manage to push to github. The following command works fine in git bash:
git push "origin" master:master
and then when I push with git extensions I get this:
C:\Program Files\Git\bin\git.exe push "origin" master:master
Permission denied (publickey).
fatal: The remote end hung up unexpectedly
Done
In the git settings it just has openSSH selected which I would like to keep because putty seems more hassle.
The remote addres I have added in gitextensions, actually the only thing I have done from the bash just to test is the command above and gerenating the SSH keys. All else is set up in git extensions. So I suppose everything must be set up correctly. Eg, the email adress and name are correct...
ssh git#github.com
also connects fine, from bash
edit:
So I can reproduce the error by putting the command from git-extensions in cmd. Appearantly there is a difference between running from git bash and from cmd and git-extensions runs this command like a windows command... any clues?
update:
If I choose git-bash from the menu in git-extensions I get a window that is exactly the same as when I right click in explorer on my repo folder and choose git bash here. Now, in the one opened from explorer I can push and in the one opened from extensions I get the public key problem.
Things to check for:
Presence of HOME environment variable.
Presence of %HOME%\.ssh\ and RSA keys there.
When you run git from command promt it is preferred to run git.cmd, because it fixes HOME automatically:
#if not exist "%HOME%" #set HOME=%HOMEDRIVE%%HOMEPATH%
#if not exist "%HOME%" #set HOME=%USERPROFILE%
To fix gitextensions, define HOME variable and set it to same value as USERPROFILE
Ok, I solved it.
I opened the git bash from git extensions and a git bash using the explorer shell extensions. I then ran ssh -vvv git#github.com from both to see the difference.
It turned out that because I changed the location of the git config file ( I tend to reformat every now and then, so c:\documents and settings... is notoriously bad for storing anything I might want to keep persistent), when run from git extensions git was also looking for the ssh keys in a different location. So I added the .ssh folder where my git cofig file is and now it works fine. I think git-extensions sets the HOME variable for git to whatever you set as path for the config file.
Now, that was not obvious...
The correct answer is the one from 'max' (set your HOME env var manually), but it may help some to understand why the problem is happening (as Git gets more users around the world it's going to be very common).
Cygwin sets $HOME to /home/yourname, but that variable is not known in the Windows environment. So if you open a bash window and do env | grep HOME you'll see all three 'HOME' variables mentioned here, and you might wonder why Gitextensions doesn't use your proper cygwin HOME - which is because its .bat file invocation doesn't see it - it only sees what you see from doing 'set' in a windows console.
It's mystifying why it doesn't do this evaluation later and get the proper cygwin path since it knows how to invoke bash, but (at least in versions up to 2.41) you have to do this manual change in the settings or in .gitconfig.
When you fire up git bash directly you'll land in your home folder for MSYS. You need to make sure you have your ssh key (id_rsa ?) in the .ssh subfolder (relative to the home folder).
Glad you solved the problem. Since this sounds like a serious problem I'm interested in the difference between git-bash when started from GitExtensions. In GitExtensions there is a setting that might fix this. The %HOME% directory can be changed in GitExtensios. By default it will be set to %HOMEDRIVE%%HOMEPATH%, but you can override this. Changing this probably solves your problem, since you suggest this is the problem. To change this open the settings dialog and go to the tab "git". In the section "Environment" you can set the %HOME% path.
I will appreciate it if you let me know if this also solves the problem. I'm also interested in what caused this in the first place. Maybe I can improve the check for a valid HOME directory.
Yes, not setting the HOME varible was the issue for me too.
Set the HOME variable as %USERPROFILE% and regenerate the Private and Public keys, then try cloning—it should work now.
I see that the HOME variable is set to USERPROFILE in git extensions. I can add and commit but not push/pull from the remote repository. I am unable to connect to any remote repository. We use http (not ssh) for the connection. However, using git bash I am able to connect to the remote and do push/pull. I had put my issue GIT extensions does not connect to remote but git bash does and was directed to this issue. However, I am unable to resolve my issue. Any ideas?

Resources