Skip to content

一台电脑配置多个 ssh ​

2023-07-17 18:26:29

本文主要记录下在一台电脑上需要连接多个不同代码平台时,配置多个 ssh 的过程。

生成 ssh ​

我们通过命令生成 ssh

shell
ssh-keygen -t rsa -b 4096 -C "邮箱地址"

默认生成的 ssh 私钥为 id_rsa,公钥为 id_rsa.pub

所以我们生成第二个ssh时要改下名字,不然会被覆盖掉。

在第二次使用命令生成ssh时会提示输入名字,我们这里使用github_id_rsa。

将生成好的公钥添加到对应的代码管理平台中。

那么目前在 ~/.ssh 文件夹下就会有五个文件

  • id_rsa 和 id_rsa.pub
  • github_id_rsa 和 github_id_rsa.pub
  • known_hosts

TIP

known_hosts 为保存已认证的远程主机ID,用于client端验证server端的文件

配置config ​

在 ~/.ssh 文件夹下创建一个 config 文件

shell
# 创建config文件
touch config

# 编辑文件
vi config

在文件中输入

txt
Host gitlab.com   
HostName gitlab.com   
PreferredAuthentications publickey  
IdentityFile ~/.ssh/id_rsa 

Host github.com
HostName github.com
PreferredAuthentications publickey
IdentityFile ~/.ssh/github_id_rsa

保存退出,现在电脑中就存在两个ssh了,在不同的平台上都可以进行代码仓库的拉取推送了。

Released under the MIT License.