1. Can I do something like:

    gpg --public-key my.pub  -e file.txt
  2. If I can, any reason that I should not do that?

P/s: I think I don't need to know about the recipient because my machine only has one public key at a time.
But that key will change soon (and I can delete all the old encrypted files, so no need to keep them).

4 Answers 4


GnuPG does not support encrypting to a recipient specified by a key file. The key must be imported in advance, and the recipient defined with either his mail address or key ID.

I'd recommend to use a cleaner approach as expected by GnuPG and hard-code either the key's fingerprint, or a user ID given by that key and import it as usual.

If you really do not want to import the key, you could do following as workaround (which actually imports the key, but to a temporary GnuPG home directory):

  1. Import the key to a temporary folder, for example using

    gpg --homedir /tmp/gnupg --import my.pub
  2. Determine the key ID of the key stored in the file:

    KEYID=`gpg --list-public-keys --batch --with-colons --homedir /tmp/gnupg | head -n1 | cut -d: -f5`
  3. Encrypt a message to the recipient

    gpg --homedir /tmp/gnupg --recipient ${KEYID} --encrypt
  4. Clean up temporary GnuPG home directory

    rm -f /tmp/gnupg

You could of course save this as a script to make using it more convenient.

  • i get gpg: keyblock resource '/tmp/gnupg/pubring.kbx': No such file or directory after the first command May 23, 2019 at 21:41
  • This looks like an indication GnuPG realized no keyring exists so far but is getting initalized now; is it working anyway? If so, you can simply ignore this message.
    – Jens Erat
    May 24, 2019 at 18:19

Since GnuPG 2.1.14 there is a new option allowing to encrypt from a keyfile: --recipient-file FILENAME. It works from an binary or an ascii armored file.

Check the release notes or the dev mailing list.

  • Is there a way to do the same for --local-user? It seems not... Aug 9, 2019 at 14:45

It appears from reviewing your question that you're interested in encrypting using your own public key which you'd already have (my.pub in the example).

Indeed, not only is it possible, it's desirable as it serves to provide non-interactive automation of encryption. If it's YOUR public key, then you trust it implicitly and can do the following without worry:

gpg --batch --yes --trust-model always -r $GPGPUBKEYRECIPIENTEMAIL -e ./file.txt

No interactive prompts that require an answer so encryption can be scripted. NOTE: I upload my PUBLIC key to the public server I want to protect data on, keeping the PRIVATE key apart from it.

However, if you're NOT encrypting with your own key, the --trust-model always switch could be dodgy. Also note that when decrypting, you'll be prompted for a password unless you automate that of course. HTH bud- Terrence Houlahan


As far as I know, the recipient's public key IDs, key Validity dates, name, and email address are embedded in the GPG ASCII Armor file (GnuPG Manual )

pub  1024D/BB7576AC 1999-06-04 Alice (Judge) <alice@cyb.org>

So using pub key file / Key ID / Name / Email to identify which public key to use should all be equivalent.

BUT I don't have access to a linux machine right now, so can't check.

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.