SSH服务详解(七)– SSH 连接 Github

本文详细介绍了在Windows环境下配置SSH服务,包括生成SSH密钥对、添加GitHub公钥、设置SSH代理以及解决常见问题。通过SSH连接GitHub可以实现安全的免密登录,提高开发效率。同时,文章还提供了在Git Bash中永久添加私钥的方法,确保每次打开都能自动加载SSH密钥。
摘要由CSDN通过智能技术生成

SSH 连接 Github

SSH服务详解(一)–Linux SSH 服务器与客户端的安装与启动

SSH服务详解(二)–使用私钥登录 SSH 服务器(免密登录)

SSH 服务详解 (三)-- 使用 SSH 代理

SSH 服务详解 (四)-- 本地调用远程主机的命令

SSH 服务详解 (五)-- 远程文件拷贝

SSH 服务详解 (六)-- Windows SSH 主机

SSH 服务详解 (七)-- SSH 连接 Github

SSH服务详解(八)– vscode 通过 SSH 远程连接 linux 服务器

  • SSH 客户端运行在 windows

本地(Windows)

Linux 网上教程较多,这里以 Windows 为例

配置本地 git

查看配置

git config --global --list

配置用户名

git config --gloabl user.name "tyustli"

配置邮箱

git config --global user.email "your email address"

生成秘钥

最好在 C:\Users\Admin\.ssh 目录下生成秘钥(防止时间太久秘钥找不到)

在上述目录下打开 git bash

ssh-keygen.exe -t rsa -C "for ssh test"
  • -t 表示类型
  • -C 注释

提示输入秘钥名称

Enter file in which to save the key (/c/Users/Admin/.ssh/id_rsa): testssh

秘钥生成结果

Enter passphrase (empty for no passphrase):
Enter same passphrase again:
Your identification has been saved in testssh.
Your public key has been saved in testssh.pub.
The key fingerprint is:
SHA256:eEFl7kK7MYymu2xxAQ for ssh test
The key's randomart image is:
+---[RSA 2048]----+
|          *=o    |
|       ooo*.     |
|E      .+o.      |
| .   o *.++      |
|      B.S+..     |
| o   +.*.o.      |
|  +.   ..        |
| +o+oo           |
|oo               |
+----[SHA256]-----+

当前目录

$ ls
config  known_hosts  testssh  testssh.pub  tyustli  tyustli.pub

testsshtestssh.pub 为生成的秘钥对

Github 公钥添加

Settings->SSH and GPG keys

请添加图片描述
点击 New SSH key
请添加图片描述

Title 输入名称,Key 输入公钥 testssh.pub

将生成的公钥 testssh.pub 内容复制到 Key 中即可

添加私钥

git bash 中添加私钥(git bash 关闭之后添加的就失效了)

bash 中依次输入如下命令
启动代理

ssh-agent

添加私钥

ssh-add ~/.ssh/testssh

上述命令执行下来会报错

Could not open a connection to your authentication agent.

再执行一遍如下命令即可
在 bash 中启动 ssh 代理

ssh-agent bash

添加 ssh 私钥

ssh-add ~/.ssh/testssh

连接测试

ssh -T git@github.com

测试成功

Hi xxx! You've successfully authenticated, but GitHub does not provide shell access.

注意:bash 关闭之后添加的 ssh 秘钥就失效了,下次再打开需要重新执行上述步骤

永久添加私钥

打开 git bash 时,需要将 ssh 密钥自动添加到每个会话中,那么如何一次添加永久使用呢?

打开 git 安装目录(D:\SoftWare_Application\Git\etc\ssh)下的 ssh_config 文件

IdentityFile 注释的地方下一行添加如下内容

IdentityFile path

path 表示 ssh 私钥的路径地址

例如

IdentityFile ~/.ssh/testssh

ssh 测试

ssh -T git@github.com

测试失败时会报如下错误:

git@github.com: Permission denied (publickey).

测试失败,秘钥未添加,参考使用 SSH 代理 的添加 SSH 私钥章节,即修改私钥文件的权限

注意事项:最好不要将新添加的私钥放在 ssh_config 文件的尾部,有些系统不支持,也会报上述错误,在有 IdentityFile 注释的下一行添加自己的私钥即可

测试成功

Hi xxx! You've successfully authenticated, but GitHub does not provide shell access.
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值