创建和配置ssh keys for gitlab

一、为gitlab.com建立ssh key

  • 建立本机的ssh publish key

git config --global user.name "you name"

git config --global user.email "aa@bb.com"

  • 生成ssh private key

1.查看是否存在ssh private key: cd ~/.ssh,如果没有密钥则不会有文件夹,有就做好备份删除;

2.生成密钥

ssh-keygen -t rsa -C "aa@bb.com"

 按3个回车,密码为空;

最后得到了两个文件:id_rsa和id_rsa.pub

二、配置ssh客户端

  • 在gitlab.com上添加ssh密钥

这里是添加id_rsa.pud里的公钥,打开gitlab.com,登录自已的账号,然后添加。

  • 配置config

进入ssh目录:cd ~/.ssh/

执行命令:sudo gedit config,打开文本后输入以下内容,注意Hostname的值是输入自已的,

 

User git
Hostname gitlab.yourwebsite.com
PreferredAuthentications publickey
IdentityFile ~/.ssh/id_rsa
TCPKeepAlive yes
IdentitiesOnly yes
//如果你要走本地代理
ProxyCommand nc -x 127.0.0.1:1080 %h %p

 

保存与退出;

 

  • 测试是否成功

ssh git@yourwebsite.com

如果成功会显示:

 

PTY allocation request failed on channel 0
Welcome to GitLab, @youname!
Connection to xx.com  closed.

 

如果出现:the authenticity of host gitlab.com can't be established..

请执行:

eval $(ssh-agent -s)

然后:

ssh-add ~/.ssh/id_rsa

 参考此文章

 

如果要配置多个id_rsa,请参考这里;如添加了bitbucket.org的代码访问后,还要修改~/.ssh下的config文件,添加对应的Hostname和IdentityFile

 

 

posted @ 2019-03-07 11:20  johnny_zhao  阅读(470)  评论(0编辑  收藏  举报