IntelliJ IDEA git Permission denied (publickey) - windows

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

Related

How to fix SSH / Git connections after windows update broke them? Issue seems to be related to ssh-agent

I use Windows 10 / PyCharm / GitHub Desktop as my developer environment.
I use ssh keys to access my remote git repository. Everything on my computer functioned perfectly a week ago, but now it does not.
I have Github Desktop installed, and that is not affected. I can use Github Desktop to pull/push/etc from the repository. My issue is with the command line tools & with the git tool integrations into PyCharm (my IDE).
I have my RSA keys in my $HOME/.ssh/ folder and a config file in $HOME/.ssh/config that has not changed...but now everytime I attempt to do a "git fetch" command from CMD/Powershell/Git Bash I get prompted for a password. This password prompt is showing up because it is NOT reading my SSH config file and it is NOT using my RSA key!!!!
I know this is the case because I can "fix" the issue in Git Bash by executing the following commands:
eval $(ssh-agent -s)
ssh-add C:/Users/MyUsername/.ssh/id_rsa
After executing those commands, everything works perfectly in Git Bash.
I have spent HOURS googling this issue and restarrting my SSH services, rebooting my computer, resintalling my IDE, and even attempting to do a system restore back to last week....NONE of them fix the issue.
WHY do I need to add my ssh keys to Git Bash everytime I open it? It seems like it is not reading the keys/config file from the default $HOME/.ssh directory. How do I fix this? What is going on? I'm about ready to throw this computer out the window!!!
Restarting Software - Nothing
Rebooting Computer - Nothing
Copying $HOME/.ssh/config to multiple directories as suggested by google search results - Nothing
Using Github Desktop - WORKS, but I want the CLI tools / integrated Git flow in PyCharm to work!
Using Git Bash with "eval $(ssh-agent -s); ssh-add" workaround - WORKS but very cumbersome. Why doesn't it remember the keys session to session? Why do I need to execute every time?
Hacky solution:
I used ssh -Tvvv git#gitlab both before and after executing the "eval $(ssh-agent -s); ssh-add " workaround and loaded them into Beyondcompare and noticed that the tool was trying to grab keys from C:\SPB_Data directory in the 'before' (I have no idea what this directory is) and C:\Users\MyUsername 'after':
debug1: Next authentication method: publickey
debug1: Trying private key: /c/SPB_Data/.ssh/id_rsa
debug3: no such identity: /c/SPB_Data/.ssh/id_rsa: No such file or directory
So I simply copied all the files from C:\Users\Myusername.ssh into C:\SPB_Data.ssh and then everything worked.
The root-cause of this issues seems to be the installation of Allegro / Cadence software which OVERWRITES the $HOME environment variable and causes OpenSSH to read from that folder instead of the Windows home folder.

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

On Windows 10 that is well configured with native OpenSSH's ssh-agent, how to have an implementation of git use the ssh-agent configured?

I have configured Native Windows 10 OpenSSH like described here: https://www.makeuseof.com/tag/windows-10-ssh-vs-putty/
Also I have started as a service ssh-agent, added private key, and between restart my private key password's are remembered.
So mainly, I can do the following immediately after a restart without a prompt: ssh -T git#gitlab.com
However, I don't know how to get any implementation of 'git' to delegate the ssh work to the Windows native ssh-agent, so that I can execute without any prompt a clone like: git clone git#gitlab.com:SandboxAtHoopSoft/create-aws-accounts.git
Solutions found are:
Set OS environment variable:
set GIT_SSH=C:\WINDOWS\System32\OpenSSH\ssh.exe
OR
Set git config:
git config --global core.sshCommand "'C:\Windows\System32\OpenSSH\ssh.exe'"
Just solved this myself this was a very frustrating journey but for me the following got me going:
Make sure C:\Windows\System32\OpenSSH\ is in your path variables (yea I know Windows should see this on its own)
Go to C:\Users\<your user>\.gitconfig edit your global git config file to point to the correct ssh.exe: C:/Windows/System32/OpenSSH
Make sure your id_rsa.ppk file is in C:\Users\<your user>\.ssh If you don't have an SSH key yet generate one and make sure it ends up in this folder
You may also want to check that your OpenSSH Authentication Agent is started in Windows Services

Problems using GIT w/ SSH key file under Windows

I've problems using GIT with the shell, Visual Studio Code and Visual Studio 2015 (community edition).
Earlier this year I started using GIT with eGit (under Eclipse of course) without any problems. I installed GIT on my server, created a ssh key pair and after a few tests it worked (and still works).
However, now I want to use that GIT on my server with Visual Studio Code and maybe later with Visual Studio 2015.
I got stuck on this issue: GIT trys to use my PPK and asks me for the passphrase (which is emtpy). If I enter a wrong phrase it asks again and continues only if I answer correct (simply enter key). So I think everything is OK till here. But then it asks for the user password for the git-user (the right one btw) on the server and fails. Why?
In the log of my server I can't see any entry about logging in using the key file, only the errors about trying to log in with the password (which is disabled).
If I use putty with that key file I'm able to connect to my GIT server and I also get an entry in the log file. So I'm sure that everything is OK with the server.
What am I missing? I would appreciate any help about that!
Using it with Visual Studio 2015 (I can't solve the issue that it doesn't find libssh when I try to recompile the GIT module following the well known blog entry from Bernardo Pastorelli) or saving the enter key press would be a bonus, however, I would be happy if I could use GIT with key file under Shell / Visual Studio Code at all.
OS: Windows 7 64 bit
GIT 2.10.2.windows.1
VSC: V 1.7.2
TIA!
I'll try to answer the simpler question in your Post:
I would be happy if I could use GIT with key file under Shell
Once, that is done, you can build on it.
Git for windows uses Openssh and therefore will not be able to use the putty PPK file directly.
Two ways forward
Option 1: Convert PPK file to OpenSSH format
Steps to do so:
Open your private key in PuTTYGen
Top menu “Conversions”->”Export OpenSSH key”.
Save the new OpenSSH key when prompted.
To use this new openssh key for your git server, do the following:
Open up Git Bash shell and there edit ~/.ssh/config (create ~/.ssh/ if it does not exist) and define this host:
Host AuxBurgerGitServer
Hostname whatevers-your-git-remote-is
User the-git-user
IdentityFile ~/.ssh/the-open-ssh-key-exported-before
Test this by doing a ssh -T AuxBurgerGitServer which should not show any errors.
If you go this way, you should use the HOST defined above whenever referring to any repositories on this host. Therefore, for example, to clone a repo you would do something like:
git clone ssh://AuxBurgerGitServer/some-repo-name
Option 2: Configure your GIT to use pageant
You can load your PPK file in pageant and configure GIT to use pageant for authentication.
For this, the only thing you would need is to setup an environment variable like so using:
Control Panel → System → Advanced system settings → Environment variables
(or on Windows 10: Control Panel → Search → Environment variables)
GIT_SSH=c:\Program Files\Putty\plink.exe

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