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

48 阅读1分钟

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

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、将公钥内容复制粘贴至github/gitlab SSH公钥

4、添加私钥

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

5、添加配置文件

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

6、编辑config文件

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

打开config文件进行如下编辑

# github
Host github.com
    HostName github.com
    PreferredAuthentications publickey
    IdentityFile ~/.ssh/id_rsa_github
    
# 如果出现22的报错请用这个
Host github.com
    HostName ssh.github.com
    Port 443
    PreferredAuthentications publickey
    IdentityFile ~/.ssh/id_rsa_github
    
    
# gitlab
Host gitlab.com
    HostName gitlab.com
    PreferredAuthentications publickey
    IdentityFile ~/.ssh/id_rsa_gitlab

#用于自动添加ssh
Host *
   UseKeychain yes
   AddKeysToAgent yes
   IdentityFile ~/.ssh/id_rsa_github
   IdentityFile ~/.ssh/id_rsa_gitlab
   
# 配置文件参数
# 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.

windows 还没有试过,普通配置一个ssh 请参下面的链接

生成新的 SSH 密钥并将其添加到 ssh-agent