Loading

Git设置多个ssh密钥

一、生成ssh密钥

推荐使用 Ed25519 算法(更安全)

ssh-keygen -t ed25519 -C "your_email@example.com"

或使用 RSA 算法(兼容性更好)

ssh-keygen -t rsa -b 4096 -C "your_email@example.com"

注:

  • -C(可选)参数默认为用户名@设备名
  • Ed25519默认长度为256-bit
  • Ed25519安全强度等效RSA 3072-bit
  • RSA 2048-bit已不再推荐,易被破解

二、创建配置文件

%USERPROFILE%\.ssh的路径下创建配置文件config
或使用Git Bash输入命令
当前文件夹:

touch config

用户文件夹:

touch ~/.ssh/config

三、配置

# github
Host github.com
	HostName github.com
	PreferredAuthentications publickey
	IdentityFile ~/.ssh/github_key

# github
Host github2.com
	HostName github.com
	PreferredAuthentications publickey
	IdentityFile ~/.ssh/github2_key

# gitee
Host gitee.com
	HostName gitee.com
	PreferredAuthentications publickey
	IdentityFile ~/.ssh/gitee_key

Host 别名,用户命令行使用
HostName 托管平台服务器地址
PreferredAuthentications 权限认证(publickey, password, keyboardinteractive)
IdentityFile 证书文件位置(私钥文件)

四、测试

连接测试

ssh -T git@github.com
ssh -T git@github2.com
ssh -T git@gitee.com

导入方法

原始导入地址

git@github.com:ganlinte/GKD-subscription.git

如果想要使用第二个密钥验证,使用别名替换地址中的原始地址

git@github2.com:ganlinte/GKD-subscription.git

五、单仓库

单仓库设置SSH密钥

git config core.sshCommand "ssh -i ~/.ssh/id_rsa_specific -F /dev/null"
  • -F /dev/null:告诉 SSH 忽略全局配置文件,完全使用你提供的指令。

查看

git config --get core.sshCommand

删除

git config --unset core.sshCommand
posted @ 2025-06-17 14:24  Techenergy  阅读(103)  评论(0)    收藏  举报