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

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.
  • So the trick is to use --batch. Then both --passphrase-file and --passphrase-fd work. --passphrase-file was my first choice. – user13097 Jul 21 '18 at 19:53
  • @user13097, I'm glad I could help you make it work :-) – sudodus Jul 21 '18 at 20:31

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.