Mac上配置SSH - 多個(gè)SSH

Mac上配置SSH - 多個(gè)SSH

前言

首先做個(gè)說明

  1. 生成一對(duì)默認(rèn)的私鑰公鑰,私鑰自己客戶端留著用,公鑰可以給多個(gè)平臺(tái)用。
  2. 多對(duì)私鑰公鑰,主要是應(yīng)對(duì)一個(gè)平臺(tái)多個(gè)賬號(hào)的情況。當(dāng)然你想每個(gè)平臺(tái)使用不同的私鑰公鑰也是可以的。
  3. 多次生成私鑰公鑰需要自己重新命名,否則會(huì)覆蓋之前的。

一、查看是否已經(jīng)生成過/是否應(yīng)存在了私鑰公鑰

進(jìn)入 用戶/.ssh 目錄

$ cd ~/.ssh

如果找不到目錄或者目錄中沒有東西,證明以前沒有生成過。

或者直接使用命令來查看

$ ls -al ~/.ssh
# Lists the files in your .ssh directory, if they exist

檢查目錄列表,看看您是否已經(jīng)擁有公共SSH密鑰。默認(rèn)情況下,GitHub受支持的公鑰的文件名如下之一。

id_rsa.pub
id_ecdsa.pub
id_ed25519.pub

提示:如果您收到~/.ssh不存在的錯(cuò)誤,則在默認(rèn)位置沒有現(xiàn)有的SSH密鑰對(duì)。您可以在下一步創(chuàng)建新的SSH密鑰對(duì)。

二、生成密鑰對(duì)(私鑰公鑰)

兩種方式,一種是生成的時(shí)候直接添加好路徑;另一種是不帶路徑,需要第二步添加路徑或使用默認(rèn)路徑。

ssh key命名的第一種方式:

$ ssh-keygen -t rsa -C "youremail@yourcompany.com" -f ~/.ssh/id_rsa_github

代碼參數(shù)含義:
-t 指定密鑰類型,默認(rèn)是 rsa ,可以省略。
-C 設(shè)置注釋文字,比如郵箱。
-f 指定密鑰文件存儲(chǔ)文件名。

ssh key命名的第二種方式:

$ ssh-keygen -t rsa -C "youremail@xxx.com"

以上代碼省略了 -f 參數(shù),因此,運(yùn)行上面那條命令后會(huì)讓你輸入一個(gè)文件名,用于保存剛才生成的 SSH key 代碼。

按回車后:

Generating public/private rsa key pair.
Enter file in which to save the key (/Users/xxx/.ssh/id_rsa): id_rsa_github(取個(gè)名字)
Enter passphrase (empty for no passphrase): 
Enter same passphrase again: 
Your identification has been saved in 
id_rsa_github.
Your public key has been saved in 
id_rsa_github.pub.

最好每次生成時(shí)都給SSH Key取個(gè)名字,這樣后面在管理時(shí)自己也一目了然。我這里的格式是id_rsa_項(xiàng)目名_git提供方,我生成的所有key都遵循這個(gè)規(guī)則命名。建議你也有你自己的一種命名方式,并且保持統(tǒng)一。如果不取名字,默認(rèn)的是id_rsa,如果后面生成時(shí)不命名,會(huì)把這個(gè)覆蓋掉。密碼可以不設(shè)置,免得每次提交時(shí)還要輸入一次,安全性自己衡量吧。第一次生成key時(shí),會(huì)在~目錄下創(chuàng)建一個(gè).ssh目錄。

附:
鍵入ssh-keygen -t,然后是鍵類型和可選注釋。此注釋包含在創(chuàng)建的.pub文件中。您可能想使用電子郵件地址進(jìn)行注釋區(qū)分。

例如,對(duì)于ED25519:

ssh-keygen -t ed25519 -C "<comment>"

對(duì)于2048位RSA:

ssh-keygen -t rsa -b 2048 -C "<comment>"

https://docs.gitlab.com/ee/ssh/#add-an-ssh-key-to-your-gitlab-account

三、將SSH密鑰添加到ssh代理中

ssh服務(wù)器默認(rèn)是去找id_rsa,現(xiàn)在需要把這個(gè)key添加到ssh-agent中,這樣ssh服務(wù)器才能認(rèn)識(shí)id_rsa_TestSSH_github。

  • 在后臺(tái)啟動(dòng)ssh-agent。
$ eval $(ssh-agent -s)
> Agent pid 59566

根據(jù)您的環(huán)境,您可能需要使用其他命令。例如,在啟動(dòng)ssh-agent之前,您可能需要通過運(yùn)行sudo -s -H來使用root訪問,或者您可能需要使用exec ssh-agent bashexec ssh-agent zsh來運(yùn)行ssh-agent。

  • 如果您使用的是macOS Sierra 10.12.2或更高版本,則需要修改~/.ssh/config文件,以自動(dòng)將密鑰加載到ssh代理中,并將密碼存儲(chǔ)在鑰匙串中。
  1. 首先,檢查您的~/.ssh/config文件是否存在于默認(rèn)位置。
$ open ~/.ssh/config
> The file /Users/you/.ssh/config does not exist.
  1. 如果文件不存在,請(qǐng)創(chuàng)建文件。
$ touch ~/.ssh/config

打開您的~/.ssh/config文件,然后修改文件以包含以下行。如果您的SSH密鑰文件的名稱或路徑與示例代碼不同,請(qǐng)修改文件名或路徑以匹配當(dāng)前設(shè)置。

Host github.com
  AddKeysToAgent yes
  UseKeychain yes
  IdentityFile ~/.ssh/id_rsa_github

注意:如果您選擇不向密鑰添加密碼,則應(yīng)省略UseKeychain行。

注意:如果您看到這樣的錯(cuò)誤
/Users/USER/.ssh/config: line 16: Bad configuration option: usekeychain
Host *部分添加額外的配置行:
Host *
IgnoreUnknown UseKeychain

注意:關(guān)于config文件,同時(shí)參考下列文件

將不同賬號(hào)的工程圖服務(wù)器與ssh-key關(guān)聯(lián)

# 添加config配置文件
# 配置文件參數(shù)
# Host : Host可以看作是一個(gè)你要識(shí)別的模式,對(duì)識(shí)別的模式,進(jìn)行配置對(duì)應(yīng)的的主機(jī)名和ssh文件
# HostName : 要登錄主機(jī)的主機(jī)名
# User : 登錄名
# IdentityFile : 指明上面User對(duì)應(yīng)的identityFile路徑

#thub user(first@email.com)
Host github1
 HostName git.some.com/
 User git
 IdentityFile /Users/xxx/.ssh/id_rsa

# second user(second@email.com)
 # 建一個(gè)github別名,新建的帳號(hào)使用這個(gè)別名做克隆和更新
Host github2
 HostName github.com
 User git
 IdentityFile /Users/xxx/.ssh/id_ras_bill_github

另一個(gè)例子

#
# Main gitlab.com server
#
Host gitlab.com
RSAAuthentication yes
IdentityFile ~/my-ssh-key-directory/my-gitlab-private-key-filename
User mygitlabusername

另一個(gè)例子

#
# Our company's internal GitLab server
#
Host my-gitlab.company.com
RSAAuthentication yes
IdentityFile ~/my-ssh-key-directory/company-com-private-key-filename

另一個(gè)例子

# GitLab.com
Host gitlab.com
  PreferredAuthentications publickey
  IdentityFile ~/.ssh/gitlab_com_rsa

# Private GitLab instance
Host gitlab.company.com
  PreferredAuthentications publickey
  IdentityFile ~/.ssh/example_com_rsa

另一個(gè)例子

# gitee
Host gitee.com
HostName gitee.com
PreferredAuthentications publickey
IdentityFile ~/.ssh/gitee_id_rsa
# github
Host github.com
HostName github.com
PreferredAuthentications publickey
IdentityFile ~/.ssh/github_id_rsa

首先,檢查您的~/.ssh/config文件是否存在于默認(rèn)位置。

$ open ~/.ssh/config
> The file /Users/you/.ssh/config does not exist.
  • 將您的SSH私鑰添加到ssh代理中,并將密碼存儲(chǔ)在鑰匙串中。如果您使用其他名稱創(chuàng)建了密鑰,或者如果您正在添加具有不同名稱的現(xiàn)有密鑰,請(qǐng)將命令中的id_rsa_github替換為私鑰文件的名稱。
$ ssh-add ~/.ssh/id_rsa_github

ssh-add <directory to private SSH key>
如果執(zhí)行ssh-add時(shí)提示"Could not open a connection to your authentication agent",可以現(xiàn)執(zhí)行命令:

$ ssh-agent bash

然后再運(yùn)行ssh-add命令。

  • 查看添加結(jié)果
ssh-add -l

補(bǔ)充:

# 可以通過 ssh-add -l 來確私鑰列表
$ ssh-add -l
# 可以通過 ssh-add -D 來清空私鑰列表
$ ssh-add -D

四、將公鑰粘貼到git服務(wù)器平臺(tái)上,如GitHub

當(dāng)然可以直接打開剛生成的公鑰文件id_rsa_github.pub文件,復(fù)制全部?jī)?nèi)容去平臺(tái)Add SSH Key;也可以使用命令復(fù)制公鑰文件內(nèi)容貼到平臺(tái) Add SSH Key。

  • 附:使用命令復(fù)制
    將SSH公鑰復(fù)制到剪貼板上
$ pbcopy < ~/.ssh/id_rsa_github.pub
# Copies the contents of the id_rsa_github.pub file to your clipboard

提示:如果pbcopy不起作用,您可以找到隱藏的.ssh文件夾,在您最喜歡的文本編輯器中打開文件,并將其復(fù)制到剪貼板。

其規(guī)則就是:從上至下讀取config的內(nèi)容,在每個(gè)Host下尋找對(duì)應(yīng)的私鑰。
這里將GitHub SSH倉庫地址中的git@github.com替換成新建的Host別名如github2,那么原地址是:git@github.com:username/Mywork.git,替換后應(yīng)該是:github2:username/Mywork.git。

五、測(cè)試連接

測(cè)試一下

$ ssh -T github2
Hi 0xJoker! You've successfully authenticated, but GitHub does not provide shell
  • 輸入以下命令測(cè)試
$ ssh -T git@github.com
# Attempts to ssh to GitHub

您可能會(huì)看到這樣的警告:

> The authenticity of host 'github.com (IP ADDRESS)' can't be established.
> RSA key fingerprint is SHA256:nThbg6kXUpJWGl7E1IGOCspRomTxdCARLviKw6E5SY8.
> Are you sure you want to continue connecting (yes/no)?
  • 驗(yàn)證您看到的消息中的指紋是否與GitHub的RSA公鑰指紋匹配。如果是這樣,則鍵入yes
> Hi username! You've successfully authenticated, but GitHub does not
> provide shell access.

您可能會(huì)看到以下錯(cuò)誤信息:

...
Agent admitted failure to sign using the key.
debug1: No more authentication methods to try.
Permission denied (publickey).

這是某些Linux發(fā)行版的已知問題。有關(guān)更多信息,請(qǐng)參閱“錯(cuò)誤:代理承認(rèn)未能簽名”。

六、常見問題處理

經(jīng)常與會(huì)遇到下面這種情況,我也很納悶,每次都解析到不到域名和地址

ssh: Could not resolve hostname git.oschina.net: 
nodename nor servname provided, or not known

查資料之后的解決辦法是:
step1.
ping address
獲取到對(duì)于地址的ip
step2. 在/etc/hosts中添加一行如下:
ip address
xxxxxxxxxx
這樣就可以玩起來了。


參考:Git配置多個(gè)SSH-Key https://gitee.com/help/articles/4229#article-header0

收集的錯(cuò)誤解決方案,希望能幫到你:

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請(qǐng)結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡(jiǎn)書系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

友情鏈接更多精彩內(nèi)容