manage multi ssh keys

管理多个SSH的密钥

大家访问github或者gitlab的时候,还是使用ssh比较方便,使用https方式需要输入用户名密码,但是同时有github和gitlab的时候就比较麻烦了

下面分享一下同时管理多个ssh密钥的方法

1.大家生成ssh key的时候一般用以下命令

~  ssh-keygen -t rsa -C "mail@mail.com"
Generating public/private rsa key pair.
Enter file in which to save the key (/Users/username/.ssh/id_rsa): /Users/username/.ssh/id_rsa_github
Enter passphrase (empty for no passphrase):
Enter same passphrase again:
Your identification has been saved in /Users/username/.ssh/id_rsa_github.
Your public key has been saved in /Users/username/.ssh/id_rsa_github.pub.
The key fingerprint is:
SHA256:D3ew605ht8B9NMCvadetvF6wcG//JwUrNbdegueAXQY/3n8 username@MBP
The key's randomart image is:
+---[RSA 2048]----+
|                 |
|                 |
|          . .    |
|        .  o o   |
|        So* = =  |
|         B+% x o |
|         .@o& = .|
|         *.*+B.oE|
|        .oB*ooo +|
+----[SHA256]-----+

可以看到,在Enter file in which to save the key (/Users/username/.ssh/id_rsa):这句提示,如果不输入的话,默认生成key就放在了/Users/username/.ssh/id_rsa这个路径下,我们可以自己输入一个路径,比如:/Users/username/.ssh/id_rsa_github

同样生成一个id_rsa_gitlab密钥

2.我们把生成好的私钥添加到ssh-agent中

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

3.将对应的公钥文件id_rsa_github.pub添加到github,将id_rsa_gitlab.pub添加到gitlab上。

4.在~/.ssh/目录下添加config文件

touch ~/.ssh/config

5.修改config文件

#aaa  (github 配置)
Host github
HostName github.com
User git
IdentityFile ~/.ssh/id_rsa_github

#aaa  (gitlab 配置)
Host gitlab
HostName gitlab.com
User git
IdentityFile ~/.ssh/id_rsa_gitlab

6.测试一下

ssh -T git@github.com

以后操作就可以使用ssh方式来访问仓库了。

Comments !

blogroll

social