Windows環(huán)境git配置多個ssh的實現(xiàn)步驟
Windows 環(huán)境下解決 SSH 連接 GitHub 要求密碼的問題
在 Windows 環(huán)境下解決 SSH 連接 GitHub 持續(xù)要求密碼的問題需要特別注意 Windows 系統(tǒng)的特性。以下是針對 Windows 的完整解決方案:
完整解決方案(Windows 專用)
1. 使用 Git Bash 進行操作
所有命令都應(yīng)在 ??Git Bash?? 中執(zhí)行(不是 CMD 或 PowerShell)
2. 驗證 SSH 配置文件
# 打開配置文件 notepad ~/.ssh/config
確保內(nèi)容為:
Host github.com HostName github.com User git IdentityFile ~/.ssh/id_ed25519_github IdentitiesOnly yes
3. 檢查密鑰文件是否存在
ls -la ~/.ssh
應(yīng)該看到:
id_ed25519_github
(私鑰)id_ed25519_github.pub
(公鑰)
4. 設(shè)置正確的文件權(quán)限(Windows 關(guān)鍵步驟)
# 設(shè)置 .ssh 目錄權(quán)限 icacls "$HOME/.ssh" /reset icacls "$HOME/.ssh" /inheritance:r icacls "$HOME/.ssh" /grant:r "$USERNAME:(OI)(CI)F" # 設(shè)置私鑰權(quán)限 icacls "$HOME/.ssh/id_ed25519_github" /reset icacls "$HOME/.ssh/id_ed25519_github" /grant:r "$USERNAME:F" # 設(shè)置公鑰權(quán)限 icacls "$HOME/.ssh/id_ed25519_github.pub" /reset icacls "$HOME/.ssh/id_ed25519_github.pub" /grant:r "$USERNAME:F" # 設(shè)置配置文件權(quán)限 icacls "$HOME/.ssh/config" /reset icacls "$HOME/.ssh/config" /grant:r "$USERNAME:F"
5. 啟動 SSH 代理并添加密鑰
# 啟動 SSH 代理 eval $(ssh-agent -s) # 添加 GitHub 密鑰 ssh-add ~/.ssh/id_ed25519_github # 驗證密鑰已添加 ssh-add -l
6. 修復(fù)行尾格式(Windows 常見問題)
# 安裝 dos2unix(如果未安裝) curl -L https://sourceforge.net/projects/dos2unix/files/dos2unix/7.4.1/dos2unix-7.4.1-win64.zip/download -o dos2unix.zip unzip dos2unix.zip -d /usr/bin # 轉(zhuǎn)換 config 文件格式 dos2unix ~/.ssh/config
7. 測試詳細(xì)連接
ssh -Tv git@github.com
在輸出中查找:
debug1: Offering public key: /c/Users/yourusername/.ssh/id_ed25519_github ED25519 SHA256:... debug1: Server accepts key: /c/Users/yourusername/.ssh/id_ed25519_github ED25519 SHA256:...
Windows 特定問題解決方案
1. 確保使用正確的路徑格式
在 ~/.ssh/config
文件中,使用以下格式:
IdentityFile C:/Users/yourusername/.ssh/id_ed25519_github
而不是:
IdentityFile ~/.ssh/id_ed25519_github # 在 Windows 可能有問題
2. 檢查 SSH 代理服務(wù)
Windows 可能需要手動啟動 SSH 代理服務(wù):
# 確保 SSH 代理運行 eval $(ssh-agent -s) # 如果提示找不到代理,手動啟動 ssh-agent -s
3. 檢查系統(tǒng)環(huán)境變量
確保 Git Bash 的環(huán)境變量設(shè)置正確:
# 檢查 HOME 變量 echo $HOME # 如果未設(shè)置,手動設(shè)置 export HOME=/c/Users/yourusername
4. 使用絕對路徑添加密鑰
ssh-add /c/Users/yourusername/.ssh/id_ed25519_github
驗證公鑰是否添加到 GitHub
顯示公鑰內(nèi)容:
cat ~/.ssh/id_ed25519_github.pub
登錄 GitHub > Settings > SSH and GPG keys
確保:
- 公鑰完全匹配
- 密鑰類型為 ED25519
- 沒有多余的空格或換行
備用解決方案
1. 創(chuàng)建全新的 SSH 配置
# 備份舊配置 mv ~/.ssh ~/.ssh_backup # 創(chuàng)建新配置 mkdir ~/.ssh notepad ~/.ssh/config
粘貼:
Host github.com HostName github.com User git IdentityFile C:/Users/yourusername/.ssh/id_ed25519_github IdentitiesOnly yes
2. 生成新密鑰
# 生成新密鑰 ssh-keygen -t ed25519 -f ~/.ssh/id_ed25519_github -C "your_email@example.com" # 添加到 GitHub clip < ~/.ssh/id_ed25519_github.pub # 復(fù)制到剪貼板
3. 使用 Windows 憑據(jù)管理器
# 刪除所有保存的憑據(jù) git credential-manager reject https://github.com git credential-manager reject git@github.com # 清除緩存 git credential-manager clear
最終測試
ssh -T git@github.com
成功時應(yīng)顯示:
Hi yourusername! You've successfully authenticated, but GitHub does not provide shell access.
如果所有方法都失敗
1. 使用 HTTPS 替代 SSH
# 設(shè)置全局使用 HTTPS git config --global url."https://github.com/".insteadOf "git@github.com:"
2. 使用 GitHub Desktop
安裝 GitHub Desktop 并登錄您的賬戶,它會自動處理認(rèn)證
3. 使用 GitHub CLI
# 安裝 GitHub CLI winget install --id GitHub.cli # 登錄 GitHub gh auth login
Windows SSH 連接檢查表
步驟 | 命令 | 預(yù)期結(jié)果 |
---|---|---|
1. 驗證配置文件 | cat ~/.ssh/config | 顯示正確的 GitHub 配置 |
2. 檢查密鑰文件 | ls -la ~/.ssh | 顯示私鑰和公鑰文件 |
3. 設(shè)置權(quán)限 | icacls 命令 | 無錯誤輸出 |
4. 啟動代理 | eval $(ssh-agent -s) | 顯示代理 PID |
5. 添加密鑰 | ssh-add ~/.ssh/id_ed25519_github | "Identity added" 消息 |
6. 測試連接 | ssh -T git@github.com | 成功認(rèn)證消息 |
請按照這些步驟操作,特別注意 Windows 的文件權(quán)限和路徑格式問題。如果問題仍然存在,請?zhí)峁?ssh -Tv git@github.com
的完整輸出,我可以進一步幫助診斷。
到此這篇關(guān)于Windows環(huán)境git配置多個ssh的實現(xiàn)步驟的文章就介紹到這了,更多相關(guān)git配置多個ssh內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
linux系統(tǒng)使用vscode進行qt開發(fā)的過程分享
最近在Linux上搞Qt,搞的一頭霧水,小編把整個過程記錄下,分享需要的朋友,如果大家對linux系統(tǒng)使用vscode進行qt開發(fā)相關(guān)知識感興趣的朋友跟隨小編一起看看吧2021-12-12HTTP協(xié)議簡介_動力節(jié)點Java學(xué)院整理
這篇文章主要介紹了HTTP協(xié)議簡介,小編覺得挺不錯的,現(xiàn)在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧2017-07-07VSCode報錯:Vetur?can't?find?'tsconfig.json'?
最近在使用VScode打開項目時發(fā)現(xiàn)報錯了,所以下面這篇文章主要給大家介紹了關(guān)于VSCode報錯:Vetur?can‘t?find?‘tsconfig.json‘?or?‘jsconfig.json‘解決辦法,需要的朋友可以參考下2022-08-08