1. 运行 git-bash.exe 进入命令行

2. 判断是否已存在本地公钥:

cat ~/.ssh/id_rsa.pub

  如果看到一长串以 ssh-rsa 或 ssh-dsa 开头的字符串,可以跳过 ssh-keygen 步骤

3. 生成 ssh key

ssh-keygen -t rsa -C "自定义标识符"

  生成代码会有两个步骤,提示设置密码(默认没有密码)、pub文件名称及保持路径,按Enter直接跳过步骤使用默认值。需要注意的是,如果自定义了文件名/路径,需要在 SSH 客户端做配置(步骤5开始介绍)。

  用以下的命令或你生成的公钥:

cat ~/.ssh/id_rsa.pub(或者你自定义的目录/文件名)

4. 复制公钥到阿里云个人设置中的 SSH/My SSH Keys下,不同系统的复制命令如下:

  Windows:

clip < ~/.ssh/id_rsa.pub(或者你自定义的目录/文件名)

  Mac:

pbcopy < ~/.ssh/id_rsa.pub

  GUN/Linux(requires xclip):

xclip -sel clip < ~/.ssh/id_rsa.pub

5. 如果使用非默认位置/文件名存放 ssh key,必须配置好 SSH 客户端以找到你的私钥取链接 code 服务器,通常实在 ~/.ssh/config 配置

1 # git bash下执行,创建并修改config文件
2 touch
~/.ssh/config

  在 config 添加如下配置:

1 # Our company's internal GitLab server
2 Host my-git.company.com3 RSAAuthentication yes4 IdentityFile ~/.ssh/id_rsa(或者是你自定义的目录&文件名)

6. 验证是否成功

ssh -T git@github.com

 

7. Git 下的一些配置

  a. 生成多个 ssh key

    重复执行步骤3,生成多个 ssh key,并自定义名称/保存路径即可

  b. 管理多个 ssh key

    运行 ssh-agent 命令添加私钥:

1 ssh-add ~/.ssh/id_rsa_gitlab
2 ssh-add ~/.ssh/id_rsa_github

    如果执行上述命令出现 Could not open a connection to your authentication agent,解决方法如下:

 1 # 杀死 ssh-agent 线程
 2 ps aux|grep ssh
 3 kill -9 线程号
 4 
 5 # 进入用户名目录下的 .ssh 目录,打开 git bash 执行如下命令:
 6 exec ssh-agent bash
 7 eval ssh-agent -s
 8 
 9 # 再执行 ssh-add 命令
10 ssh-add ./id_rsa_gitlab
11 ssh-add ./id_rsa_github

  c. 创建并修改 config 配置文件

 1 # gitlab
 2 Host gitool.glanway.com
 3 HostName gitool.glanway.com
 4 PreferredAuthentications publickey
 5 IdentityFile ~/.ssh/id_rsa_gitlab
 6 User your-username
 7 
 8 # github
 9 Host github.com
10 HostName github.com
11 PreferredAuthentications publickey
12 IdentityFile ~/.ssh/id_rsa_github
13 User your-username

 

 

参考文章:

  https://code.aliyun.com/help/ssh/README 阿里云帮助文档

  https://www.cnblogs.com/mingyue5826/p/11141324.html Git安装及配置SSH Key