GitLab配置ssh key

2020-06-11  本文已影响0人  coming_168

一、Git的user name和email设置

git config --global user.name "xxx"
git config --global user.email "xxx@qq.com"

二、生成密钥

ls ~/.ssh/id_rsa.pub

输出:No such file or directory。代表不存在

1、使用你注册github的邮箱生成秘钥
ssh-keygen -t rsa -C "xxx@qq.com" 然后一路回车(-C 参数是你的邮箱地址)

.ssh目录会生成id_rsa和id_rsa.pub两个文件:id_rsa是私钥,不能泄露出去,id_rsa.pub是公钥

2.然后会出现:Enter file in which to save the key (/Users/mm_lj/.ssh/id_rsa):
回车键
3.如果你的.ssh/id_rsa已经存在,则会出现:/Users/mm_lj/.ssh/id_rsa already exists.

Overwrite (y/n)? y

输入:y  (重新覆盖)
输入:n  (不覆盖)
4.设置密码(位数不要太短,尽量设置6位)

三、添加SSH key到github账户

两种方式:

cd ~/.ssh
ls(查看目录是否有id_rsa.pub文件)
查看公钥:cat id_rsa.pub 
详细如下图: image.png

获取到的那一大段,就是我们需要的ssh key,复制下来,包括前面和后面的,给到下图配置,即可完成GitLab配置ssh key


image.png

四、把ssh 添加到keychain中

如果去git clone代码,会让你输入密码,如果一个还好说,如果关联了很多的话,那就比较麻烦了,这个时候的解决方法就是添加到keychain中:

//文件路径:/Users/mm_lj/.ssh/id_rsa
ssh-add -K /Users/mm_lj/.ssh/id_rsa

注:.ssl 文件是隐藏的
可以用如下的方式显示:
开启显示隐藏文件:
defaults write com.apple.finder AppleShowAllFiles -bool TRUE
关闭显示隐藏文件:
defaults write com.apple.finder AppleShowAllFiles -bool FALSE

五、测试SSH key是否设置成功

ssh -T git@github.com

输出:
The authenticity of host 'github.com (172.16.0.11)' can't be established.
RSA key fingerprint is SHA256:nThbg6kXUpJWGl9E1IGOCspRomTxdCARLviKw6E6SY6.
Are you sure you want to continue connecting (yes/no)? yes

是否继续连接?输入 yes
输出如下,则表示通过
Hi xxxx! You've successfully authenticated, but GitHub does not provide shell access.

六、设置项目连接方式

git remote set-url git@github.com:Demo/Demo-Base
或者:
// 用的http协议,换成ssh协议
git remote set-url origin git@172.10.1.25:Eshop/CCEshop.git

修改前:

[remote "origin"]
    url = https://github.com/Demo/Demo-Base
    fetch = +refs/heads/*:refs/remotes/origin/*

修改后:

[remote "origin"]
    url = git@github.com:Demo/Demo-Base
    fetch = +refs/heads/*:refs/remotes/origin/*
上一篇下一篇

猜你喜欢

热点阅读