一台终端-多个ssh-key配置

2020-03-04  本文已影响0人  ricefun

1、生成SSH-Key

$ ssh-keygen -t rsa -C "youremail@xxx.com" -f ~/.ssh/id_rsa_github
$ ssh-keygen -t rsa -C "youremail@xxx.com" -f ~/.ssh/id_rsa_gitlab

2、查看公钥

$ cat ~/.ssh/id_rsa_github.pub
$ cat ~/.ssh/id_rsa_gitlab.pub

3、将公钥内容复制粘贴至gitlab/github SSH公钥

4、添加私钥

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

5、添加配置文件

//进入ssh目录
$  cd ~/.ssh
//新建config文件
$ touch config

6、编辑config文件

//打开ssh文件
$  open ~/.ssh

打开config文件进行如下编辑

# gitlab
Host gitlab.com
    HostName gitlab.com
    PreferredAuthentications publickey
    IdentityFile ~/.ssh/id_rsa_gitlab
# github
Host github.com
    HostName github.com
    PreferredAuthentications publickey
    IdentityFile ~/.ssh/id_rsa_github
#用于自动添加ssh
Host *
   UseKeychain yes
   AddKeysToAgent yes
   IdentityFile ~/.ssh/id_rsa_gitlab
   IdentityFile ~/.ssh/id_rsa_github

# 配置文件参数
# Host : Host可以看作是一个你要识别的模式,对识别的模式,进行配置对应的的主机名和ssh文件
# HostName : 要登录主机的主机名
# IdentityFile : 指明上面User对应的identityFile路径

编辑好后 command + s保存下,这样就OK了

7、测试

$ ssh -T git@github.com

第一次链接会出现提示Are you sure you want to continue connecting (yes/no)? 输入yes回车。
提示下面的就代表成功了!
Hi riceFun! You've successfully authenticated, but GitHub does not provide shell access.

上一篇 下一篇

猜你喜欢

热点阅读