Peter Oettig
Peter Oettig

Reputation: 551

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:

  1. Setting the ssh executable to Native
  2. Converting my public key from Putty to ssh-keygen style
  3. 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.

Upvotes: 40

Views: 74231

Answers (8)

Dinoshna
Dinoshna

Reputation: 1

I have tried these steps below which resolved my issue,

Issues occurs with ssh and Github push / fetch Step-1: add your GitHub account to Idea.. Step-2: check the ssh key and add if not exists (Configure ur access with GitHub) use below commands: The error message you’re encountering, “Permission denied (publickey),” typically occurs when Git is unable to authenticate using your SSH key. Let’s troubleshoot this issue:

Check Your SSH Key Setup: Ensure you have an SSH key pair generated. If not, create one using the following command:

  ssh-keygen -t rsa -b 4096 -C "[email protected]"

This command generates a new SSH key. You can press Enter for the default path and passphrase. Verify that the public key (id_rsa.pub) exists in your ~/.ssh directory. Add Your SSH Key to GitHub: Copy your public key using:

      cat ~/.ssh/id_rsa.pub  or copy from your .ssh/id_rsa.pub path and paste it in GitHub account as below step...

Add this key to your GitHub account by navigating to Settings > SSH and GPG keys and pasting it there. Test SSH Connection: Run the following command to verify your SSH connection to GitHub:

     ssh -T [email protected]

You should receive a welcome message in your console. Update Your Git Remote URL: If your Git remote URL starts with https or http, switch to using SSH:

      git remote rm origin
      git remote add origin [email protected]:<username>/<repo>.git

Replace and with your GitHub username and repository name. Try Pushing Again: Navigate to your project folder and run: git push -u origin master

This should work now.

Upvotes: 0

Adam
Adam

Reputation: 6117

On Windows 11, a fresh install (no Putty or anything, just the built-in OpenSSH server that is in the Optional Features of Windows). First add your keys:

ssh-add c:\work\gitlabKey c:\work\githubKey
ssh-add -l  #this will verify the keys were added

If you get an error that the agent isn't running, it's a Windows Service that you need to enable and set to start-up on boot -- do this in Powershell:

Set-Service   ssh-agent -StartupType Automatic
Start-Service ssh-agent

Once the keys are added, PHPstorm still wasn't using them to connect when I tried to create a new project. So adding this to my Environment Variables did the trick (reboot may be required)

name=GIT_SSH
value=C:\windows\System32\OpenSSH\ssh.exe

(you will need to restart your IDE or possibly computer)

If you see this error Clone failed: CreateProcessW failed...Host key verification failed then open your Terminal and do a manual connection to the site first

ssh -T [email protected]

SSH

Upvotes: 0

Sahil
Sahil

Reputation: 920

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.

  1. 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\
  2. 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

Upvotes: 66

maslick
maslick

Reputation: 3370

Adding to Cristian Torres's answer:

For all of you folks struggling with a similar problem on Mac OSX:

  1. In IntelliJ, PyCharm, etc. click Tools -> Create Command-line Launcher...

  2. 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

Upvotes: 0

cb4
cb4

Reputation: 9383

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

  1. 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.
  2. 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).
  3. Unset environment variable HOME -- it still breaks ssh in this version of IDEA.
  4. In a Windows cmd.exe console, run start-ssh-agent.cmd to start the agent
  5. 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

  1. 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!
  2. Start the OpenSSH agent in Task Manager | Services (or any number of other ways)
  3. 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.

Upvotes: 6

isudfv
isudfv

Reputation: 457

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.

Upvotes: 1

POBrien
POBrien

Reputation: 101

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

Upvotes: 1

Cristian Torres
Cristian Torres

Reputation: 206

It has been really long since OP but here is my solution on an execution basis:

  1. You need to open a prompt and set the ENVVAR GIT_SSH_COMMAND.
  2. 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 /.

Upvotes: 4

Related Questions