git-credential-manager-core是微软出品的,管理Git凭证的扩展。
使用Git在线服务时(如Github)要检查客户端的凭证是否有效,如果每次都要通过输入用户名和密码来确认凭证,就显得繁琐。 git-credential-manager-core帮你缓存和管理凭证。
git-credential-manager-core之所以名字中带core,大概是因为它是.net core编写的。
本文主要是讲述如何在Ubuntu Linux上使用pass作为凭证的安全存储。
在Ubuntu上安装需要从https://github.com/GitCredentialManager/git-credential-manager/releases/latest下载最新的deb料包,然后使用dpkg -i
来安装。
之前是在https://packages.microsoft.com/repos/提供安装源的,现已停止。
将Git的凭证管理器配置成git-credential-manager-core:
git config --global credential.helper manager-core
在Ubuntu下,有几种凭证存储方式:
- 纯文本
- https://git-scm.com/docs/git-credential-cache提供的是在内存中存储凭证,无法持久
- freedesktop.org Secret Service API
- GPG/pass
因为主要在命令行下操作,综合下来,pass最符合需求。
配置pass作为凭证存储后端:
export GCM_CREDENTIAL_STORE=gpg
# or
git config --global credential.credentialStore gpg
先用GPG生成一对公钥和私钥(执行gpg --gen-key
),然后通过pass init <gpg-id>
初始化pass。
这时,如果直接使用的话,会出现以下错误:
gpg: public key decryption failed: Inappropriate ioctl for device
原因是没有弹出选项输入密码,解决办法是采用适用于命令行的pinentry-tty或者pinentry-curses作为gpg-agent的密码输入介质。
这边选用的是pinentry-tty,主要是因为pinentry-curses会使用一个大窗口来提示输入密码,显得有点夸张。但是pinentry-tty默认没有安装,需要通过apt安装,并且使用update-alternatives配置成默认的pinentry:
sudo apt install pinentry-tty
sudo update-alternatives --config pinentry # 在产生的对话框中选pinentry-tty
pinentry应该是pin和entry的合体,就是“pin码输入”的意思
编辑~/.gnupg/gpg-agent.conf
,增加以下内容:
pinentry-program /usr/bin/pinentry
执行以下命令,刷新gpg-agent:
gpg-connect-agent reloadagent /bye
最后一步,需要设置export GPG_TTY=$(tty)
,因为这边没有用SSH登录,所以没有SSH_TTY
环境变量。
~~大功告成~~
更多配置选项参考:https://github.com/GitCredentialManager/git-credential-manager/blob/main/docs/configuration.md。
其他参考:
(本篇完)
2022-07-28更新
在~/.gnupg/gpg-agent.conf
中添加以下设置,可以缓存密码,避免每次都需要输入:
default-cache-ttl 34560000
max-cache-ttl 34560000
数值如34560000是以秒为单位。
修改完配置后,需要使用gpg-connect-agent reloadagent /bye
来重启gpg-agent。
(更新完)