[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. [CTCI] 最小调整有序

    最小调整有序 题目描述 有一个整数数组,请编写一个函数,找出索引m和n,只要将m和n之间的元素排好序,整个数组就是有序的.注意:n-m应该越小越好,也就是说,找出符合条件的最短序列. 给定一个int数 ...

  2. 扩展layui中的自带字体图标

    项目中,虽然layui的字体图标库中已经有了1000多个图标了,但是也有时候不能满足我们自定义图标的需求,所以需要进行自定义,具体步骤如下: 1.在iconfont上找到自己喜欢的图标,也可以上传ui ...

  3. 【九天教您南方cass 9.1】 03 编码法绘制地形图

    同学们大家好,欢迎收看由老王测量上班记出品的cass9.1视频课程 我是本节课主讲老师九天. 测量空间的[九天教您南方cass]专栏是九天老师专门开设cass免费教学班.希望能帮助那些刚入行的同学,并 ...

  4. 【九天教您南方cass 9.1】 04 编码法Ⅱ绘制地形图

    同学们大家好,欢迎收看由老王测量上班记出品的cass9.1视频课程 我是本节课主讲老师九天. 我们讲课的教程附件也是共享的,请注意索取测量空间中. [点击索取cass教程]5元立得 (给客服说暗号:“ ...

  5. sql server 获取动态sql输出结果

    不带输出结果 我们一般会这样写 例子:一个输出6位递增号码结果 ALTER proc GetCode ), ) as declare @sqlstring nvarchar(max) set @sql ...

  6. Java知多少(55)线程

    和其他多数计算机语言不同,Java内置支持多线程编程(multithreaded programming). 多线程程序包含两条或两条以上并发运行的部分.程序中每个这样的部分都叫一个线程(thread ...

  7. Java知多少(80)图形界面设计基础

    早先程序使用最简单的输入输出方式,用户在键盘输入数据,程序将信息输出在屏幕上.现代程序要求使用图形用户界面(Graphical User Interface,GUI),界面中有菜单.按钮等,用户通过鼠 ...

  8. 第三百九十五节,Django+Xadmin打造上线标准的在线教育平台—Xadmin集成富文本框

    第三百九十五节,Django+Xadmin打造上线标准的在线教育平台—Xadmin集成富文本框 首先安装DjangoUeditor3模块 Ueditor HTML编辑器是百度开源的HTML编辑器 下载 ...

  9. Python简易web服务

    利用Python自带的包可以建立简单的web服务器.在DOS里cd到准备做服务器根目录的路径下,输入命令: python -m Web服务器模块 [端口号,默认8000] 例如: python -m ...

  10. JSESSIONID、SESSION、cookie .

    所谓session可以这样理解:当与服务端进行会话时,比如说登陆成功后,服务端会为用户开壁一块内存区间,用以存放用户这次会话的一些内容,比如说用户名之类的.那么就需要一个东西来标志这个内存区间是你的而 ...