I have a text file on my GNU/Linux laptop and I want to encrypt it with my GPG private key so I can decrypt it later and see the output. I've tried this:

gpg --encrypt file.txt

but I'm asked to provide Current recipients. I don't want any recipients, I want to read it myself.

How can I encrypt a file using GPG?

3 Answers 3


I have used this method to encrypt a file gpg -r [email protected] -e ./filename and this will create filename.gpg which is the encrypted content.

And to decrypt you do gpg -d filename.gpg

In regards to the email requirement - when you generate a new key using gpg --gen-key you will be required to enter an email address and it will create a public/privatey key pair based on that email address. You simply need to use that same email address. It does not send it, it simply tell gpg which private/public key pair to use (and the identifier is the email address)

  • But this will encrypt file with public key. Then it will decrypt with private key. How can I encrypt with private so somebody with my public can decrypt it? Nov 16, 2021 at 20:31

A better way is to encrypt using your PUBLIC key, then use your PRIVATE key later to decrypt the file. This way lends itself to automating encryption via a non-interactive script:

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

NOTE: I upload ONLY my PUBLIC key to public server I want to protect data on, keeping my PRIVATE key apart from it. That's pretty tight.

Obviously if you're NOT using your own public key, tread carefully with the --trust-model always switch.

Also be aware that decrypting will of course require a password unless you automate this too. HTH- Terrence Houlahan

  • I think it's the same as accepted answer.
    – jcubic
    Feb 7, 2019 at 8:16
  • The accepted answer implies both public and private keys are on the same host. My answer proposes uploading ONLY your PUBLIC key to the host encrypting the data. Without the --trust-model always switch I add to the command, an error will be puked about trust and break non-interactive automation. So the approaches are technically very different.
    – F1Linux
    Feb 7, 2019 at 8:22

The canonical way is to use --encrypt-to name with your id (typical: mail address) for name. Documentation says that's the way to to "encrypt-to-self".

  • ...may be used with your own user-id as an "encrypt-to-self"... does in my book not indicate that it is a canonical way to do something. What are the advantages and disadvantages of using --encrypt-to name over --recipient?
    – Ini
    Dec 30, 2018 at 16:37
  • @ini I guess I'm inclined to refer to that option as canonical because it's suggested to use it for "encrypt to self" in the official documentation at gnupg.org, see my link. In any case, --recipient will not be sufficient, you would have to add --encrypt (it's not optional). Classification of differences in behavior in "advantages" and "disadvantages" will depend on your use case and would be probably be opinion-based.
    – jvb
    Dec 30, 2018 at 20:33

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .