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

?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
  • 序言:七十年代末税灌,一起剝皮案震驚了整個濱河市,隨后出現(xiàn)的幾起案子硬耍,更是在濱河造成了極大的恐慌垄琐,老刑警劉巖,帶你破解...
    沈念sama閱讀 216,372評論 6 498
  • 序言:濱河連續(xù)發(fā)生了三起死亡事件经柴,死亡現(xiàn)場離奇詭異狸窘,居然都是意外死亡,警方通過查閱死者的電腦和手機坯认,發(fā)現(xiàn)死者居然都...
    沈念sama閱讀 92,368評論 3 392
  • 文/潘曉璐 我一進(jìn)店門翻擒,熙熙樓的掌柜王于貴愁眉苦臉地迎上來,“玉大人牛哺,你說我怎么就攤上這事陋气。” “怎么了引润?”我有些...
    開封第一講書人閱讀 162,415評論 0 353
  • 文/不壞的土叔 我叫張陵巩趁,是天一觀的道長。 經(jīng)常有香客問我淳附,道長议慰,這世上最難降的妖魔是什么? 我笑而不...
    開封第一講書人閱讀 58,157評論 1 292
  • 正文 為了忘掉前任奴曙,我火速辦了婚禮别凹,結(jié)果婚禮上,老公的妹妹穿的比我還像新娘洽糟。我一直安慰自己炉菲,他們只是感情好,可當(dāng)我...
    茶點故事閱讀 67,171評論 6 388
  • 文/花漫 我一把揭開白布坤溃。 她就那樣靜靜地躺著拍霜,像睡著了一般。 火紅的嫁衣襯著肌膚如雪薪介。 梳的紋絲不亂的頭發(fā)上祠饺,一...
    開封第一講書人閱讀 51,125評論 1 297
  • 那天,我揣著相機與錄音昭灵,去河邊找鬼吠裆。 笑死伐谈,一個胖子當(dāng)著我的面吹牛,可吹牛的內(nèi)容都是我干的试疙。 我是一名探鬼主播诵棵,決...
    沈念sama閱讀 40,028評論 3 417
  • 文/蒼蘭香墨 我猛地睜開眼,長吁一口氣:“原來是場噩夢啊……” “哼祝旷!你這毒婦竟也來了履澳?” 一聲冷哼從身側(cè)響起,我...
    開封第一講書人閱讀 38,887評論 0 274
  • 序言:老撾萬榮一對情侶失蹤怀跛,失蹤者是張志新(化名)和其女友劉穎距贷,沒想到半個月后,有當(dāng)?shù)厝嗽跇淞掷锇l(fā)現(xiàn)了一具尸體吻谋,經(jīng)...
    沈念sama閱讀 45,310評論 1 310
  • 正文 獨居荒郊野嶺守林人離奇死亡忠蝗,尸身上長有42處帶血的膿包…… 初始之章·張勛 以下內(nèi)容為張勛視角 年9月15日...
    茶點故事閱讀 37,533評論 2 332
  • 正文 我和宋清朗相戀三年,在試婚紗的時候發(fā)現(xiàn)自己被綠了漓拾。 大學(xué)時的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片阁最。...
    茶點故事閱讀 39,690評論 1 348
  • 序言:一個原本活蹦亂跳的男人離奇死亡,死狀恐怖骇两,靈堂內(nèi)的尸體忽然破棺而出速种,到底是詐尸還是另有隱情,我是刑警寧澤低千,帶...
    沈念sama閱讀 35,411評論 5 343
  • 正文 年R本政府宣布配阵,位于F島的核電站,受9級特大地震影響示血,放射性物質(zhì)發(fā)生泄漏棋傍。R本人自食惡果不足惜,卻給世界環(huán)境...
    茶點故事閱讀 41,004評論 3 325
  • 文/蒙蒙 一矾芙、第九天 我趴在偏房一處隱蔽的房頂上張望舍沙。 院中可真熱鬧近上,春花似錦剔宪、人聲如沸酱酬。這莊子的主人今日做“春日...
    開封第一講書人閱讀 31,659評論 0 22
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽门扇。三九已至斗锭,卻和暖如春地淀,著一層夾襖步出監(jiān)牢的瞬間,已是汗流浹背岖是。 一陣腳步聲響...
    開封第一講書人閱讀 32,812評論 1 268
  • 我被黑心中介騙來泰國打工帮毁, 沒想到剛下飛機就差點兒被人妖公主榨干…… 1. 我叫王不留实苞,地道東北人。 一個月前我還...
    沈念sama閱讀 47,693評論 2 368
  • 正文 我出身青樓烈疚,卻偏偏與公主長得像黔牵,于是被迫代替她去往敵國和親。 傳聞我的和親對象是個殘疾皇子爷肝,可洞房花燭夜當(dāng)晚...
    茶點故事閱讀 44,577評論 2 353

推薦閱讀更多精彩內(nèi)容