git对于多个ssh key的配置
在项目开发的过程中Git是项目管理常用的工具,但是时间长了就会出现很多不同网站的项目都需要用ssh的情况,这个时候怎么配置多个ssh key是一个很重要的问题了,首先要先确认你的电脑有没有设置全局账号和邮箱,如果有的话,可以通过以下命令取消
git config --global --unset yourusername
git config --global --unset youremail
生成SSH KEY
首先要先生成账号的公钥,我的是OS系统,所以生成的文件是在~/.ssh路径下
ssh-keygen -t rsa -C "yourusername"
或者也可以用下面的命令命名生成的文件的名字
ssh-keygen -t rsa -C "yourusername” -f ~/.ssh/id-rsa
现在有GitHub和GitLab两个网站需要生成SSH KEY可以用以下命令生成公钥文件
ssh-keygen -t rsa -C "yourGitHubusername” -f ~/.ssh/id-rsa
ssh-keygen -t rsa -C "yourGitLabusername” -f ~/.ssh/github-rsa
添加SSH KEY
用一下命令添加以上生成的GitHub和GitLab的私钥
ssh-add ~/.ssh/id-rsa
ssh-add ~/.ssh/github-rsa
修改配置文件
在~/.ssh文件路径下新建config文件,然后输出一下内容
# 在.ssh 路径下新建config文件
touch config
# 在config文件添加内容
vi config
# config的内容
Host gitlab.com
HostName gitlab.com
PreferredAuthentications publickey
IdentityFile ~/.ssh/id-rsa
# github
Host github.com
HostName github.com
PreferredAuthentications publickey
IdentityFile ~/.ssh/github-rsa
测试
保存config的内容,然后一下命令测试
ssh -T git@github.com
# 输出
Welcome to GitLab, your username!