說說Git的ssh key,gitsshkey

來源:互聯網
上載者:User

說說Git的ssh key,gitsshkey
ssh key的作用

我們使用ssh登入伺服器時,一般常見的會使用使用者名稱/密碼方式登入,
也可以使用ssh key實行免密碼登入,一般現在這種方式被Git伺服器使用的比較多。

產生ssh key
ssh-keygen -t rsa -C "your name"

這樣預設會在本地的~/.ssh目錄下產生id_rsa,id_rsa.pub兩個檔案,
id_rsa是私密金鑰,id_rsa.pub是公開金鑰。

使用ssh key
  • ssh key方式登入遠程伺服器
    我們需要把id_rsa.pub拷貝到遠程伺服器的~/.ssh下面,
    並改名為authorized_keys,這樣我們就可以使用key的方式登入了
$ ssh git@your.server.comEnter passphrase for key '~/.ssh/id_rsa':

這樣我們輸入shh key的密碼就可以登入了。

  • ssh key方式使用git clone代碼
    如果我們使用的是github/gitcafe, 只需要把id_rsa.pub的內容拷貝出來,
    在github的setting裡面添加即可, 我們就可以使用ssh 方式clone代碼了。
//請修改為你自己的git地址git clone git@github.com:chenyangcun/mydouban.git

多網站使用不同的ssh key

預設ssh登入時使用的是id_rsa檔案,我們也可以指定其他檔案;
首先我們產生不同的key:

ssh-keygen -t rsa -C "user1" -f user1ssh-keygen -t rsa -C "user2" -f user2

我們產生user1,user2兩個不同的ssh key,然後我們在.ssh下建立config檔案,
輸入:

Host company  HostName company.com  User git  IdentityFile ~/.ssh/user1Host github  HostName github.com  User git  IdentityFile ~/.ssh/user2

這樣訪問company.com就會使用user1, 訪問github.com就會使用user2

同一網站使用不同的ssh key

我們有時候需要在同一網站使用不同的ssh key, 比如使用gitcafe的pages服務,
想要建立兩個網站,但是gitcafe上不允許不同的使用者使用同一個ssh key,
我們在config檔案裡做相應的修改

Host gitcafe-site1  HostName gitcafe.com  User git  IdentityFile ~/.ssh/user1Host gitcafe-site2  HostName gitcafe.com  User git  IdentityFile ~/.ssh/user2

然後我們更改git參考的遠程地址

#site1$ git remote remove origin $ git remote add origin git@gitcafe-site1:user1/user1.git#site2$ git remote remove origin $ git remote add origin git@gitcafe-site2:user2/user2.git

關鍵是把gitcafe.com改為gitcafe-site1和gitcafe-site2

如果使用hexo, 在hexo的config檔案中,把deploy的遠程地址改成gitcafe-site1這樣的形式即可
deploy:
type: git
repo: git@gitcafe-site1:user1/user1.git

#

本文出處:http://www.aswifter.com/2015/06/18/git-ssh-key/ 
轉載請在開頭註明本文出處。

歡迎關注我的公眾號,分享Swift開發,Android 開發和互連網內容
號:APP開發人員

著作權聲明:本文為博主原創文章,未經博主允許不得轉載。

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.