本地创建多个ssh-key

前言

有时我们在开发的过程中,需要把代码提交到不同的gitlab仓库里面,比如:公司的gitlab、GitHub等其他git仓库;在代码提交的时候都需要验证本机的SSH Keys;

在使用 ssh-keygen -t rsa -C "[email protected]"这个命令是默认都是生成到/.ssh/id_rsa和/.ssh/id_rsa.pub里面,如果已存在会覆盖的;

那么如果是本地有多个SSH Keys呢?

添加ssh key

如果你有多个gitlab账号

执行命令

 ssh-keygen -t rsa -C "[email protected]" //这个邮箱一定要是你对应的gitlab绑定的邮箱

执行是会出现以下询问:

Generating public/private rsa key pair.
//默认的输出文件就是根目录下的.ssh/id_rsa 你需要重新指定一个文件名 
//如:.ssh/id_rsa_emal1 文件名如何定义自行决定
Enter file in which to save the key (/Users/a58/.ssh/id_rsa): /Users/a58/.ssh/id_rsa_emal1
/Users/a58/.ssh/id_rsa_58 already exists.
//已存在是否覆盖
Overwrite (y/n)? y
//设置密码 可直接跳过
Enter passphrase (empty for no passphrase): 
Enter same passphrase again: 
Your identification has been saved in /Users/a58/.ssh/id_rsa_emal1.
Your public key has been saved in /Users/a58/.ssh/id_rsa_emal1.pub.
The key fingerprint is:
SHA256:aNZ0rCQP3xI1jmcvv9H9GKDjuRhE6VMVcLDeLKi53dE emal1@163.com
The key's randomart image is:
+---[RSA 2048]----+
|          =o+.   |
|         * =     |
|      o B O      |
|       % @ +     |
|      + S = =    |
|     o + o * o . |
|      o . + E o .|
|       o = + o o.|
|      . o =.. . .|
+----[SHA256]-----+

直接vim或者cat输出pub公钥文件:

cat /Users/a58/.ssh/id_rsa_emal1.pub         
ssh-rsa AAAAB3NzaC1yc2EAAAADAQABAAABAQC12HyuAvMnRzQK24SXtJF7v5BL3lsH6ab1/NzYO/Q7whOTndqsS59/ZCqK/HK5TiwJkoEH+l2tB9o8a+1De5ri/9a7zLVIEST/ig79LiCcoIengSLX8jX37dq6pchBfKSdukXBsGJnrxKVsnVZScY5Bozx8LlhPYOuFuxZ8yW/2nxVAVaa1aIa8W4ZDA3rxBsdClh5GjT8dWHvfZtb8w8bFdjKSywvf4+GeIW0gjrRgmpkRmwaiCRam2dmnOS0vWNy+ZhkIeiMR0DqSpbT5Zeb81NVuvN3oXAornUR5GjN5wyKR2UsXLIJ4TvccYS+8EOKItM1FBX3oyGeQHO8qeWL emal1@163.com

拷贝文件里面的内容添加到你对应的gitlab的ssh keys里面

配置config

在你本地的~/.ssh/ 下面新建config文件 文件里面添加如下内容

# emal1 gitlab
# 添加你Git仓库地址的域名 如:github
Host emal1.gitlab.com
#填写你gitlab绑定的邮箱
User emal1@163.com
PreferredAuthentications publickey
#对应你刚刚生成的公钥文件
IdentityFile ~/.ssh/id_rsa_emal1

OK以上你都完成了之后那边也就完成了git的提交的配置了。

你可能感兴趣的:(【ssh】)