SSH 连接 Github
SSH服务详解(一)–Linux SSH 服务器与客户端的安装与启动
SSH服务详解(二)–使用私钥登录 SSH 服务器(免密登录)
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
testssh
和 testssh.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.