GPG标志GIT在root拥有的存储库上提交



我有一个root用户拥有的git存储库,我可以使用sudo访问。通常,为了将此回购与git一起使用,我使用 sudo -E git ...-E标志是必要的,因此Sudo中的Git继承了我的默认git配置,例如作者...等

我决定尝试此存储库的GPG签名,因此我启用了选项。

> sudo -E git config user.email 'EMAIL' && sudo -E git config user.signingkey 'SIGNINGKEY' && sudo -E git config commit.gpgsign true && sudo -E git config tag.gpgsign true

但是,在尝试执行提交时:

> sudo -E git commit --allow-empty -m 'Test commit'
error: gpg failed to sign the data
fatal: failed to write commit object

但是,GPG似乎无法使用我的用户配置文件中存在的GPG密钥签名。

我发现了有关此问题的新信息:https://github.com/nixos/nixpkgs/issues/57779

我做了什么:

# Login as root.
$ sudo -i
# Set up .gitconfig and .gnupg.
% mv /root/.gitconfig /root/.gitconfig.orig
% mv /root/.gnupg /root/.gnupg.orig
% ln -s /home/$USER/.gitconfig /root/.gitconfig
% ln -s /home/$USER/.gnupg /root/.gnupg
# Ensure GPG uses given tty and ensure permissions.
% export GPG_TTY=$(tty)
% chown root $(tty)
# Simple test.
% killall gpg-agent
% echo "test" | gpg --clearsign

确保:

  • gpg --list-keys包括您要签名的密钥。
  • root具有给定$(tty)的权限。
  • echo "test" | gpg --clearsign成功运行。

如果您还使用 ssh键,一个类似的解决方案:

# Login as root.
$ sudo -i
# Set up .ssh.
# NOTE: we must copy these files for correct perms.
% mv /root/.ssh /root/.ssh.orig
% cp -r /home/$USER/.ssh /root/.ssh
# Do some remote action.
% git push

最新更新