git配置两个SSH-Key
我们在日常工作中会遇到公司有个gitlab,还有些自己的一些项目放在github上。这样就导致我们要配置不同的ssh-key对应不同的环境。下面我们来看看具体的操作:
1,生成一个公司用的SSH-Key
1 | $ ssh-keygen -t rsa -C "youremail@yourcompany.com" -f ~/.ssh/id_rsa |
在~/.ssh/目录会生成id_rsa和id_rsa.pub私钥和公钥。 我们将id_rsa.pub中的内容粘帖到公司gitlab服务器的SSH-key的配置中。
2,生成一个github用的SSH-Key
1 | $ ssh-keygen -t rsa -C "youremail@your.com" -f ~/.ssh/id_rsa_github |
在~/.ssh/目录会生成id_rsa_github和id_rsa_github私钥和公钥。 我们将id_rsa_github中的内容粘帖到github服务器的SSH-key的配置中。
3,添加私钥
1 | $ ssh-add ~/.ssh/id_rsa $ ssh-add ~/.ssh/id_rsa_github |
如果执行ssh-add时提示"Could not open a connection to your authentication agent"
,可以现执行命令:
1 | $ ssh-agent bash |
然后再运行ssh-add
命令。
1 | # 可以通过 ssh-add -l 来确私钥列表 |
4,修改配置文件
在 ~/.ssh
目录下新建一个config
文件
1 | vi config |
添加内容:
1 | # gitlab |
5.测试
1 | $ ssh -T git@github.com |
输出
Hi stefzhlg! You've successfully authenticated, but GitHub does not provide shell access.
就表示成功的连上github了.也可以试试链接公司的gitlab.
6.配置以ip地址的远程仓库
在120.24.63.25上配置了一个代码库
此时需要修改config
文件
vi config文件
1 | Host 120.24.63.25 |
需要指定User为git
本博客所有文章除特别声明外,均采用 CC BY-NC-SA 4.0 许可协议。转载请注明来自 螃蟹壳!