![為什麼 git 不使用 ssh 設定檔中設定的身份?](https://rvso.com/image/726408/%E7%82%BA%E4%BB%80%E9%BA%BC%20git%20%E4%B8%8D%E4%BD%BF%E7%94%A8%20ssh%20%E8%A8%AD%E5%AE%9A%E6%AA%94%E4%B8%AD%E8%A8%AD%E5%AE%9A%E7%9A%84%E8%BA%AB%E4%BB%BD%EF%BC%9F%20.png)
我正在嘗試連接到本地吉泰亞伺服器.我已將其設定為在連接埠 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
不使用IdentityFile
from呢~/.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