簡體   English   中英

IntelliJ IDEA git 權限被拒絕(公鑰)

[英]IntelliJ IDEA git Permission denied (publickey)

我在 Windows 10 上的 IntelliJ IDEA 上使用 git 時遇到了一個奇怪的問題。如果我想訪問 GitLab 上的遠程存儲庫,我總是得到Permission denied (publickey) 我在這里或通過谷歌找到的一切都沒有解決問題。 我試過:

  1. 將 ssh 可執行文件設置為 Native
  2. 將我的公鑰從 Putty 轉換為 ssh-keygen 樣式
  3. 刪除known_hosts

我還嘗試使用Git Bash訪問存儲庫,它可以正常工作。 另一個有趣的事實是,在刪除 known_hosts 后,我得到“主機密鑰驗證失敗”。 我不得不使用 Git Bash 重新創建 known_hosts 條目,之后我再次收到 Permission denied 錯誤。 不知何故,我沒有被要求提供任何用戶輸入。

我在從 Intellij 2019.2 連接 bitbucket 雲時遇到了這個問題,幸好通過以下步驟得到了解決。 請注意,這些步驟將在您成功生成並添加 ssh-public 密鑰到您的 bitbucket/github/gitlab 配置文件后執行。

  1. 打開 Git-SCM 安裝目錄中的 Git-SCM ssh_config文件。它在 Windows 中的默認位置(對於 64 位)在 C:\\Program Files\\Git\\etc\\ssh\\
  2. 將以下行添加到 ssh_config
Host *your-gitlab-or-github-or-bitbucket-hostname*
IdentityFile *your-ssh-rsa-privatekey-file-with-absolute-path*

例如

Host bitbucket.org
IdentityFile C:/Users/Sahil/.ssh/id_rsa

來源: https : //intellij-support.jetbrains.com/hc/en-us/community/posts/360004124959-IntelliJ-2019-1-3-Can-t-access-git-using-SSH-keys

自 OP 以來已經很久了,但這是我基於執行的解決方案:

  1. 您需要打開提示並設置 ENVVAR GIT_SSH_COMMAND
  2. 您需要從同一提示打開 IntelliJ。

示例:

> set GIT_SSH_COMMAND=ssh -i C:\\path\\to\\not\\default\\key
> idea

此外,not-default-key 的路徑應該使用~而不是 %userprofile% 或使用/的類似 unix 的路徑。

更新 IDEA 2020.3

Sahil 的答案中的錯誤已得到修復,並且對於一台或多台主機的一個密鑰的最常見情況,在 ssh 配置文件中正確輸入不需要 Christian 的解決方案。 復雜配置的其他選項,例如同一或多個主機的多個密鑰,可以在超級用戶的 Maddes 綜合回答中找到。

ssh-agent 和 ssh-add 之間的任何不一致都會導致某些事情起作用而某些事情不起作用的情況。

例如,我運行了 Windows OpenSSH 代理,但我的路徑首先指向 Windows ssh-add的 Git。 鑒於存在許多 ssh 實現,許多此類失敗都是可能的,因此要知道您知道事情是從哪里運行的。

要讓 ssh 在 IDEA 中工作...


適用於 Windows 的 Git

  1. 安裝適用於 Windows 的 Git。 我將它放在 C:\\Git 中,因為作為沒有提升安裝的標准用戶,我無法寫入C:\\Program Files
  2. Control Panel | User Accounts | Change my environment variables Control Panel | User Accounts | Change my environment variables Control Panel | User Accounts | Change my environment variables將 Git usr\\bin文件夾添加到cmd文件夾條目之后的路徑中(例如C:\\Git\\usr\\bin用於我的安裝)。
  3. 如果您設置了環境變量 HOME(就像我為 Git 所做的那樣)將其刪除; IDEA 如果設置就會中斷(整個星期都在與 JetBrains 支持一起工作)。
  4. 在 Windows cmd.exe 控制台中,運行 start-ssh-agent.cmd 以啟動代理
  5. 在 IDEA 中Settings | SSH Configurations | Authentication type: Settings | SSH Configurations | Authentication type: Settings | SSH Configurations | Authentication type:將其設置為Key pair OpenSSH or PuTTY並測試您的連接。 然后,做一個小改動並測試Commit and push...

視窗 OpenSSH

  1. Control Panel | User Accounts | Change my environment variables Control Panel | User Accounts | Change my environment variables Control Panel | User Accounts | Change my environment variablesC:\\Windows\\System32\\OpenSSH到您的路徑。 確保路徑中沒有其他 ssh 實現位於此之前!
  2. Task Manager | Services啟動 OpenSSH 代理Task Manager | Services Task Manager | Services (或任何數量的其他方式)
  3. 將您的 IDEA SSH Configuration為上面的 #5

請記住,在 Windows 上,IDEA 中的 ssh 使用標准 Windows 命令行控制台 (cmd.exe) 中可用的環境。 如果 git 操作在那里工作,那么通過上述步驟,它們將在 IntelliJ 中工作。

我遇到了同樣的問題,雖然它確實與公鑰有關,但我的問題與 WSL2/Linux 和 windows .ssh 文件夾有關。 我的密鑰在我的 WSL2/linux 文件夾系統中,但 intelliJ 正在我的 windows 文件夾系統中查找。

我將我的 rsa 密鑰從 WSL2 復制到 Windows,它會自動工作。 事實上,我試圖通過從 Windows 文件夾中刪除密鑰來讓它再次失敗,但是 intelliJ 必須有它自己的 keylocker 解決方案,因為即使沒有 Windows .ssh 文件夾中的密鑰,intelliJ 也繼續工作。

有關解釋它的屏幕截圖,您可以在此處查看https://vimeo.com/558267383/74d55415c4

如果你像我一樣在 Clion 中使用 wsl2,只需添加

Host github.com
        IdentityFile /home/yieatn/.ssh/github

到 /home/user/.ssh/config (如果不退出則創建)。 您甚至不必重新啟動 IDE。

添加到Cristian Torres的回答中:

對於在Mac OSX上遇到類似問題的所有人:

  1. 在 IntelliJ、PyCharm 等中,單擊Tools -> Create Command-line Launcher...

  2. 打開終端並執行:

$ export GIT_SSH_COMMAND="ssh -i ~/.ssh/your-custom-private-key"
$ idea

對於 PyCharm,可執行文件名稱是charm/usr/local/bin/charm

Goland: goland/usr/local/bin/goland

WebStorm: webstorm/usr/local/bin/webstorm

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM