天天看點

git使用sshkey免密碼登陸

最近在oschina上托管項目,oschina上的項目都是用git來管理。git有個很麻煩的地方就是每次送出代碼,都要求輸入oschina的使用者名和密碼進行驗證,極大的影響效率。oschina提供了SSH Key通路的方法,該方法隻要在oschina上添加公鑰,在本地使用密鑰就可以免密碼連接配接,參考教程。

首先用ssh-keygen生成sshkey

ssh-keygen -t rsa -C "[email protected]" -f "d:\id_rsa"
           

[email protected]是個人郵箱

d:\id_rsa 是生成的sshkey檔案

接下來會要求輸入私鑰密碼,如果想留白可以直接按回車(Enter)

Enter passphrase (empty for no passphrase):
Enter same passphrase again:
           

完成後會有如下提示,下面的key值可能不一樣

The key fingerprint is:
bf:3c:17:0b:16:31:86:bb:c4:f3:06:75:7d:83:72:78 [email protected]
           

最後生成兩個檔案id_rsa和id_rsa.pub,把這兩個檔案放到.ssh檔案夾下,windows中.ssh檔案夾一般在系統盤的使用者下(c:\users\)

git使用sshkey免密碼登陸

用記事本把id_rsa.pub打開,把文本添加到oschina的公鑰清單中

git使用sshkey免密碼登陸

在git bash中輸入

ssh -T [email protected]
           

傳回Welcome to [email protected], 你的名字! 表示添加成功。

注意事項:

生成的sshkey檔案一定要命名為id_rsa,因為ssh預設讀id_rsa的sshkey。

在完成以上配置後,送出代碼還是需要輸入使用者名和密碼,可以到本地git repository的.git\config檔案,如果url使用的是https協定,改為git協定即可。

修改前

[remote "origin"]
	url = https://git.oschina.net/oschina/git-osc.git
	fetch = +refs/heads/*:refs/remotes/origin/*
           

修改後

[remote "origin"]
	url = [email protected]:oschina/git-osc.git
	fetch = +refs/heads/*:refs/remotes/origin/*