0


【Git】SSH密钥配置通关

前言

想起以前刚入行时,各种git、maven等工具的问题……每次都在网上找各种博客看,虽然能解决但是耗时费力……现在对我来说是问题但又不是问题了,所以记录下解决过程,为自己、为他人

能解决的问题都不是问题,问题是过程要快速高效!

错误提示信息&场景

连接关闭

本地连接远程仓库操作时报错,我这里远程仓库是github,报错信息如下。内网地址?连接被关闭?有点懵~

Connection closed by 198.18.0.72 port 22 Could not read from remote repository. Please make sure you have the correct access rights and the repository exists.

在这里插入图片描述

结论:Connection closed连接关闭的是网络问题,比如挂了梯子(我是这种),关了梯子又好了,不需要更改任何配置。

连接拒绝

想起来还有类似问题,连接被拒绝connection refused……

ssh: connect to host github.com port 22: Connection refused
fatal: Could not read from remote repository.
Please make sure you have the correct access rights
and the repository exists.

结论:Connection refused连接拒绝的极大可能是密钥配置不对,看下面的丝滑连招~

SSH配置一条龙

Win系统使用Git Bash工具窗口来操作,参考文尾连接
下面是Mac系统的演示过程。
不同系统仅仅是命令不同

检查已有SSH密钥配置

检查目录列表是否已拥有公共 SSH 密钥。

$ ls-al ~/.ssh
# Lists the files in your .ssh directory, if they exist

在这里插入图片描述
我这都是自定义名称的,但是文件名称格式类似
你这里如果有,那就检查“添加新的SSH密钥”小节
没有那就继续往下看

生成新的SSH密钥

生成SSH密钥

执行命令,注意替换邮箱。

ssh-keygen -t ed25519 -C "[email protected]"# 可以带上-f file_name来自定义文件名称,如
ssh-keygen -t ed25519 -C "[email protected]"-f github_rsa

然后会提示输入密码配置,没要求就使用默认即可,直接连续按回车跳过

可以用上面的命令去检查一下有没有生成

将 SSH 密钥添加到 ssh-agent

在后台启动 ssh-agent。

$ eval "$(ssh-agent -s)"
> Agent pid 59566

配置config文件

$ open ~/.ssh/config

文件内容格式说明

Host github.com                     # 连接IP或者域名
  AddKeysToAgent yes
  UseKeychain yes
  IdentityFile ~/.ssh/id_ed25519    # 路径文件名称
Host github.com                     # 可以根据需求配置多组
  AddKeysToAgent yes
  UseKeychain yes
  IdentityFile ~/.ssh/github_rsa

将 SSH 私钥添加到 ssh-agent 并将密码存储在钥匙串中

注意你的替换文件名

ssh-add --apple-use-keychain ~/.ssh/id_ed25519

添加新的SSH密钥

打印一下密钥,可以像这样找到你的密钥文件,然后打印输出,注意文件名

 ~ cd ~/.ssh
 ~/.ssh  ls-al
total 64
drwx------@ 10 will  staff   320  6 28 17:17 .
drwxr-x---+ 44 will  staff  1408  6 28 20:31 ..-rw-r--r--@  1 will  staff   173  6 28 17:13 config
-rw-------@  1 will  staff   411  8 12  2023 gitee_rsa
-rw-r--r--@  1 will  staff    98  8 12  2023 gitee_rsa.pub
-rw-------@  1 will  staff   411  6 28 17:06 github_rsa
-rw-r--r--@  1 will  staff    98  6 28 17:06 github_rsa.pub
-rw-r--r--@  1 will  staff   211  8 19  2023 iterm2login.sh
-rw-------   1 will  staff   919  6 28 17:17 known_hosts
-rw-r--r--   1 will  staff   183  6 28 17:17 known_hosts.old
 ~/.ssh  cat github_rsa.pub
## 会打印出你的公钥内容,复制出来

把复制的公钥内容配置到平台上即可

在这里插入图片描述

SSH连接测试

最后测试一下

ssh -T [email protected]

类似这种就成功了!如果有问题,就检查一下每个步骤吧,仔细、认真、有耐心!
在这里插入图片描述

最后

建议直接看这个文档!!!

https://docs.github.com/en/authentication/connecting-to-github-with-ssh/checking-for-existing-ssh-keys

另外现在各种AI工具一用俱全,多用用看吧,也能解决大部分问题

标签: git ssh 运维

本文转载自: https://blog.csdn.net/qq_44373969/article/details/140049321
版权归原作者 Will~ 所有, 如有侵权,请联系我们删除。

“【Git】SSH密钥配置通关”的评论:

还没有评论