Multiple SSH keys for different accounts on Github or Gitlab

SSH GIT GITLAB GITHUB
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"

https://coderwall.com/p/7smjkq/multiple-ssh-keys-for-different-accounts-on-github-or-gitlab的更多相关文章

  1. Multiple SSH keys for different accounts on Github or Gitlab

    [inside this square brackets give a name to the followed acc.] name = github_username email = github ...

  2. git SSH keys

    An SSH key allows you to establish a secure connection between your computer and GitLab. Before gene ...

  3. Generating SSH Keys [Ubuntu Linux]

    Generating SSH Keys We strongly recommend using an SSH connection when interacting with GitHub. SSH ...

  4. ssh keys管理工具

    原文地址:https://rtyan.github.io/%E5%B7%A5%E5%85%B7/2017/09/12/ssh-keys-manager.html 引言 我有两个github账户,一个是 ...

  5. Git多个SSH KEYS解决方案(含windows自动化、TortoiseGit、SourceTree等)

    工作过程中,经常会使用到多个git仓库,每个git仓库对应一个账号,可以理解为每个git仓库对应一个ssh key,因此我们需要管理多个ssh key.   一.快速创建ssh key   1. 创建 ...

  6. How To Set Up SSH Keys

    How To Set Up SSH Keys.https://www.digitalocean.com/community/tutorials/how-to-set-up-ssh-keys--2

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

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

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

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

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

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

随机推荐

  1. 【转载】Jsp页面传Json数据到服务端,转对象或集合进行数据处理

    需求:1.将页面数据带到服务端并转成对象,2.将页面的集合数据带到服务端转List实现:用ajax请求传递数据,数据格式为json JS方法: testJsonMethod = function(){ ...

  2. win10家庭版转专业版并激活

    之前重装win10的时候没注意,不小心装成家庭版. 本以为家庭版也没什么,后来发现这对程序员来说造成致命打击. 在系统信息页面底部点击“更改密匙”,输入win10升级产品密匙:VK7JG-NPHTM- ...

  3. 9.在idea中创建Maven项目

    1.新建maven WEB项目 打开-File-New-Project 点击NEXT 2.选择项目组,并给项目命名 3.选择maven路径 添加的配置为 archetypeCatalog=intern ...

  4. MySQL 复制 - 性能与扩展性的基石:概述及其原理

    原文:MySQL 复制 - 性能与扩展性的基石:概述及其原理 1. 复制概述 MySQL 内置的复制功能是构建基于 MySQL 的大规模.高性能应用的基础,复制解决的基本问题是让一台服务器的数据与其他 ...

  5. 【例题4-2 uva489】Hangman Judge

    [链接] 我是链接,点我呀:) [题意] 在这里输入题意 [题解] 水题. 中间就赢了算赢.(重复说,算错 [代码] #include <bits/stdc++.h> using name ...

  6. display显示属性理解

    display属性设置一个元素应如何显示,是我们在前端开发中常常使用的一个属性,其中,最常见的有: 目录 display:none;表示此元素将不被显示. display:block;将元素显示为块元 ...

  7. mybatis使用-高级用法(二)

    新建学生表和学生证表 --学生表 CREATE TABLE student( id INT NOT NULL PRIMARY KEY AUTO_INCREMENT COMMENT 'id', `nam ...

  8. POJ 1985

    求一棵树内最远的两点,DFS,顺便记录以某节点为根内最远的两点的距离,返回最远点的距离.其实是DP. #include <cstdio> #include <iostream> ...

  9. Macserver服务更新经常使用的几个shell命令

    Macserver须要先开启远程登录.开启方式:系统偏好设置->共享 勾选远程登录 经常使用命令有: 拷贝(把本机文件复制到远程server上) scp -r 本地路径/file userNam ...

  10. 出错Can't convert 'WebElement' object to str implicitly