I expect the following command to extract the gpg file without asking for password:

  gpg --passphrase 1234 file.gpg

But it asks for the password. Why?

This also have the same behavior:

  gpg --passphrase-file passfile.txt file.gpg

I use Ubuntu with gnome 3, and remember that it was working in Fedora

  • 1
    Are you sure gpg run the right command, not an alias nor a wrapper? Try /usr/bin/gpg --passphrase 1234 file.gpg , type gpg , gpg --version and set | grep '^.\{0,9\}PG' – F. Hauri Jan 8 '13 at 20:07
  • Just for the record, if you use the old version of GPG, it should work (on Ubuntu and such, it's the gnupg1 package. However, they discourage using it unless you have to. – Shule Aug 23 '18 at 5:16
  • Also note that in GPG 2.x gpg --list-packets --batch myFile.gpg prompts for a passphrase, while it doesn't in GPG 1.x. That was my problem (in a program that I'm writing), while I thought I had your problem (the --list-packets thing executed first, before attempting to decrypt, and I didn't notice). So, I made a new way to determine if files were encrypted. – Shule Aug 24 '18 at 11:01

16 Answers 16


I am in your exact same boat (it worked on Fedora but not Ubuntu). Here is an apparent work around I discovered:

echo your_password | gpg --batch --yes --passphrase-fd 0 your_file.gpg

Explanation: Passing 0 causes --passphrase-fd to read from STDIN rather than from a file. So, piping the passphrase will get --passphrase-fd to accept your specified password string.

  • 15
    adding --batch --yes to the above worked for me. – Ryan Tuck Oct 29 '15 at 15:37
  • 1
    But then I get a problem, if I want to encrypt data that is piped into gpg, e.g. echo "encrypt me" | gpg --passphrarse "mypw" -c -o test.gpg. How do I solve this? – con-f-use Mar 16 '17 at 14:36
  • @con-f-use I think you'll have to save either the plaintext or the passphrase to a temporary file. I don't see any way around it. – jonS90 Mar 18 '17 at 17:33
  • 2
    Well, with the Ubuntu version of gpg, echo "encrypt me" | gpg --passphrase "mypassphrase" --batch --quiet --yes --no-use-agent -c -o encrypted.gpg seems to work. – con-f-use Mar 19 '17 at 7:25
  • 1
    I'm getting Inappropriate ioctl for device with and without --batch here (on gpg (GnuPG) 2.1.18). – Nico Schlömer Dec 17 '17 at 14:35

Upgraded 2017-12-04. (Adding --batch in order to prevent passphrase prompt)

You may have to add --batch option:

From version 2 of GPG, the option --batch is needed to ensure no prompt... Ok, looking that:

$ gpg --version
gpg (GnuPG) 2.1.18
libgcrypt 1.7.6-beta
Copyright (C) 2017 Free Software Foundation, Inc.
License GPLv3+: GNU GPL version 3 or later <https://gnu.org/licenses/gpl.html>
This is free software: you are free to change and redistribute it.
There is NO WARRANTY, to the extent permitted by law.

Home: /home/user /.gnupg
Supported algorithms:
Hash: SHA1, RIPEMD160, SHA256, SHA384, SHA512, SHA224
Compression: Uncompressed, ZIP, ZLIB, BZIP2


$ newdir=$(mktemp -d)
$ cd $newdir
$ seq 1 10 | gpg -c --batch --passphrase 1234 -o file.gpg -
$ ls -ltr
total 4
-rw-r--r-- 1 user  user  91 Dec  4 15:42 file.gpg
$ hd file.gpg 
00000000  8c 0d 04 07 03 02 ea fa  d0 d3 2b 9a ea 06 df d2  |..........+.....|
00000010  4a 01 ed 50 74 ff 27 45  0e 6c 94 74 db e9 8a a5  |J..Pt.'E.l.t....|
00000020  03 9f 67 a0 73 97 e9 15  6b 56 a0 f0 88 71 85 a8  |..g.s...kV...q..|
00000030  dc 41 71 9f fa 3b f9 9d  af ac 80 eb f4 f7 28 19  |.Aq..;........(.|
00000040  9f be 75 47 e6 d8 00 3e  f6 60 f1 00 5e 63 57 ef  |..uG...>.`..^cW.|
00000050  14 c3 4b 20 ff 94 03 03  c1 fc 98                 |..K .......|

sound good! Well, now:

$ gpg -d --batch --passphrase 1234 file.gpg
gpg: AES encrypted data
gpg: encrypted with 1 passphrase

While no -d parameter is given (same syntaxe as SO's question), decrypted datas from file.gpg will be extracted to a new file.

$ gpg --batch --passphrase 1234 file.gpg
gpg: WARNING: no command supplied.  Trying to guess what you mean ...
gpg: AES encrypted data
gpg: encrypted with 1 passphrase

$ ls -ltr
total 8
-rw-r--r-- 1 user  user  91 Dec  4 15:42 file.gpg
-rw-r--r-- 1 user  user  21 Dec  4 15:44 file

$ cat file

This work well!

$ cd -
$ rm -fR $newdir
$ unset newdir
  • You are not getting the warning "gpg: gpg-agent is not available in this session" so you probably have the passphrase stored in the agent, perhaps? – Asfand Qazi Aug 20 '15 at 16:32
  • @AsfandYarQazi No, the passphrase is entered in command line. – F. Hauri Aug 23 '15 at 17:10
  • This answer worked for me. Ubuntu with gpg 1.4.16. The --passphrase parameter works for batch operations, and doesn't prompt for a password. – Trevor Sullivan Apr 28 '16 at 0:27
  • This may appear to work because the annoying gpg-agent is caching the passphrase. Try rebooting system completely and starting fresh, or entering wrong --passphrase 5678 (wrong passphrase). – yahermann Jan 5 at 22:14
  • @yahermann: Just tried now after unset GPG_AGENT_INFO and even GPG_AGENT_INFO=/dev/null, this (continue) to work... Maybe unsetting $GPG_AGENT_INFO may help!? (Let us know; please answer, I'll edit if this help!) – F. Hauri Jan 6 at 18:36

It sounds like you're using gpg2. You need to throw in the --batch option as well. (If you're planning to add this to a script, you'll also want to add in --no-tty and probably --yes.)

  • 3
    It's 1.4. using --batch has no effect. – Omid Jan 4 '13 at 10:11
  • Sorry then @Nima. I don't know what to tell you. With GnuPG v1.4 you shouldn't need to do anything else to pass the passphrase in with either of those options. – rsaw Jan 4 '13 at 15:19
  • Good note, @rsaw, helped me prevent password prompts (and slightly less elegant echo/STDIN option). – ryanm Oct 3 '16 at 16:39
  • --batch helped even in windows. woo hoo. – Hybridzz Jan 12 '17 at 1:25

For gpg version 2.x you don't need to use --batch, just

--pinentry-mode loopback  

works with --passphrase & --passphrase-file, and will let you enter new info, in case of filename conflicts for example:

gpg --pinentry-mode loopback --passphrase-file=file encrypted.gpg

File 'encrypted' exists. Overwrite? (y/N)n
Enter new filename: f2

unlike --batch that will quickly fail, saying ...failed: File exists

(tested on Debian Stable/Stretch's gpg 2.1.18. This behaviour of ignoring important --passphrase options really should be a bug, if it isn't already)

  • 1
    This works nicely also on Ubuntu 18.04 Bionic with gpg (GnuPG) 2.2.4 – Disassembler May 31 '18 at 10:52
  • This works on MacOS after installing gpg with homebrew – Joel Feb 15 at 18:42

for me, adding "--no-use-agent" solved this for "gpg (GnuPG) 1.4.16":

date > foo
echo pass > passphrase
# w/o --no-use-agent
> rm -f foo.gpg; gpg --cipher-algo aes256 --output foo.gpg --passphrase-file ./passphrase --batch --yes --symmetric foo
gpg: gpg-agent is not available in this session
gpg: can't query passphrase in batch mode
gpg: error creating passphrase: invalid passphrase
gpg: symmetric encryption of `foo' failed: invalid passphrase

> rm -f foo.gpg; gpg --cipher-algo aes256 --output foo.gpg --passphrase-file ./passphrase --batch --yes --no-use-agent --symmetric foo
> ls -al
total 20
drwxr-xr-x  2 root root 4096 Aug 22 13:59 .
drwx------ 18 root root 4096 Aug 22 13:58 ..
-rw-r--r--  1 root root   30 Aug 22 13:58 foo
-rw-r--r--  1 root root  103 Aug 22 13:59 foo.gpg
-rw-r--r--  1 root root    5 Aug 22 13:58 passphrase

It worked like magic for me:

echo "YOUR_PASS_PHRASE" | gpg --batch --yes  --passphrase-fd 0 /home/somewhere/your_file.pgp
  • error: gpg: no valid OpenPGP data found. gpg: processing message failed: eof. Any ideas? – Gabriel Staples Mar 6 '18 at 3:18

If using gpg (GnuPG) 2.2.7 According to the man page,

--passphrase-fd n

Read the passphrase from file descriptor n. Only the first line will be read from file descriptor n. If you use 0 for n, the passphrase will be read from STDIN. This can only be used if only one passphrase is supplied.

--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.

--passphrase string

Use string as the passphrase. This can only be used if only one passphrase is supplied. Obviously, this is of very questionable security on a multi-user system. Don't use this option if you can avoid it.

add --pinentry-mode loopback in order to work

Note that since Version 2.0 this passphrase is only used if the option --batch has also been given. Since Version 2.1 the --pinentry-mode also needs to be set to loopback.

For example:

gpg --batch --yes --passphrase="pw" --pinentry-mode loopback -o out -d in
  • Are both the --batch and --pinentry-mode loopback options needed for any --passphrase... option to work? On v.2.1.18 the info page says the same thing (but not the man page) about batch & pinentry needed, but still works with only --pinentry... If both really are needed for v.2.2.7 then things are getting ridiculous, developers are introducing serious bugs on purpose – Xen2050 Dec 16 '18 at 16:18
read -sp "Enter passphrase: " pass
echo "${pass}" | gpg --batch --no-tty --yes --passphrase-fd 0 --symmetric -o /path/to/saved/file.jpg.gpg /path/to/file.jpg
echo "${pass}" | gpg --batch --no-tty --yes --passphrase-fd 0 --decrypt -o /path/to/decrypted/file.jpg /path/to/encrypted/file.jpg.gpg

have you tried doing :

gpg --batch --passphrase-fd 0 --decrypt-files *.gpg
gpg --passphrase-fd 0 1234 file.gpg 

Source: Here


I think that a quite secure method to pass the password to the command line is this:

gpg --passphrase-file <(echo password) --batch --output outfile -c file

What this will do is to spawn the "echo" command and pass a file descriptor as a path name to gpg (e.g. /dev/fd/63). gpg will then read the key from there. In the mean time, the echo command should run in parallel and should finish instantly, leaving the key on the buffer of the fd.

Benefits are:

  • The gpg command will not have the password on its command line
  • The echo will be short lived. In fact, it should be almost instant
  • The password will never reside on the disk, there won't be a file to be deleted and if the command is interrupted there will be no leftovers

You won't believe me when I tell you that on ubuntu gpg tries to ask your password if $DISPLAY is set and takes it from commandline --password if you unset it. This works as expected:

DISPLAY=    gpg --symmetric --passphrase pass --batch

Just another example of over engineering I guess.


Here's a link to a stackoverflow answer that maybe of further assistance; I have a project that does bulk decryption/encryption, and due to GnuPG being very strict about passphrases, learned the hard way that --passphrase only works on rare occasions. Instead consider the --passphrase-fd option to be more reliable.

This script makes proper use of the --passphrase -fd option, and has been tested publicly via Travis-CI where you can find logs of it in action.

Now I ain't going to just post links to an answer without providing some example code here, so here's an updated "stand alone" script you can play with:

#!/usr/bin/env bash
# Set some variables for easy modding
Var_gpg_opts="--passphrase-fd ${Var_fd} --decrypt"
Arr_string=( "$@" )
# Open file descriptor and shove the passphrase file into it
if [ -f "${Var_pass}" ]; then
    exec ${Var_fd}<"${Var_pass}"
    exec ${Var_fd}<(echo "${Var_pass}")
# Pipe input array though gpg and append to output file
cat <<<"${Arr_string[*]}" | $(which gpg) ${Var_gpg_opts} >> ${Var_output_location}
# Do not forget to close the file descriptor
exec ${Var_fd}>&-

While the above isn't as fancy as the linked protect at GitHub it should be even more functional than the answer linked at the beginning of this post.

Happy hacking.


As mentioned in man gpg following option can be used

--pinentry-mode mode Set the pinentry mode to mode. Allowed values for mode are:

                 Use the default of the agent, which is ask.

          ask    Force the use of the Pinentry.

          cancel Emulate use of Pinentry's cancel button.

          error  Return a Pinentry error (``No Pinentry'').

                 Redirect Pinentry queries to the caller.  Note that in contrast to Pinentry the user is not prompted again if he enters a bad password.

So default behaviour of gpg is to prompt user for passphrase, if change this user agent mode to " --pinentry-mode loopback " It works perfectly fine. complete command

gpg --pinentry-mode loopback --passphrase <passphrase> -d <file to decrypt>

One simple method I found working on a linux machine is : 1) import key to gpg :=> shell> gpg —import private_key.key

2) decrypt giving outfile name :=> shell> gpg —output -d

2.1) Giving above command will prompt you to enter paraphrase. Enter the paraphrase and it will decrypt the gpg file.

gpg2 -se --passphrase yourpassword --batch --yes -r user@example.com filename
  • 1
    it would be nice if you could explain why this should fix the problem – Zanna Feb 27 '17 at 8:57
  • 1
    While this code snippet may solve the question, including an explanation really helps to improve the quality of your post. Remember that you are answering the question for readers in the future, not just the person asking now! Please edit your answer to add explanation, and give an indication of what limitations and assumptions apply. – Toby Speight Feb 27 '17 at 10:23

Put at the end of ~/.gnupg/gpg.conf:

pinentry-mode loopback

Put at the end of (maybe new) file ~/.gnupg/gpg-agent.conf:


And then run this command:

echo RELOADAGENT | gpg-connect-agent

Now you are able to run this without asking password:

echo "$1" | gpg2 --trust-model always --clearsign --batch --no-tty --quiet --no-verbose --yes -u $2 --digest-algo SHA512
--s2k-digest-algo SHA512 --passphrase "$3"

Where $1 is the text to be encrypted, $2 is the user ID and $3 the password.

Note: I can't remember why it works, but it works. If you know the details, please edit and insert here.

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.