Git ssh添加多个账号

生成并添加第一个ssh key

第一次使用ssh生成key,默认会在用户~(根目录)下生成 id_rsa, id_rsa.pub 2个文件;所以需要添加多个ssh key时也会生成对应的私钥和公钥。

$ ssh-keygen -t rsa -C "youremail@yourcompany.com"

在Git Bash中执行这条命令一路回车,会在 ~/.ssh/ 目录下生成 id_rsa 和 id_rsa.pub 两个文件,用文本编辑器将 id_rsa_pub 中的内容复制一下粘贴到github(gitlab)上。

添加私钥

$ ssh-add ~/.ssh/id_rsa
$ ssh-add ~/.ssh/id_rsa_github

如果执行ssh-add时提示"Could not open a connection to your authentication agent",可以现执行命令:

$ ssh-agent bash

然后再运行ssh-add命令。

可以通过 ssh-add -l 来确私钥列表

$ ssh-add -l

通过 ssh-add -D 来清空私钥列表

$ ssh-add -D
修改配置文件
在 ~/.ssh 目录下新建一个config文件

touch config
添加内容:

# gitlab
Host gitlab.com
    HostName gitlab.com
    PreferredAuthentications publickey
    IdentityFile ~/.ssh/id_rsa

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

测试

$ ssh -T git@github.com

输出
Hi user! You've successfully authenticated, but GitHub does not provide shell access. 就表示成功的连上github了

©著作权归作者所有,转载或内容合作请联系作者
【社区内容提示】社区部分内容疑似由AI辅助生成,浏览时请结合常识与多方信息审慎甄别。
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

相关阅读更多精彩内容

友情链接更多精彩内容