How can I encrypt a file with gpg non-interactively?

I want to write a script that automatically encrypts a file using gpg and a password that is saved in a file.

I tried this:

gpg -c --passphrase-fd 0 file.txt < pass.txt

When I run this on an Ubuntu 16.04 server, it encrypts the file, as expected. When I run it on an Ubuntu 18.04 desktop, it asks me for a passphrase, using the Password Manager modal dialog.

How can I skip the dialog and do a non-interactive encryption?

As a workaround, I did this using openssl instead of gpg:

openssl aes-256-cbc -pass file:pass.txt -e -in file.txt -out file.txt.enc

Solution 1:

I tested in my Lubuntu 18.04 LTS.

  • Your command line fails for me in the same way as you describe.

  • The following command line works for me,

    gpg --batch -c --passphrase-file pass.txt file.txt
    

See details in 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.