解決git本地多ssh key的問題

最近手上一個項目須要使用到一臺服務器做爲專用的部署服務器,在實施過程當中遇到了一些問題,具體以下:javascript

1. 服務器的ssh默認端口和項目git倉庫的ssh端口不一致
2. 部署須要使用項目提供的ssh key,不能使用服務器自己的默認ssh key

這些問題都被順利解決了,這裏特記錄一下,防止遺忘。html


針對上述問題,下面主要從這三個點來記錄解決方案。java

  1. 如何生成ssh keypython

  2. 如何使用特定ssh端口從git倉庫拉取項目git

  3. 如何使用特定密鑰文件從git倉庫拉取項目github

1、生成 ssh key

系統默認的ssh key存放在以下目錄:shell

[root@hostname ~]# cd ~/.ssh/
[root@hostname .ssh]# ls
authorized_keys  id_rsa  id_rsa.pub  known_hosts

咱們將新建.git目錄,用來存放git相關部署key的公私鑰。ruby

[root@hostname .ssh]# mkdir ~/.git[root@hostname .ssh]# ssh-keygen -t rsa -f ~/.git/pub_coding.id_rsa -C "sunsky.lau@gmail.com"Generating public/private rsa key pair.
Enter passphrase (empty for no passphrase):  # 回車Enter same passphrase again:                 # 回車Your identification has been saved in /root/.git/pub_coding.id_rsa.
Your public key has been saved in /root/.git/pub_coding.id_rsa.pub.
The key fingerprint is:b1:30:9c:9c:24:78:54:1e:b1:bb:d9:65:3a:44:8c:3b sunsky.lau@gmail.com
The key's randomart p_w_picpath is:
+--[ RSA 2048]----+
|   oo.=.         |
|  . .* B         |
|   .  @ +        |
|       * o       |
|      E S o      |
|       * +       |
|      o +        |
|         .       |
|                 |
+-----------------+
[root@hostname .ssh]# ls ~/.git/
pub_coding.id_rsa  pub_coding.id_rsa.pub

經過上述操做,在~/.git目錄下生成了2個文件,其中pub_coding.id_rsa爲私鑰,pub_coding.id_rsa.pub爲公鑰。
咱們須要將公鑰添加到相關版本控制軟件的帳戶下。服務器


2、使用特定ssh端口從git倉庫拉取項目

這種狀況通常會發生在咱們本地的ssh默認端口和git倉庫的ssh端口不一致時。好比,咱們本地使用了57832做爲ssh默認端口,而git倉庫使用了22做爲ssh默認端口。
這種狀況,對使用https方式訪問git倉庫的用戶是不會受到影響的,可是會致使使用ssh方式訪問git倉庫的用戶拉取項目使用。markdown

針對這個問題,這裏提供兩種解決方法:

  1. 使用ssh://的方式拉取項目

    [root@hostname .ssh]# git clone ssh://git@git.coding.net:端口號/用戶名/項目名稱.git

    咱們能夠在上面的命令中去指明對應的ssh的端口號。

  2. 使用ssh config配置來自定義端口
    這種方式,咱們將放到管理多ssh key的段落中去作介紹。


3、使用特定密鑰文件從git倉庫拉取項目

一、介紹

這個問題,換句話說就是如何git如何使用多ssh key。針對這種多ssh key的管理,咱們目前主要經過定義ssh的客戶端配置文件來實現。
咱們能夠在ssh的客戶端配置文件文件中定義服務器別名、服務器地址以及針對特定服務器使用的一些專用鏈接配置信息。
有關ssh的客戶端配置文件,咱們能夠經過man config來獲取相關的介紹,這裏簡單放一部分介紹。

NAME
     ssh_config - OpenSSH SSH client configuration files

SYNOPSIS
     ~/.ssh/config
     /etc/ssh/ssh_config

DESCRIPTION
     ssh(1) obtains configuration data from the following sources in the following order:           1.   command-line options           2.   user’s configuration file (~/.ssh/config)           3.   system-wide configuration file (/etc/ssh/ssh_config)

從描述中,咱們能夠看到,有關ssh的客戶端配置文件有2個,分別是~/.ssh/config/etc/ssh/ssh_config。他們一個是用戶範圍的配置,一個是系統範圍的配置。
因爲咱們的操做要限定在用戶範圍,所以要使用~/.ssh/config文件。

二、配置

須要注意的是,~/.ssh/config文件默認不存在,須要用戶本身建立。
樣例文件:

[root@hostname ~]# touch ~/.ssh/config
[root@hostname ~]# cat ~/.ssh/config 
# github key
Host git-github
    Port 22
    User git    
    HostName git.github.com    
    PreferredAuthentications publickey    
    IdentityFile ~/.git/pub_github.id_rsa

# coding key
Host git-coding    
    Port 22
    User git    
    HostName git.coding.net    
    PreferredAuthentications publickey    
    IdentityFile ~/.git/pub_coding.id_rsa

下面對上述配置文件中使用到的配置字段信息進行簡單解釋。

Host
    它涵蓋了下面一個段的配置,咱們能夠經過他來替代將要鏈接的服務器地址。
    這裏可使用任意字段或通配符。
    當ssh的時候若是服務器地址能匹配上這裏Host指定的值,則Host下面指定的HostName將被做爲最終的服務器地址使用,而且將使用該Host字段下面配置的全部自定義配置來覆蓋默認的`/etc/ssh/ssh_config`配置信息。
Port
    自定義的端口
User
    自定義的用戶名
HostName
    真正鏈接的服務器地址
PreferredAuthentications
    指定優先使用哪一種方式驗證,支持密碼和祕鑰驗證方式
IdentityFile
    指定本次鏈接使用的密鑰文件

經過上面設置以後,咱們就可使用多ssh key來鏈接不一樣的git倉庫了

三、鏈接測試

咱們可使用ssh來進行鏈接驗證測試。

[root@hostname .ssh]# ssh -T git@git-coding
Hello 用戶名 You've connected to Coding.net by SSH successfully!
[root@hostname .ssh]# ssh -T git@git-github
Hi 用戶名! You've successfully authenticated, but GitHub does not provide shell access.

四、拉取項目設置

經過上述設置以後,咱們就能夠經過不一樣的Host來針對不一樣的git倉庫和git項目使用不一樣的ssh key了。可是,這裏還須要注意的是,一般狀況下咱們從git倉庫拉取的項目ssh訪問地址,相似這種git@git倉庫地址:用戶名/項目名.git。咱們必定要把這裏的git倉庫地址替換爲咱們ssh config裏面設定的Host。
範例:

[root@hostname .ssh]# git clone git@github.com:用戶名/項目名.git
替換爲以下
[root@hostname .ssh]# git clone git@pub_github:用戶名/項目名.git

到這裏就大功告成了!

參考連接:
多個ssh key的管理
git生成ssh key及本地解決多個ssh key的問題
ssh配置講解大全
ssh_config 文件配置詳解
ssh config配置更新

相關文章
相關標籤/搜索