windows生成私钥并配置到github

一、 在.ssh目录下生成私钥

  1. 在C:\Users\{UserName}\.ssh目录下打开终端或git bash
  2. 输入命令:
1
ssh-keygen -t rsa -C "xxx@xxx.com" -f "key_name"
  • -C后面跟的参数是邮箱,可省略
  • -f后面跟的参数是私钥的名字,可省略
    • 若要生成多个私钥,为了防止私钥被覆盖,需要使用不同的私钥名字
  1. 回车确定,会提示你设置密码和再次输入密码。为了简化操作,这里不建议设置密码,直接回车确定。
image-20240303200705859

二、 将生成的 key 添加到 github

  1. 打开生成的 github_id_rsa.pub 文件,全选并复制内容
  2. 登录你的 github ,点击 右上角头像 —> Setttings –> SSH and GPG keys –>New SSH keys
  3. Titile 随意起名, Key 中粘贴上刚刚拷贝的公钥
  4. 点击 Add SSH key ,就成功啦!
image-20240303201134834

三、配置 config 文件

在.ssh文件夹中手动创建config文件或者输入命令touch config生成,并按下面的模板填写,该文件用于配置私钥对应的服务器。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
# gitlab
Host gitlab.ylwnl.com  
HostName gitlab.xxx.com   
PreferredAuthentications publickey
IdentityFile ~/.ssh/gitlab_id_rsa

# github
Host github.com
HostName github.com
PreferredAuthentications publickey
IdentityFile ~/.ssh/github_id_rsa

# gitee
Host gitee.com
HostName gitee.com
PreferredAuthentications publickey
IdentityFile ~/.ssh/gitee_id_rsa

配置说明:

  • Host:自定义别名,会影响git相关命令
  • HostName:真实的服务器地址(域名)
  • User:之前配置的用户名可以省略(xxx@xxx.com
  • PreferredAuthentications:权限认证(publickey,password publickey,keyboard-interactive)一般直接设为publickey
  • IdentityFile:rsa文件地址

GitHub下载速度太慢的解决方式

一、修改host文件

1.访问https://www.ipaddress.com/。

2.查询github.comgithub.global.ssl.fastly.net的对应ip地址。

3.进入目录C:\Windows\System32\drivers\etc,以管理员权限打开hosts文件。

4.将查询到的ip地址加入hosts文件。
例如:

1
2
140.82.112.4        github.com
199.232.69.194 github.global.ssl.fastly.net
阅读更多