為什麼 git 不使用 ssh 設定檔中設定的身份?

為什麼 git 不使用 ssh 設定檔中設定的身份?

我正在嘗試連接到本地吉泰亞伺服器.我已將其設定為在連接埠 2222 上使用整合 SSH 伺服器。 Gitea 運作良好。

現在我想使用連接Cygwin 的 git。為了測試與我的存儲庫的連接,我正在使用ls-遠端如果我使用以下命令,該命令可以正常工作GIT_SSH_COMMAND像這樣的選項:

GIT_SSH_COMMAND="ssh -i ~/.ssh/id_rsa" git ls-remote --exit-code -h ssh://username@localhost:2222/username/Repo.git

接下來我想使用以下方法來簡化生活~/.ssh/config

host gitea
 HostName localhost
 Port 2222
 IdentityFile ~/.ssh/id_rsa
 User username

但是,這確實失敗並出現錯誤Unable to open connection

git ls-remote --exit-code -h ssh://gitea/username/Repo.git

問題:IdentityFile沒有應用。這有效:

GIT_SSH_COMMAND="ssh -i ~/.ssh/id_rsa" git ls-remote --exit-code -h ssh://gitea/username/Repo.git

但我確信我的方法~/.ssh/config是正確的,因為透過直接連接ssh -vv gitea是有效的。輸出(摘錄):

[...]
debug1: Connecting to localhost [::1] port 2222.
debug1: Connection established.
[...]
debug1: Offering public key: RSA SHA256:XXX /home/username/.ssh/id_rsa
debug2: we sent a publickey packet, wait for reply
debug1: Server accepts key: pkalg ssh-rsa blen 535
debug2: input_userauth_pk_ok: fp SHA256:XXX
debug1: Authentication succeeded (publickey).
Authenticated to localhost ([::1]:2222).
[...]

那為什麼git不使用IdentityFilefrom呢~/.ssh/config

答案1

事實證明這樣設定GIT_SSH_COMMAND="ssh"就夠了。這意味著git正在使用其他一些 SSH 客戶端。可能是 OpenSSH 恰好位於系統路徑上:

$ whereis ssh
ssh: /usr/bin/ssh.exe /cygdrive/c/WINDOWS/System32/OpenSSH/ssh.exe /usr/share/man/man1/ssh.1.gz

我通過添加解決了這個export GIT_SSH_COMMAND="/usr/bin/ssh"問題~/.bash_profile

相關內容