配置Git ssh-key

在Windows 11的Git Bash中为阿里云Codeup和GitHub配置独立的ED25519密钥,并实现自动切换,需按以下步骤操作:

步骤1:生成两对独立密钥

1.1 生成GitHub密钥

ssh-keygen -t ed25519 -C "your_email@github.com" -f ~/.ssh/github_ed25519
  • 生成私钥:~/.ssh/github_ed25519
  • 生成公钥:~/.ssh/github_ed25519.pub

1.2 生成阿里云Codeup密钥

ssh-keygen -t ed25519 -C "your_email@codeup.aliyun.com" -f ~/.ssh/codeup_ed25519
  • 生成私钥:~/.ssh/codeup_ed25519
  • 生成公钥:~/.ssh/codeup_ed25519.pub

步骤2:配置SSH客户端(~/.ssh/config)

编辑~/.ssh/config文件(若无则创建),添加以下内容:

# GitHub 配置
Host github.com
	HostName github.com
	User git
	IdentityFile ~/.ssh/github_ed25519
	PreferredAuthentications publickey

# 阿里云Codeup配置
Host codeup.aliyun.com
	HostName codeup.aliyun.com
	User git
	IdentityFile ~/.ssh/codeup_ed25519
	PreferredAuthentications publickey
  • 关键点:通过Host字段定义不同平台的别名,IdentityFile指定对应密钥。

步骤3:添加私钥到SSH Agent

启动SSH Agent并加载密钥:

eval "$(ssh-agent -s)"
ssh-add ~/.ssh/github_ed25519
ssh-add ~/.ssh/codeup_ed25519
  • 验证密钥是否加载成功:ssh-add -l

步骤4:在平台账户中添加公钥

4.1 GitHub

  • 登录GitHub → Settings → SSH and GPG keys → New SSH key
  • 粘贴~/.ssh/github_ed25519.pub内容

4.2 阿里云Codeup

  • 登录Codeup → 个人设置 → SSH公钥 → 添加公钥
  • 粘贴~/.ssh/codeup_ed25519.pub内容

步骤5:测试连接

5.1 测试GitHub

ssh -T git@github.com

成功提示:Hi username! You've successfully authenticated...

5.2 测试Codeup

ssh -T git@codeup.aliyun.com

成功提示:Welcome to Codeup!

步骤6:配置Git仓库使用SSH

6.1 修改现有仓库URL(示例)

# GitHub仓库
git remote set-url origin git@github.com:user/repo.git 

# Codeup仓库
git remote set-url origin git@codeup.aliyun.com:project/repo.git

6.2 新建仓库时直接使用SSH URL

  • 从平台复制SSH格式的仓库地址(如git@github.com:user/repo.git

步骤7:验证Git操作自动选择密钥

执行git pushgit pull时,SSH客户端会根据Host自动匹配密钥:

  • 操作GitHub仓库 → 使用github_ed25519
  • 操作Codeup仓库 → 使用codeup_ed25519

常见问题解决

  1. 权限错误
  2. 确保私钥权限为600chmod 600 ~/.ssh/github_ed25519
  3. 确保~/.ssh/config权限为644
  4. 连接超时/拒绝
  5. 检查HostName是否拼写正确(如github.com而非git.github.com
  6. 确认平台SSH服务状态正常
  7. 密钥未自动选择
  8. 运行ssh -v git@github.com查看调试信息,确认IdentityFile是否正确加载

补充:密钥管理最佳实践

  • 备份密钥:将私钥备份至安全位置(如密码管理器、加密U盘)
  • 定期轮换密钥:每6-12个月更新密钥,减少泄露风险
  • 禁用密码登录:在平台账户中关闭密码验证,强制使用SSH密钥

通过上述配置,Git将根据仓库的远程URL自动选择对应的ED25519密钥,实现无缝的跨平台操作。

暂无评论

发送评论 编辑评论


				
|´・ω・)ノ
ヾ(≧∇≦*)ゝ
(☆ω☆)
(╯‵□′)╯︵┴─┴
 ̄﹃ ̄
(/ω\)
∠( ᐛ 」∠)_
(๑•̀ㅁ•́ฅ)
→_→
୧(๑•̀⌄•́๑)૭
٩(ˊᗜˋ*)و
(ノ°ο°)ノ
(´இ皿இ`)
⌇●﹏●⌇
(ฅ´ω`ฅ)
(╯°A°)╯︵○○○
φ( ̄∇ ̄o)
ヾ(´・ ・`。)ノ"
( ง ᵒ̌皿ᵒ̌)ง⁼³₌₃
(ó﹏ò。)
Σ(っ °Д °;)っ
( ,,´・ω・)ノ"(´っω・`。)
╮(╯▽╰)╭
o(*////▽////*)q
>﹏<
( ๑´•ω•) "(ㆆᴗㆆ)
😂
😀
😅
😊
🙂
🙃
😌
😍
😘
😜
😝
😏
😒
🙄
😳
😡
😔
😫
😱
😭
💩
👻
🙌
🖕
👍
👫
👬
👭
🌚
🌝
🙈
💊
😶
🙏
🍦
🍉
😣
Source: github.com/k4yt3x/flowerhd
颜文字
Emoji
小恐龙
花!
上一篇
下一篇
error: Content is protected !!