mac配置多个git的ssh-key

前言

有时候我们需要配置多个git网站的ssh-key,来区分公司和个人的仓库。以下分为几种情况来阐述配置方式。
以下以giteegithub作为例子,实际可根据自己情况而定

配置gitee和github

  1. 分别生成giteegithub的ssh-key

    这边的邮箱随便写,没有任何影响,只是用来作为标识

    ssh-keygen -t ed25519 -C "x@x.com" -f ~/.ssh/github_id
    
    ssh-keygen -t ed25519 -C "x@x.com" -f ~/.ssh/gitee_id
    

    连续按三个回车

  2. 进入目录~/.ssh

    cd ~/.ssh
    
  3. 添加配置文件config

    # gitee
    Host gitee.com
    HostName gitee.com
    PreferredAuthentications publickey
    IdentityFile ~/.ssh/gitee_id
    
    # github
    Host github.com
    HostName github.com
    PreferredAuthentications publickey
    IdentityFile ~/.ssh/github_id
    
  4. 此时的文件目录

    	.
    	├── config
    	├── gitee_id
    	├── gitee_id.pub
    	├── github_id
    	└── github_id.pub
    
  5. 验证

    	 命令框输入:ssh -T git@github.com
    	 出现以下就是成功:
    	 Hi ***! You've successfully authenticated, but GITEE.COM does not provide shell access.
    

配置两个gitee

方案一 修改host

  1. 添加host

这里的ip是gitee.com

212.64.62.183 gitee_self.com
  1. 生成两个配置
 ssh-keygen -t ed25519 -C "x@x.com" -f ~/.ssh/gitee_company
 ssh-keygen -t ed25519 -C "x@x.com" -f ~/.ssh/gitee_self
  1. 在目录~/.ssh下创建配置
# gitee_self
Host gitee_self.com
HostName gitee.com
PreferredAuthentications publickey
IdentityFile ~/.ssh/gitee_self
	
# gitee_company
Host gitee.com
HostName gitee.com
PreferredAuthentications publickey
IdentityFile ~/.ssh/gitee_company
  1. 验证下
ssh -T git@gitee_self.com
ssh -T git@gitee.com
  1. clone项目
公司:
git clone git@gitee.com:**/**.git
个人:
git clone git@gitee_self.com:**/**.git

方案二 shell脚本

个人不太喜欢方案一,其实切换ssh-key的频率并不是很高,完全可以写一个脚本去做

  1. 在目录~/.ssh目录下生成config_companyconfig_self
	# config_self
	Host gitee.com
	HostName gitee.com
	PreferredAuthentications publickey
	IdentityFile ~/.ssh/gitee_self
	# config_company
	Host gitee.com
	HostName gitee.com
	PreferredAuthentications publickey
	IdentityFile ~/.ssh/gitee_company
  1. 脚本check_config.sh
# 这里使用了相对路径,根据实际情况,可以替换成绝对路径
parent_path="."
config_self="${parent_path}/config_self"
config_company="${parent_path}/config_company"
config_current="${parent_path}/config"
if [ ${1} -eq 1 ]; then
  cat ${config_company} >${config_current}
else
  cat ${config_self} >${config_current}
fi

# 打印结果
echoResult() {
  current_config_desc="切换完成,当前配置:"$([ ${1} -eq 1 ] && echo "公司配置" || echo "个人配置")
  echo
  echo "                  * * * * * * * * * * * * * * * * * * * * *"
  echo "                  * * * * * * * * * * * * * * * * * * * * *"
  echo "                  * * * * * * * * * * * * * * * * * * * * *"
  echo "                  * * * * * * * * * * * * * * * * * * * * *"
  echo "                  * * * * * * * * * * * * * * * * * * * * *"
  echo "                  * * * \033[33m" $current_config_desc "\033[0m* * * "
  echo "                  * * * * * * * * * * * * * * * * * * * * *"
  echo "                  * * * * * * * * * * * * * * * * * * * * *"
  echo "                  * * * * * * * * * * * * * * * * * * * * *"
  echo "                  * * * * * * * * * * * * * * * * * * * * *"
  echo "                  * * * * * * * * * * * * * * * * * * * * *"
  echo
  echo
  echo
}

echoResult ${1}

  1. 运行
cd ~/.ssh
# 切换到config_company环境
sh check_config.sh  1
# 切换到config_self环境
sh check_config.sh  9
  1. 最终每次切换成功后可以用命令ssh -T git@gitee.com验证下

如果感觉有用,请点个赞,谢谢

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值