如何使用gpg在非交互式环境下加密文件?

我想写一个脚本,可以使用gpg和保存在文件中的密码自动加密文件。
我尝试了这个:
gpg -c --passphrase-fd 0 file.txt < pass.txt

当我在Ubuntu 16.04服务器上运行时,它按预期加密文件。 当我在Ubuntu 18.04桌面上运行时,它会弹出密码管理器模态对话框要求我输入密码。
我该如何跳过对话框进行非交互式加密?
作为一种解决方法,我使用openssl而不是gpg来实现了这一点。
openssl aes-256-cbc -pass file:pass.txt -e -in file.txt -out file.txt.enc
2个回答

我在我的Lubuntu 18.04 LTS上进行了测试。
  • 你所描述的命令行对我来说同样失败了。

  • 下面的命令行对我而言可行,

    gpg --batch -c --passphrase-file pass.txt file.txt
    
请参阅man gpg以获取详细信息。
   --passphrase-file file
          Read  the passphrase from file file. Only the first line will be
          read from  file  file.  This  can  only  be  used  if  only  one
          passphrase is supplied. Obviously, a passphrase stored in a file
          is of questionable security if other users can read  this  file.
          Don't  use  this  option  if  you  can avoid it.  Note that this
          passphrase is only used if the  option  --batch  has  also  been
          given.  This is different from GnuPG version 1.x.

1所以诀窍就是使用--batch。然后--passphrase-file和--passphrase-fd都可以工作。--passphrase-file是我的首选。 - user13097
@user13097,我很高兴能帮助你让它正常工作 :-) - sudodus

如果您不想创建一个密码短语文件,比如在编写脚本时,您可以像这样使用它:
``` gpg --batch --passphrase 'somepass' -c file.txt ```