Git 创建使用 SSH Key

####1、GitHub中找到 setting-> SSH and GPG keys,添加一个SSH key
图片.png
图片.png

####2、生成ssh-keygen
先跳转到.ssh目录

cd  ~/.ssh/

再通过命令 ssh-keygen 生成 SSH Key:

ssh-keygen -t ed25519 -C "这里最好填写账号一目了然"

    //-t key 类型
   // -C 注释

######2.1、中间通过三次回车键确定,得到下面就成功了

Generating public/private ed25519 key pair.
Enter file in which to save the key (/home/git/.ssh/id_ed25519):
Enter passphrase (empty for no passphrase):
Enter same passphrase again:
Your identification has been saved in /home/git/.ssh/id_ed25519
Your public key has been saved in /home/git/.ssh/id_ed25519.pub
The key fingerprint is:
SHA256:ohDd0OK5WG2dx4gST/j35HjvlJlGHvihyY+Msl6IC8I Gitee SSH Key
The key’s randomart image is:
±-[ED25519 256]–+
| .o |
| .+oo |
| …O.o + |
| .= * = +. |
| .o +…S*. + |
|. …o o…+* * |
|.E. o . …+.O |
| . . … o =. |
| …oo. o.o |
±—[SHA256]-----+
######2.3、查看秘钥

ls ~/.ssh/

这是两个密钥文件

id_ed25519 id_ed25519.pub
//私钥文件 id_ed25519
//公钥文件 id_ed25519.pub

######2.4、读取公钥文件

 cat ~/.ssh/id_ed25519.pub

出现下面局势成功了

ssh-ed25519 AAAA***5B Gitee SSH Key

####3、再把这个秘钥复制到第一步的key中。保存
####4、我们还需要将私钥添加到本地中,不然无法使用。执行下面命令行

ssh-add ~/.ssh/id_ed25519//将私钥添加到本地

如果出现

Could not open a connection to your authentication agent.

则先执行下面的命令行

ssh-agent bash

添加完成之后,查看添加的秘钥

ssh-add -l   //查看已经添加的秘钥

出现下面结果就是添加好了

3072 SHA256:************ “邮箱”

####5、管理密钥,下面我们需要在本地创建一个密钥配置文件,通过该文件,实现根据仓库的 remote 链接地址自动选择合适的私钥。
编辑 ~/.ssh 目录下的 config 文件,如果没有,就需要创建

Host github       
HostName ssh.github.com
User usergitlab
IdentityFile ~/.ssh/id_ed25519

Host gitee
HostName gitee.com
User user
IdentityFile ~/.ssh/id_ed25519

Host:仓库网站的别名,随意取(个人建议可以和HostName一样也可以)
HostName:仓库网站的域名(PS:IP 地址应该也可以)
User:仓库网站上的用户名
IdentityFile:私钥的绝对路径
上面的两个网站可以使用两套SSH Key的,但是我用同一套也行,偷懒

####7、验证能不能链接服务

ssh -T git@github.com 

如果下面的提示,是缺少known_hosts文件,或者known_hosts不对,直接yes 就对了,然后重新验证

The authenticity of host ‘github.com (20.205.243.166)’ can’t be established.
ECDSA key fingerprint is SHA256:p2QAMXNIC1TJYWeIOttrVc98/R1BUFWu3/LiyKgUfQM.
Are you sure you want to continue connecting (yes/no/[fingerprint])?

出现下面的就是成功了

Hi Anonymous! You’ve successfully authenticated, but GITEE.COM does not provide shell access.

tip:如果是老的项目,项目中的config文件中的 url ,https修改成git

  • 22
    点赞
  • 17
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值