4

配置git-credentials完成git身份认证

 2 years ago
source link: https://blog.sbw.so/u/use-git-credentials-auth-system.html
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.
neoserver,ios ssh client

配置git-credentials完成git身份认证

来源: sbw Blog | 浏览: 5770 | 评论: 0 发表时间: 2020-06-01

在使用git协议拉取远程代码时,是需要进行用户身份认证的。虽然使用github的开源库+HTTPS协议可以避免认证,但有些不方便公开的私有库就没办法使用了。在使用一些自动化脚本或构建系统时,无法人工手动的输入密码,这就需要使用git所提供的credentials功能来完成用户认证了。

简单来说,git-credentials是和用户约定了一个存储和读取用户名/密码的方式,我们提前将认证密钥存放在一个文件或服务里,当git需要进行用户身份认证时,自动通过credentials预先选择的途径来获取所需要的密码,然后进行认证。

“store”模式

git-credentials有许多认证存储后端可以选择,最简单易用(但安全性较差)的方式就是store模式。你的认证信息会被明文保存在一个本地文件中,并且没有过期限制。可以使用下面的命令设置git-credentials为store模式:

git config --global credential.helper store

默认情况下,所使用的存储文件是~/.git-credentials文件,里面每条凭据一行,一般格式为:

https://user:[email protected]

为了安全,这里的password一般并不是账户的密码,以Github为例,这里使用的是AccessToken,可以在Settings-Developer Settings-Personal access tokens中生成新的密钥,然后配置git-credentials使用此密钥访问。

配置好credentials服务和密钥后,在其它使用git命令需要认证的时候,就会自动从这里读取用户认证信息完成认证了。

更安全的使用方式

以文本文件进行密钥存储只是为了自己个人计算机方便使用的场景下比较合适。在Windows/Linux/Mac OS下,都可以选用更安全的密码保存方案,如Github所推荐的这些工具。毕竟git-credentials只是约定了一个密码存储及获取的标准,你甚至可以像这样自定义一个密码存储后端来进行认证凭证的存储。


About Joyk


Aggregate valuable and interesting links.
Joyk means Joy of geeK