同一臺Linux電腦關於多個SSH KEY管理

使用環境:關於同一臺電腦LInux系統下使用多個SSH key 切換使用(或者多用戶使用ssh提交代碼)php

要求,能夠建立不一樣的 PUBLIC KEY ,根據下面步驟設置.html

(1)好比有aaa,bbb,ccc 三個賬號須要生成不一樣的 PUBLIC KEYgit

$ ssh-keygen -t rsa
Generating public/private rsa key pair.
Enter file in which to save the key (/root/.ssh/id_rsa):/root/.ssh/id_rsa_aaa
Enter passphrase (empty for no passphrase):
Enter same passphrase again:
Your identification has been saved in /root/.ssh/id_rsa_aaa.
Your public key has been saved in /root/.ssh/id_rsa_aaa.pub.
The key fingerprint is:
9b:92:f6:1f:d2:72:bd:72:19:45:42:5f:e4:65:33:64 root@AY140122145815620396Z
The key's randomart p_w_picpath is:
+--[ RSA 2048]----+
|           .. .E=|
|            ..o++|
|             o. .|
|              .  |
|        S    .   |
|       . + ..    |
|      + = + .o   |
|     . o +..o.   |
|        ...o.    |
+-----------------+

 進過上面的shell 咱們在 /root/.ssh/ 目錄下建立 id_rsa_aaa  私鑰 和 id_rsa_aaa.pub 公鑰github

注意這裏shell

Enter file in which to save the key (/root/.ssh/id_rsa):/root/.ssh/id_rsa_aaa  #設置路徑,若是不設置默認生成 id_rsa  和  id_rsa.pub

(2)按照上面的步驟逐個生成 bbb 和 ccc 對應的公鑰和私鑰 ........
bash

(3)查看系統ssh-key代理,執行以下命令服務器

$ ssh-add -l
Could not open a connection to your authentication agent.
若是發現上面的提示,說明系統代理裏沒有任何key,執行以下操做
exec ssh-agent bash

若是系統已經有ssh-key 代理 ,執行下面的命令能夠刪除app

$ ssh-add -D


(4)把 .ssh 目錄下的3個私鑰添加的 ssh-agent
dom

$ ssh-add ~/.ssh/id_rsa_aaa
$ ssh-add ~/.ssh/id_rsa_bbb
$ ssh-add ~/.ssh/id_rsa_ccc

依次執行上面三條shell 把三個私鑰添加到 ssh-key 代理裏面ssh

(5)打開github 或者 開源中國 ssh 管理頁面把 對應的公鑰提交保存到代碼管理服務器 (.pub 結尾)

(6)在 .ssh 目錄建立 config 配置文件

nano ~/.ssh/config

輸入以下配置信息

#aaa  (github 配置)
Host aaa
    HostName github.com
    User git
    IdentityFile ~/.ssh/id_rsa_aaa

#bbb  (開源中國 配置)
Host bbb
    HostName git.oschina.net
    User git
    IdentityFile ~/.ssh/id_rsa_bbb

#ccc
........

(6)記住上面一步 Host 裏設置的別名,開始克隆項目,以開源中國爲例

git clone git@git.oschina.net:userName/projectName.git
附:進階配置,已經測試經過:

Host 192.168.0.19,192.168.0.25
    HostName 192.168.0.19,192.168.0.25
    IdentityFile ~/.ssh/id_rsa.pub
    User billtan
Host 
    HostName www.test.com
    IdentityFile ~/.ssh/www.test.com.pub
    PreferredAuthentications publickey
    User admin
相關文章
相關標籤/搜索