項目在使用bitbucket,每次fetch、pull都提示輸入密碼,今天總結一下我是如何實現免密登錄和操作的。
我用的是bitbucket,只要是ssh其他的也差不多,就那么個原理。如果需要幫助的話,可以私信我或者留言,看到后我會及時回復。
方法一:ssh
bitbucket支持的加密有 Ed25519, ECDSA, RSA, and DSA,選擇一個你想要的加密方式就可以,下面我選的是RSA。
//xx.xx@xxx.xx替換成自己的郵箱
ssh-keygen -t rsa -C "xx.xx@xxx.xx"
Generating public/private rsa key pair.
// /home/jin/.ssh/id_rsa 可以替換為自定義的文件
Enter file in which to save the key (/home/jin/.ssh/id_rsa):
// 輸入公鑰秘鑰的加密密碼,一般不用,直接回車就好,下同
Enter passphrase (empty for no passphrase):
Enter same passphrase again:
Your identification has been saved in /home/jin/.ssh/id_rsa.
Your public key has been saved in /home/jin/.ssh/id_rsa.pub.
這樣,公鑰和私鑰就生成好了。
登錄bitbucket,找到 bitbucket settings -> sercurity -> ssh keys 點擊添加,將id_rsa.pub 中的內容復制到key對應的輸入框中,點擊保存。
保存好了之后,測試一下是否成功:
ssh -T git@bitbucket.org
如果出現以下信息,說明配置成功了,xxx為你的用戶名
logged in as xxx.
You can use git or hg to connect to Bitbucket. Shell access is disabled.
注意:
網上很多教程講到這里就結束了,網友也很納悶,配置都對了,為啥還需要輸入密碼?
我來回答你,原因只有一個,那就是現在你的git還是使用著原來的訪問方式。
bitbucket使用兩種方式訪問,ssh & https
所以,需要將你的訪問方式改成ssh,xxx/xxx.git為你自己的倉庫地址
git remote add bitbucket git@bitbucket.org:xxx/xxx.git
檢查一下你git remote的配置
git remote
看到bitbucket了沒有?那個就是我們要的。如果有其他的,需要每次操作時候指定用哪個remote,bitbucket為使用的remote名稱,dev為branch。
git fetch bitbucket dev
或者將其他的刪除。name就是其他的remote的名稱。
git remote remove name
刪的只剩bitbucket就對了。
bitbucket 設置ssh key參考資料:
https://confluence.atlassian.com/bitbucket/ssh-keys-935365775.html
https://confluence.atlassian.com/bitbucket/set-up-an-ssh-key-728138079.html
方法二:git自帶命令行
git config --global credential.helper store
查看配置
git config --list
如果有下面的行,說明配置成功了
credential.helper=store
后面操作的時候只需要輸入一次密碼之后,就可以免密碼操作了。