git 多账号 ssh-key 管理(github和gitlab共同使用)

本文详细介绍了如何在同一台电脑上配置多个GitHub账号的方法,包括清除全局设置、生成新的SSH keys、配置config文件等内容。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

日常使用 git 作为仓库使用时,会遇到以下情况:

  1. 有两个 github 账号(至少两个),一台电脑同时连接这两个账号进行维护
  2. 私人 github 账号,公司 gitlab 账号

1 清除 git 的全局设置(针对已安装 git)

新安装 git 跳过。

若之前对 git 设置过全局的 user.name 和 user.email。类似(用git config --global --list 进行查看你是否设置)

$ git config --global user.name "你的名字"
$ git config --global user.email  "你的邮箱"
复制代码

必须删除该设置

$ git config --global --unset user.name "你的名字"
$ git config --global --unset user.email "你的邮箱"复制代码

2 生成新的 SSH keys

(1)#GitHub的钥匙

ssh-keygen -t rsa -C "xxx@qq.com"
Enter file in which to save the key (/Users/kingboy/.ssh/id_rsa): /Users/kingboy/.ssh/github_id_rsa

(2)#gitlab

ssh-keygen -t rsa -C "xxx@xx.com"
Enter file in which to save the key (/Users/kingboy/.ssh/id_rsa): /Users/kingboy/.ssh/gitlab_id_rsa

注意:输入的是钥匙的位置和名称。github和gitlab是不同的。

(3)完成后会在~/.ssh/目录下生成以下文件:

  • github_id_rsa
  • github_id_rsa.pub
  • gitlab_id_rsa
  • gitlab_id_rsa.pub

3 添加识别 SSH keys 新的私钥

默认只读取 id_rsa,为了让 SSH 识别新的私钥,需要将新的私钥加入到 SSH agent 中

$ ssh-agent bash
$ ssh-add ~/.ssh/github_id_rsa
$ ssh-add ~/.ssh/gitlab_id_rsa复制代码

4 多账号必须配置 config 文件

若无 config 文件,则需创建 config 文件

$ touch ~/.ssh/config        # 创建config文件复制代码
config 里需要填的内容

#Default gitHub user Self
Host github.com
HostName github.com
User git
IdentityFile ~/.ssh/github_rsa

#Add gitLab user
Host git@git.startdt.net
HostName http://git.startdt.net
User git
IdentityFile ~/.ssh/lab_rsa复制代码

5 在github和gitlab网站添加ssh

6 测试是否连接成功

# 测试github
$ ssh -T git@github.com
 
# 测试gitlab
$ ssh -T git@gitlab.com复制代码



转载于:https://juejin.im/post/5aeff650f265da0b8070a7e0

<think>我们正在回答用户关于在Git中生成配置SSH-Key的问题。根据引用内容,我们可以总结出以下步骤:1.生成SSHKey使用命令`ssh-keygen-trsa-C"your_email@example.com"-f~/.ssh/filename`其中:-`-trsa`表示使用RSA算法-`-C`后面是注释,一般用邮箱-`-f`指定生成的文件名路径(可选,如果不指定,则使用默认文件名id_rsa)2.如果配置多个SSHKey(例如同时使用GitHubGitee),则需要为每个平台生成不同的文件,避免覆盖。例如:-GitHub:`ssh-keygen-trsa-C"your_email@example.com"-f~/.ssh/github_id_rsa`-Gitee:`ssh-keygen-trsa-C"your_email@example.com"-f~/.ssh/gitee_id_rsa`3.将生成的公钥(.pub文件内容)添加到对应的Git托管平台(如GitHubGitee等)的SSHKey设置中。4.配置~/.ssh/config文件(可选,当有多个SSHKey时需要配置):如果没有config文件,可以创建一个。然后为每个平台配置一个Host,指定对应的私钥文件。示例:```#GitHubHostgithub.comHostNamegithub.comUsergitIdentityFile~/.ssh/github_id_rsa#GiteeHostgitee.comHostNamegitee.comUsergitIdentityFile~/.ssh/gitee_id_rsa```5.测试连接:使用命令`ssh-Tgit@github.com`测试GitHub,`ssh-Tgit@gitee.com`测试Gitee。6.设置全局的用户名邮箱(如果还没有设置):```gitconfig--globaluser.name"YourName"gitconfig--globaluser.email"your_email@example.com"```注意:在生成SSHKey的过程中,会提示输入密码(passphrase),可以设置也可以直接回车跳过(但为了安全建议设置)。下面我们将详细解释每个步骤。</think>### 如何在 Git 中生成配置 SSH-Key #### 步骤 1:生成 SSH-Key 1. **打开终端** Windows 用户使用 Git Bash,Linux/macOS 使用系统终端。 2. **执行生成命令** ```bash ssh-keygen -t rsa -C "your_email@example.com" ``` - `-t rsa`:指定 RSA 加密算法 - `-C`:添加注释(建议用邮箱标识) - 按提示操作: - 输入保存路径(默认 `~/.ssh/id_rsa`) - 设置密码(可选,直接回车跳过) 3. **生成结果** - 私钥:`~/.ssh/id_rsa`(需保密) - 公钥:`~/.ssh/id_rsa.pub`(需上传到 Git 平台)[^1][^3] --- #### 步骤 2:配置多个 SSH-Key(可选) 当同时使用 GitHubGitee 等平台时: 1. **为每个平台生成独立 Key** ```bash # GitHub 专用 ssh-keygen -t rsa -C "github@mail.com" -f ~/.ssh/github_id_rsa # Gitee 专用 ssh-keygen -t rsa -C "gitee@mail.com" -f ~/.ssh/gitee_id_rsa ``` 2. **创建配置文件 `~/.ssh/config`** ```bash touch ~/.ssh/config # 新建文件 ``` 添加内容: ```conf # GitHub Host github.com HostName github.com User git IdentityFile ~/.ssh/github_id_rsa # Gitee Host gitee.com HostName gitee.com User git IdentityFile ~/.ssh/gitee_id_rsa ``` 3. **修改文件权限** ```bash chmod 600 ~/.ssh/* # 确保密钥安全 ``` [^1][^2] --- #### 步骤 3:添加公钥到 Git 平台 1. **复制公钥内容** ```bash cat ~/.ssh/id_rsa.pub # 显示并复制全部内容 ``` 2. **粘贴到平台设置** - **GitHub**:`Settings > SSH and GPG keys` - **Gitee**:`设置 > SSH 公钥` - 其他平台类似位置 --- #### 步骤 4:测试连接 ```bash ssh -T git@github.com # 测试 GitHub ssh -T git@gitee.com # 测试 Gitee ``` 成功提示示例: `Hi username! You've successfully authenticated.` --- #### 补充:全局用户配置 ```bash git config --global user.name "Your Name" git config --global user.email "your_email@example.com" ``` 确保提交信息与 SSH-Key 邮箱一致[^4]。 --- ### 常见问题解决 - **权限错误**:运行 `chmod 700 ~/.ssh && chmod 600 ~/.ssh/*` - **连接超时**:检查防火墙或代理设置 - **多个 Key 冲突**:确认 `~/.ssh/config` 配置正确
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值