[inside this square brackets give a name to the followed acc.]
name = github_username
email = github_emailaddress
[any other name]
name = github_username
email = github_email
[credential]
helper = osxkeychain
useHttpPath = true

Multiple SSH keys for different accounts on Github or Gitlab

Sometimes you need more accounts than one for access to Github or Gitlab and similar tools. For example you can have one account for your projects at home and second account for your company.

Case 1: Multiple accounts on Github

Create SSH keys with different names

$ ssh-keygen -t rsa -C "your_name@home_email.com"

When you see this message

Generating public/private rsa key pair.
Enter file in which to save the key (/home/user_name/.ssh/id_rsa):

Enter unique name, for example:

id_rsa_home

Next, you'll be asked to enter a passphrase.

So, you'll have created SSH key for your home account, now you can generate SSH key for your company account.

Call SSH key generator again with second mail.

$ ssh-keygen -t rsa -C "your_name@company_email.com"

Enter name for file

id_rsa_company

After all steps you can check that all keys were created.

$ ls ~/.ssh

You should see a similar files list:

id_rsa_home  id_rsa_company  id_rsa_home.pub  id_rsa_company.pub

Now you need a config file for organise these keys.

$ cd ~/.ssh/
$ touch config
$ nano config

Add into config file:

# Home account
Host home.github.com
HostName github.com
PreferredAuthentications publickey
IdentityFile ~/.ssh/id_rsa_home # Company account
Host company.github.com
HostName github.com
PreferredAuthentications publickey
IdentityFile ~/.ssh/id_rsa_company

Next you'll delete cached keys

$ ssh-add -D

If you see a message

Could not open a connection to your authentication agent.

Then enter:

eval `ssh-agent -s`

and try again previous command.

Next, you can check that your keys were added:

$ ssh-add -l
2048 d4:e0:39:e1:bf:6f:e3:26:14:6b:26:73:4e:b4:53:83 /home/user/.ssh/id_rsa_home (RSA)
2048 7a:32:06:3f:3d:6c:f4:a1:d4:65:13:64:a4:ed:1d:63 /home/mateusz/.ssh/id_rsa_company (RSA)

If you haven't any entries then you should add your keys

ssh-add ~/.ssh/id_rsa_company
ssh-add ~/.ssh/id_rsa_home

Now you can check connection

$ ssh -T git@home.github.com
Hi home_user! You've successfully authenticated, but GitHub does not provide shell access. $ ssh -T git@work.github.com
Hi company_user! You've successfully authenticated, but GitHub does not provide shell access.

Note! Check the last paragraph of this tip.

Case 2: Account on Github and Gitlab

This is very similar case to the previous. I won't describe it step by step, because all steps are the same. I'll add only example config file.

For example you have own account for home works and company account on gitlab.

# GITLAB
Host gitlab.company_url.com
HostName gitlab.company_url.com
PreferredAuthentications publickey
IdentityFile ~/.ssh/id_rsa_company # GITHUB
Host github.com
HostName github.com
PreferredAuthentications publickey
IdentityFile ~/.ssh/id_rsa_home

The test connections

$ ssh -T git@gitlab.company_url.com
Welcome to GitLab, CompanyUser! $ ssh -T git@github.com
Hi home_user! You've successfully authenticated, but GitHub does not provide shell access.

As you probably have seen, prefix on hostname isn't required.

You may need to set git config user details for any project.

It's required to distinguish your accounts.

$ cd ~/home_project
$ git config user.name "home_user"
$ git config user.email "your_name@home_email.com"

Multiple SSH keys for different accounts on Github or Gitlab的更多相关文章

  1. mac下生成ssh keys 并上传github仓储

    使用github仓储需要本机生成一个公钥key 添加到自己的git账户SSH keys中   mac 生成方法:   1. 打开终端 输入   ssh-keygen 然后系统提示输入文件保存位置等信息 ...

  2. git的id_rsa.pub的生成(也就是github上的SSH Keys)

    只需要一条语句就可以实现生成id_rsa.pub和id_rsa的目的:ssh-keygen -t rsa -C your_email 注意:这个邮箱是你github上的邮箱.只有在gthub上添加了这 ...

  3. 为github帐号添加SSH keys

    为github帐号添加SSH keys 2012-05-26 00:05 34279人阅读 评论(6) 收藏 举报 ssh文本编辑gitvim工具up 使用git clone命令从github上同步g ...

  4. GitHub 添加 SSH keys

    首先在本地创建 SSH Keys $ ssh-keygen -t rsa -C "18817801185@163.com" 后面的邮箱即为 github 注册邮箱,之后会要求确认路 ...

  5. 为github帐号添加SSH keys(Linux和Windows)

    文章转自:https://blog.cofface.com/archives/406.html/2 一.Linux增加ssh keys方法: 使用git clone命令从github上同步github ...

  6. 为Github 托管项目的访问添加SSH keys

    为了便于访问远程仓库,各个协作者将自己的本地的项目内容推送到远程仓库中,使用 SSH keys 验证github的好处:不用每次提交代码时都输入用户名和密码. 如果SSH key没有添加到github ...

  7. 七、配置ssh keys连通github跟ssh-agent

    jenkins+github配置完成后,能够实现在提交pull request或者直接push时,能够将提交的代码拉去一份到服务器本地,并自动merge:但是代码拉去下来了,部署环境的时候却需要输入登 ...

  8. Generating SSH Keys for github

    由于最近电脑重装了Windows 8.1, 想用github维护一些代码.故不得不重新生成一下ssh key. 按https://help.github.com/articles/generating ...

  9. 添加SSH keys到github帐号

    使用git clone命令从github上同步github上的代码库时,如果使用SSH链接(如我自己的esesgrid项目:git@github.com:hmilycc/esesgrid.git),而 ...

随机推荐

  1. Atitit mysql数据库统计信息

    Atitit mysql数据库统计信息 SELECT table_name, table_rows, index_length, data_length, auto_increment, create ...

  2. [k8s]kube-dns架构图解

    kubedns DNS Policy http://blog.fleeto.us/translation/configuring-private-dns-zones-and-upstream-name ...

  3. [k8s]zookeeper集群在k8s的搭建(statefulset模式)-pod的调度

    之前一直docker-compose跑zk集群,现在把它挪到k8s集群里. docker-compose跑zk集群 zk集群in k8s部署 参考: https://github.com/kubern ...

  4. linux每日命令(36):wc命令

    Linux系统中的wc(Word Count)命令的功能为统计指定文件中的字节数.字数.行数,并将统计结果显示输出. 一.命令格式 wc [-clw][--help][--version][文件... ...

  5. TEST DESIGN TECHNIQUES: AN OVERVIEW

    TEST DESIGN TECHNIQUES: AN OVERVIEW -Test note of “Essential Software Test Design” 2015-11-16 目录: 7. ...

  6. Python测试DB2连通性

    Python测试数据库连通性: #!/usr/bin/python27 #encoding: utf-8 import ibm_db import os import sys def find_db( ...

  7. cuda和显卡驱动版本

    TensorFlow安装时需要cuda+对应的显卡驱动.这里给出英伟达官方的cuda和驱动的对应: 地址在这里 然后在这里可下载最新的显卡驱动(英伟达公版的驱动,程序员友好型) 最后说一下我的Tens ...

  8. emacs自动折行设置

    - emacs自动折行     - 临时设置下 M-x `toggle-truncate-lines`    - init.el 中添加 `(toggle-truncate-lines 1)`

  9. JVM——垃圾回收(GC)

    GC简单介绍 java语言执行在java虚拟机(jvm)上.为了解决有限的空间和性能的保证这个矛盾体,jvm所具备的GC能力.能够有效的清除不用的对象.使空间的利用更加合理.以下介绍该机制的原理. 推 ...

  10. zookeeper 入门(一)

    1 下载安装 wget http://mirrors.hust.edu.cn/apache/zookeeper/stable/zookeeper-3.4.6.tar.gzcp zookeeper-3. ...