manage multiple ssh-keys

Posted by deng liuyan on May 17, 2018

Environment config

when you start your work environment,manage mutilple ssh keys is important just like keep your home key safe,using mutilple ssh keys to match different systems,such as gitlab,github,personal keys,work keys.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
[... .ssh]$ ssh-keygen -t rsa -C "****"
Generating public/private rsa key pair.
Enter file in which to save the key (/home/usbmount/.ssh/id_rsa): id_rsa_gitlab
Enter passphrase (empty for no passphrase):
Enter same passphrase again:
Your identification has been saved in id_rsa_gitlab.
Your public key has been saved in id_rsa_gitlab.pub.
The key fingerprint is:
3a:fe:c4:1c:48:af:2a:f1:a1:fb:94:21:b6:c1:23:54 ***
The key''s randomart image is:
+--[ RSA 2048]----+
|   E             |

▽
# gitlab
|  .              |
| .    .          |
|..   . o         |
|. * . . S        |
| o.=.o = .       |
|  .+o.+ +        |
|  o..o o         |
|  .+o ...        |
+-----------------+


[... .ssh]$ ssh-add id_rsa_gitlab
Identity added: id_rsa_gitlab (id_rsa_gitlab)


[... .ssh]$ vim config
# gitlab
  #HOST can use regular expression
  HOST gitlab.*
  #HostName must match the Host regular expression,and without port
  #if the host is ip address,you need add a <Port> param
  HostName ****
  #the prefer ssh auth mode
  PreferredAuthentications publickey
  IdentityFile ~/.ssh/id_rsa_gitlab
  User genetfp2

#github
  Host github
  HostName github.com
  IdentityFile ~/.ssh/id_rsa_github

#if there are many ssh keys,you are suggested to add the follow config to let password first,
#because if ssh keys first,and match the all above keys failed,the auth times may be
#greater than auth max-time,and then you cannot use password auth any more.
   Host *
   PubkeyAuthentication no

if you want add another,just do it again.