github使用ssh秘鑰的好處以及設置(轉)


git使用https協議,每次pull,push都要輸入密碼,使用git協議,使用ssh秘鑰,可以省去每次輸密碼

大概需要三個步驟:
一、本地生成密鑰對;
二、設置github上的公鑰;
三、修改git的remote url為git協議。




一、生成密鑰對。
=============
大多數 Git 服務器都會選擇使用 SSH 公鑰來進行授權。系統中的每個用戶都必須提供一個公鑰用於授權,沒有的話就要生成一個。生成公鑰的過程在所有操作系統上都差不多。首先先確認一下是否已經有一個公鑰了。SSH 公鑰默認儲存在賬戶的主目錄下的 ~/.ssh 目錄。進去看看:
$ cd ~/.ssh 
$ ls
authorized_keys2  id_dsa       known_hosts config            id_dsa.pub
關鍵是看有沒有用 something 和 something.pub 來命名的一對文件,這個 something 通常就是 id_dsa 或 id_rsa。有 .pub 后綴的文件就是公鑰,另一個文件則是密鑰。假如沒有這些文件,或者干脆連 .ssh 目錄都沒有,可以用 ssh-keygen 來創建。該程序在 Linux/Mac 系統上由 SSH 包提供,而在 Windows 上則包含在 MSysGit 包里:
$ ssh-keygen -t rsa -C "your_email@youremail.com"


# Creates a new ssh key using the provided email # Generating public/private rsa key pair. 


# Enter file in which to save the key (/home/you/.ssh/id_rsa):


直接Enter就行。然后,會提示你輸入密碼,如下(建議輸一個,安全一點,當然不輸也行):
Enter passphrase (empty for no passphrase): [Type a passphrase] 
# Enter same passphrase again: [Type passphrase again]
完了之后,大概是這樣。
Your identification has been saved in /home/you/.ssh/id_rsa. 
# Your public key has been saved in /home/you/.ssh/id_rsa.pub. 
# The key fingerprint is: # 01:0f:f4:3b:ca:85:d6:17:a1:7d:f0:68:9d:f0:a2:db your_email@youremail.com
這樣。你本地生成密鑰對的工作就做好了。




二、添加公鑰到你的github帳戶
========================
1、查看你生成的公鑰:大概如下:
$ cat ~/.ssh/id_rsa.pub  
ssh-rsa AAAAB3NzaC1yc2EAAAABIwAAAQEAklOUpkDHrfHY17SbrmTIpNLTGK9Tjom/BWDSU GPl+nafzlHDTYW7hdI4yZ5ew18JH4JW9jbhUFrviQzM7xlE
LEVf4h9lFX5QVkbPppSwg0cda3 Pbv7kOdJ/MTyBlWXFCR+HAo3FXRitBqxiX1nKhXpHAZsMciLq8V6RjsNAQwdsdMFvSlVK/7XA t3FaoJoAsncM1Q9x5+3V
0Ww68/eIFmb1zuUFljQJKprrX88XypNDvjYNby6vw/Pb0rwert/En mZ+AW4OZPnTPI89ZPmVMLuayrD2cE86Z/il8b+gw3r3+1nKatmIkjn2so1d01QraTlMqVSsbx NrRFi9wrf+M7Q== schacon@agadorlaptop.local
2、登陸你的github帳戶。然后 Account Settings -> 左欄點擊 SSH Keys -> 點擊 Add SSH key
3、然后你復制上面的公鑰內容,粘貼進“Key”文本域內。 title域,你隨便填一個都行。
4、完了,點擊 Add key。


這樣,就OK了。然后,驗證下這個key是不是正常工作。
$ ssh -T git@github.com
# Attempts to ssh to github
如果,看到:
Hi username! You've successfully authenticated, but GitHub does not # provide shell access.
就表示你的設置已經成功了。




三、修改你本地的ssh remote url. 不用https協議,改用git 協議
================================================
可以用git remote -v 查看你當前的remote url
$ git remote -v
origin https://github.com/someaccount/someproject.git (fetch)
origin https://github.com/someaccount/someproject.git (push)
可以看到是使用https協議進行訪問的。


你可以使用瀏覽器登陸你的github,在上面可以看到你的ssh協議相應的url。類似如下:


git@github.com:someaccount/someproject.git


這時,你可以使用 git remote set-url 來調整你的url。


git remote set-url origin git@github.com:someaccount/someproject.git


完了之后,你便可以再用 git remote -v 查看一下。


OK。

以上操作表明我們本地到github上可以實現無需輸入用戶名密碼就可以提交文件。但是實際我們再來提交代碼時仍然提示需要輸入用戶名密碼,這是怎么回事?

[plain]  view plain  copy
 
  1. [root@client css]# git push origin master  
  2. Username for 'https://github.com': yangfei2013  
  3. Password for 'https://yangfei2013@github.com':   
  4. remote: Invalid username or password.  
  5. fatal: Authentication failed for 'https://github.com/yangfei2013/ghblog.git/'  
 

原來git提交有兩個地址一個是https://github.com/yourname/repo.git,還有一個地址是git@github.com:yourname/repo.git,如果我們配置了公鑰,那么再提交代碼時需要使用git@github.com:yourname/repo.git的url來提交。這樣就需要我們配置這個提交的URL地址,沒有修改配置之前我們通過 git config --list可以看到相關配置。

 

[plain]  view plain  copy
 
  1. [root@client css]# git config --list  
  2. user.name=yangfei2013  
  3. user.email=yangfei5459@126.com  
  4. core.repositoryformatversion=0  
  5. core.filemode=true  
  6. core.bare=false  
  7. core.logallrefupdates=true  
  8. remote.origin.url=https://github.com/yangfei2013/ghblog.git  
  9. remote.origin.fetch=+refs/heads/*:refs/remotes/origin/*  
  10. branch.master.remote=origin  
  11. branch.master.merge=refs/heads/master  

 

第三步、配置提交的URL,更改項目路徑下.git/config文件,修改remote.origin.url

更改之后,我們再通過git config --list查看remote.origin.url確實發生了變化

更改之后再次提交代碼,就不出現提示輸入用戶名和密碼了。

 

[plain]  view plain  copy
 
  1. [root@client css]# vi ../.git/config   
  2. [root@client css]# git push origin master  
  3. Warning: Permanently added the RSA host key for IP address '192.30.255.112' to the list of known hosts.  
  4. Counting objects: 7, done.  
  5. Compressing objects: 100% (3/3), done.  
  6. Writing objects: 100% (4/4), 358 bytes | 0 bytes/s, done.  
  7. Total 4 (delta 2), reused 0 (delta 0)  
  8. remote: Resolving deltas: 100% (2/2), completed with 2 local objects.  
  9. To git@github.com:yangfei2013/ghblog.git  
  10.    8dd24e8..3fb9ab9  master -> master  

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM