iOS 工具集

MAC电脑-git添加多个SSH公钥

2022-03-22  本文已影响0人  kangomake
普通权限与root权限的区别就是路径地址不同
普通权限:~/.ssh/
root权限:/var/root/.ssh/

SSH之于Git的原理
Git提交时有Https和SSH两种验证方式,Https的方式需要帐号和密码比较好理解,不过它需要在每次提交时输入帐号和密码,有点麻烦;而SSH的功能可以粗暴的理解为记住帐号密码,不过对这个过程有人会有点疑惑。首先,我们用SSH命令生成一个公钥-私钥对,我们会把公钥添加到Git的服务器,把私钥放在本地。提交文件的时候Git服务器会用公钥和客户端提交私钥做验证(具体细节不究),如果验证通过则提交成功,那么我们在把公钥添加到服务器的时候肯定是需要登录Git服务器的,这个过程其实可以理解为帐号和密码托管给SSH了,所以也是相当于输入了帐号密码,但是由SSH帮你记住了。这么理解是可以,但是SSH的意义不仅仅是这样,关于SSH的更详细内容看客可以自行再了解。

一、创建 github SSH-KEY

1.生成 按照提示完成三次回车

ssh-keygen -t rsa -C 'xxx@xxx.xxx' -f ~/.ssh/github_id_rsa
# sudo ssh-keygen -t rsa -C 'xxx@xxx.xxx' -f /var/root/.ssh/github_id_rsa

2.查看 ssh key

cat ~/.ssh/github_id_rsa.pub
# sudo cat /var/root/.ssh/github_id_rsa.pub
# ssh-rsa AAAAB3NzaC1yc2EAAAADAQABA....

3.复制内容到 github 添加 SSH key

二、创建 gitee(码云) SSH-KEY

1.生成
按照提示完成三次回车

ssh-keygen -t rsa -C 'xxx@xxx.xxx' -f ~/.ssh/gitee_id_rsa
# sudo ssh-keygen -t rsa -C 'xxx@xxx.xxx' -f /var/root/.ssh/gitee_id_rsa

2.查看 ssh key

cat ~/.ssh/gitee_id_rsa.pub
# sudo cat /var/root/.ssh/gitee_id_rsa.pub
# ssh-rsa AAAAB3NzaC1yc2EAAAADAQAB...

3:复制内容到 gitee 添加 SSH key

三、创建 SSH 用户配置文件 config

touch ~/.ssh/config
# sudo touch /var/root/.ssh/config

添加以下内容

# github
Host github.com
HostName github.com
PreferredAuthentications publickey
IdentityFile ~/.ssh/github_id_rsa
# IdentityFile /var/root/.ssh/github_id_rsa
# gitee
Host gitee.com
HostName gitee.com
PreferredAuthentications publickey
IdentityFile ~/.ssh/gitee_id_rsa
# IdentityFile /var/root/.ssh/gitee_id_rsa

Host 和 HostName 填写 git 服务器的域名
IdentityFile 指定私钥的路径

测试

ssh -T git@github.com
# sudo ssh -T git@github.com
# Hi xxx! You've successfully authenticated, but GitHub does not provide shell access.

ssh -T git@gitee.com
# Hi xxx! You've successfully authenticated, but GITEE.COM does not provide shell access.

遇到了一个问题:使用ssh推送代码到远程git服务器报错,ssh 配置正常没有做任何的修改

ssh: Could not resolve hostname [git.xxx.com](http://git.xxx.com): 
nodename nor servname provided, or not known
fatal: 无法读取远程仓库。

解决办法:

ssh-keyscan -H git.xxx.com >> ~/.ssh/known_hosts

参考文章:https://www.jianshu.com/p/c2e815937746

上一篇 下一篇

猜你喜欢

热点阅读