ssh 免密登录,git免密登录

2018-03-27  本文已影响780人  UUID

项目在使用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

后面操作的时候只需要输入一次密码之后,就可以免密码操作了。

上一篇下一篇

猜你喜欢

热点阅读