今天配置了github的ssh访问密钥,同时也遇到了一些问题,这里记录一下.
- 生成新SSH密钥
$ ssh-keygen -t rsa -b 4096 -C "[email protected]"
这将创建以所提供的电子邮件地址为标签的新 SSH 密钥。
> Generating public/private rsa key pair.
提示您“Enter a file in which to save the key(输入要保存密钥的文件)”时,按 Enter 键。 这将接受默认文件位置。
> Enter a file in which to save the key (/c/Users/you/.ssh/id_rsa):[Press enter]
在提示时输入安全密码。
> Enter passphrase (empty for no passphrase): [Type a passphrase] > Enter same passphrase again: [Type passphrase again]
- 将 SSH 密钥添加到 ssh-agent
ssh-agent用于管理ssh的密钥密码,不用每次访问时都输入密钥密码
# 在后台启动 ssh-agent $ eval $(ssh-agent -s) > Agent pid 59566
将 SSH 私钥添加到 ssh-agent。 如果您创建了不同名称的密钥,或者您要添加不同名称的现有密钥,请将命令中的 id_rsa 替换为您的私钥文件的名称。
$ ssh-add ~/.ssh/id_rsa
将 SSH 密钥添加到 GitHub 帐户
测试账户是否配置成功
$ ssh -T [email protected]
如果连接成功控制台会返回你的用户名信息.
> Hi username! You've successfully authenticated
- ssh配置多账户
因为实际使用中你可能会同时使用github,gitlab,gitee等,不同平台需要配置不同的访问域名或密钥,这是可以通过git的config配置来实现
在~/.ssh/目录下新建config文件,并将你需要支持的git平台配置填写进去.示例如下:
Host github.com
User git
Hostname github.com
IdentityFile ~/.ssh/id_rsa
Host gitlab.com
User git
Hostname gitlab.com
IdentityFile ~/.ssh/id_rsa
- 问题
我这边配置完成后,不知道什么原因,github测试总是提示连接超时,但是gitlab可以正常访问.可能是防火墙问题(实际关闭防火墙后还是无法访问)异常信息:
$ ssh -T [email protected] ssh: connect to host github.com port 22: Connection timed out
解决方式:
官方解释说可能由于防火墙等原因造成ssh连接异常,这时通过https连接可以正常的访问到github.要测试通过 HTTPS 端口的 SSH 是否可行,请运行以下 SSH 命令:
$ ssh -T -p 443 [email protected] > Hi username! You've successfully authenticated, but GitHub does not > provide shell access.
config配置方式如下:
Host github.com Hostname ssh.github.com Port 443
然后直接测试github连接:
$ ssh -T [email protected] Hi liulei3! You've successfully authenticated, but GitHub does not provide shell access.
原文地址:https://www.cnblogs.com/chengmuyu/p/12244337.html
时间: 2024-12-16 13:26:58