一臺電腦配置多個SSH KEY,多個用戶身份

SSH KEY

生成 SSH KEY

$ ssh-keygen -t rsa -C "your_email@example.com"
Generating public/private rsa key pair.
Enter file in which to save the key (/Users/your_user_directory/.ssh/id_rsa):
Enter passphrase (empty for no passphrase):
Enter same passphrase again:

your_email@example.com 改爲本身的郵箱。git

密碼輸入成功後出現以下提示,表示建立成功:github

Your identification has been saved in /Users/your_user_directory/.ssh/id_rsa.
Your public key has been saved in /Users/your_user_directory/.ssh/id_rsa.pub.
The key fingerprint is:
fingerprint值 your_email@example.com
The key's randomart image is:
 +--[ RSA 2048]----+
 | .+ + |
 | =oO. |
 ...

註解:這裏第一次輸入的是文件名,若是直接按回車則會自動生成私鑰和公鑰:id_rsaid_rsa.pub。後面跟着的是密碼確認密碼vim

上方的命令執行屢次則會生成多個 SSH KEY。bash

查看 SSH KEY

$ cat ~/.ssh/id_rsa.pub
ssh-rsa 公開密鑰的內容 your_email@example.com

若是建立時輸入了文件名,上方的id_rsa替換成文件名。dom

此處返回的這段內容可用於使用 SSH KEY 的網站,這裏不做過多闡述。ssh

GIT CONFIG

github 或者 gitlab 等網站都會要求驗證身份。一般狀況下配置一個全局信息就能夠了,針對一些特殊狀況,若是須要配置多個身份信息,能夠爲項目單獨配置。ide

配置全局信息

$ git config --global user.name "Firstname Lastname"
$ git config --global user.email "your_email@example.com"

這個命令會在~/.gitconfig填入如下信息:gitlab

[user]
  name = Firstname Lastname
  email = your_email@example.com

若是須要修改信息,直接修改這個文件便可。網站

配置單獨信息

$ cd your_project
$ git config user.name "Firstname Lastname"
$ git config user.email "your_email@example.com"

這個命令會在項目目錄下輸出文件:/.git/.config.net

這裏設置的姓名和郵箱地址會用在 Git 的提交日誌中。

爲不一樣網站應用各自的 SSH KEY

~/.ssh 目錄下建立 config 文件:

$ vim ~/.ssh/config

輸入如下信息:

Host github.com
    HostName github.com
    User git
    IdentityFile ~/.ssh/id_rsa_a

Host git.oschina.net
    HostName git.oschina.net
    User git
    IdentityFile ~/.ssh/id_rsa_b

...

再把對應的公鑰添加至對應的網站上面。

註解:未加入配置文件的網站會自動應用id_rsa

至此,多個 SSH KEY 就能夠同時使用了。

原文地址:一臺電腦配置多個SSH KEY,多個用戶身份
文章做者:何啓邦
版權聲明:自由轉載-非商用-非衍生-保持署名(創意共享3.0許可證) 轉載請註明出處

相關文章
相關標籤/搜索