How to Use GPG to Encrypt a File: Simple Beginner’s Guide (2026)

To encrypt a file with GPG, run gpg --symmetric --cipher-algo AES256 notes.txt for a passphrase-protected file, or gpg --encrypt --recipient [email protected] --output notes.txt.gpg notes.txt when you are encrypting to a key pair. Both commands take under a minute once GnuPG is installed.

GPG, short for GNU Privacy Guard, is the free open-source implementation of the OpenPGP standard. It reads a file, encrypts the contents with a random session key, wraps that session key either with your passphrase or with someone’s public key, and writes the result into a new .gpg file. The original file stays exactly where it was, so the ciphertext is a copy, not a move.

This guide targets GnuPG 2.x, which is what ships with current Linux distributions, macOS via Homebrew, Gpg4win and the Windows Subsystem for Linux. If a tutorial you found shows a key-type menu asking for DSA or 1024-bit RSA, it was written for GnuPG 1.4 and you can safely ignore that part.

Table of Contents

What You Need

You need three things: the GnuPG command-line tool, either a passphrase or a key pair, and one test file that holds nothing sensitive. Practice on hello.txt first, not on your tax return.

On Debian and Ubuntu, install it with sudo apt install gnupg. On Fedora, sudo dnf install gnupg2. On Arch, sudo pacman -S gnupg. macOS ships GnuPG in recent versions, and brew install gnupg gives you a current one either way.

Windows users have two routes. Gpg4win bundles GnuPG with Kleopatra, a graphical front end, plus the same gpg.exe the rest of this guide uses. The Windows Subsystem for Linux gives you a real Linux environment where these commands work verbatim. Either one is fine; Kleopatra is friendlier if you never want to touch a terminal.

One more prerequisite that gets skipped: your home directory needs a .gnupg folder with permissions set to 700, owned by you. GnuPG creates it on first run and refuses to start if someone else can read it.

Step-by-Step

Step 1: Check GPG and List Existing Keys

Confirm the tool is present and see which keys already live in your keyring:

gpg --version
gpg --list-keys
gpg --list-secret-keys
gpg --fingerprint

gpg --version prints the release, for example 2.4.5, along with the cipher and digest algorithms compiled in. gpg --list-keys shows the public keys you hold, and gpg --list-secret-keys shows the private keys sitting in your private keyring. If both come back empty, you have no key pair yet and Step 2 fixes that.

The value you pass to --recipient comes from this list. It can be a key ID such as 3F2A9C41D8B7E650, a fingerprint, or the email address and name on the user ID. Long key IDs are the safer choice because an 8-character short ID can collide.

Step 2: Generate or Choose a GPG Key Pair

A key pair beats a passphrase alone whenever a second person needs to read the file, because you never have to hand them a shared secret. It also gives you signing, so they can confirm the file really came from you and was not modified in transit.

Generate one non-interactively:

gpg --quick-generate-key "Your Name" [email protected] rsa4096 sign,cert 2y

The last two arguments pick the algorithm and the lifetime. rsa4096 is a safe, boring default that everything understands. GnuPG 2.2 and newer will happily create an ed25519 key instead with ed25519 sign,cert 2y, which is faster and equally strong; both are fine, and compatibility with other people’s tooling is the only reason to prefer RSA.

A two-year expiry is deliberate. Keys that never expire are keys you will still be using in a decade, and a passphrase protects a lost laptop badly. GnuPG will warn you before expiry and you can extend it later.

When the prompt appears, set a passphrase on the private key. Four random words beat a short clever sentence every time. What is public and what stays private comes down to one line: share the public key freely, and never send your private key or passphrase to anyone, for any reason.

Step 3: How to Use GPG to Encrypt a File for a Recipient

Encrypting to your own key, or to someone who already has your public key imported:

gpg --encrypt --recipient [email protected] --output notes.txt.gpg notes.txt

The --output flag is worth using every time. Without it GPG appends .gpg to the input name, which is usually fine but surprises people who already have a file by that name.

If you would rather set a passphrase than manage keys, symmetric mode does the whole job with no keyring involved:

gpg --symmetric --cipher-algo AES256 --output notes.txt.gpg notes.txt

One warning that appears in plenty of top-ranking answers: never pass --passphrase on the command line. It works, it leaks into your shell history and into the process list where any local user can read it, and no flag makes that safe. Type the passphrase at the prompt instead.

Binary output is normal and expected. Opening notes.txt.gpg in a text editor shows unreadable bytes, which is not a sign that encryption failed. When you need something you can paste into an email or a web form, add --armor:

gpg --encrypt --recipient [email protected] --armor --output notes.txt.asc notes.txt

Armored output is Base64 wrapped in a BEGIN PGP MESSAGE header. It is a third larger than binary, and every tool reads it. For a whole folder, tar first and pipe it in:

tar -czf - project | gpg --encrypt --recipient [email protected] --output project.tar.gz.gpg

Looping gpg over a directory instead gives you a separate unreadable .gpg per file, which is almost never what you wanted.

Step 4: Verify and Transfer the Encrypted File

Check the output before you move it anywhere:

ls -lh notes.txt.gpg
file notes.txt.gpg
head -c 64 notes.txt.gpg | xxd
sha256sum notes.txt.gpg

file reports OpenPGP encrypted data, which confirms you are not looking at a renamed plaintext file. A size close to the original, or larger, is expected because of the encryption overhead and the integrity packet.

Record the checksum if the file is travelling somewhere unreliable, then compare it after the transfer. Over plain HTTP or through a chat app that recompresses attachments, silent corruption shows up here first rather than after you need the data.

Move only the .gpg file, and send the public key separately through a channel the recipient already trusts. Never upload the plaintext original to a service you do not control just because the ciphertext upload felt slow.

Step 5: Decrypt the File With Your Private Key

On the receiving machine, import the sender’s public key if you have not already, then decrypt:

gpg --import sender-public-key.asc
gpg --decrypt --output notes.txt notes.txt.gpg

GPG reads the recipient key ID out of the file header, finds the matching secret key in your private keyring, unwraps the session key and writes the plaintext. The passphrase prompt you see comes from gpg-agent, the helper process that caches your unlocked key for a few minutes. It reappearing on the next file is normal, not a sign of a broken file.

Without --output, GPG writes the recovered content to standard output, which means your terminal fills with binary data and the OK and Decryption OK messages are buried in it. Add the flag or redirect it yourself.

On a headless box, a container or inside CI, there is no pinentry window to type into. That is the gpg-agent and pinentry failure people hit, and the fix is --pinentry-mode loopback with --passphrase-file pointing at a file only you can read, or an agent configured for loopback. Never solve it by putting the passphrase into the command.

Step 6: Verify the Decrypted File

Compare the recovered file against the original when you still have both:

sha256sum notes.txt
cmp original.txt notes.txt && echo identical

A zero exit status from gpg --decrypt tells you the decryption succeeded and the integrity packet matched. It does not tell you the recipient was the person you expected, because anyone can encrypt to your public key. That check comes from a signature:

gpg --verify notes.txt.gpg
gpg --verify notes.txt.asc notes.txt.sig

The first form works when the file was signed and encrypted in one pass with --sign --encrypt. The second reads a detached signature. If you have never verified the signer’s key fingerprint over a phone call or another channel you already trust, GPG will report the signature as good but the key as untrusted, and that warning is worth pausing on.

Common Mistakes

gpg: command not found. The tool is not installed, or it is installed outside your PATH. Run which gpg and fix your PATH, or install the package for your distribution.

No public key for the recipient. GPG cannot find a key matching the name or ID you gave. Run gpg --list-keys and copy the exact user ID from the output, then import the missing key with gpg --import.

Decryption failed: No secret key. You hold the sender’s public key but not the private half. Whoever encrypted for you must re-send it encrypted to your own key.

Decryption failed: Bad session key, or a terminal full of mojibake. Almost always a wrong passphrase or a truncated file. Check the file size against what the sender reports, then try again with the agent cache cleared using gpgconf --kill gpg-agent.

The .gpg file looks like garbage in an editor. Binary output, by design. Use --armor next time if you need readable output.

Permission denied on ~/.gnupg. Another user owns the folder or it is group-readable. Fix it with chmod 700 ~/.gnupg and chown $USER ~/.gnupg.

pinentry: No such file or directory in headless environments. Expected, and covered in Step 5.

Two more that catch people out: signing is not encryption, and encrypting is not signing. --sign alone proves who wrote a file but leaves it readable, while --encrypt alone hides the contents but tells the reader nothing about the sender. Use both flags together when you need both properties.

Operationally, back up your private key and confirm you can restore it. Losing the private key means losing the data, since there is no recovery path. Generate a revocation certificate when you create the key so you can retire it properly if a laptop walks away, and verify fingerprints out of band before trusting a new contact’s key. Delete plaintext copies from disk rather than trusting a move to the recycle bin, and keep passphrases out of your shell history.

Frequently Asked Questions

Can I use GPG to encrypt a file with just a password?

Yes. Symmetric mode wraps a random session key with a passphrase instead of a public key, so no key pair is needed. Run gpg –symmetric –cipher-algo AES256 –output file.gpg file, then decrypt with gpg –decrypt –output file file.gpg and type the same passphrase. The key is the passphrase itself, so anyone who has it can read the file, which makes it best for your own backups rather than sharing.

Should I use armored or binary GPG output?

Binary is the default and is slightly smaller, which matters for large files or attachments. Armored output, made with –armor, is Base64 text wrapped in BEGIN PGP MESSAGE and PGP END markers, roughly a third larger. Every tool reads both, so choose armored when a human has to move the file through email or a web form, and binary for scripts, automated pipelines and large archives.

How do I encrypt multiple files or a directory with GPG?

Pipe a tar stream straight into GPG so it reads from standard input and writes one ciphertext: tar -czf – myfolder | gpg –encrypt –recipient [email protected] –output myfolder.tar.gz.gpg. On the other side, reverse it with gpg –decrypt –output myfolder.tar.gz myfolder.tar.gz.gpg, then tar -xzf myfolder.tar.gz. Running GPG over a folder directly only produces one unreadable file per item and loses directory structure.

How do I decrypt a GPG file if I do not know its original name?

Use –output and pick any filename you like: gpg –decrypt –output recovered.txt mystery.gpg. GPG does not store the original filename inside the ciphertext, so without –output it writes the plaintext to standard output and your terminal fills with binary data. Choose a name that matches the type of data, and write it somewhere the file will not be mistaken for something already decrypted.

How do I send an encrypted file to someone safely?

Encrypt to their public key, not to a passphrase you exchange separately. Send the ciphertext as an attachment or in cloud storage, and send the public key through a different channel you already trust, such as an in-person meeting or an existing verified conversation. Confirm their key fingerprint through a second channel before encrypting, and if authenticity matters, have them sign a test file so you can run gpg –verify.

Does GPG encryption hide the original filename?

No. The filename is not part of the plaintext and GPG does not encrypt it by default. A binary file also has no header, but ASCII-armored output carries a filename comment that can reveal the original name to anyone who sees the file. If that matters, strip it with gpg –decrypt piped through sed, or avoid armor and move the file in an encrypted container.

Conclusion

Start with one harmless file. Generate a key pair, encrypt that file to a key you control, decrypt it back, and compare the two copies with sha256sum so you have seen the full round trip work once. Everything after that, key exchange, folders, signing, batch runs, is the same loop with a different flag.

Keep the private key and its passphrase somewhere other than the machine holding the ciphertext, because that pairing defeats the whole exercise. The commands above run unchanged on Linux, macOS, in WSL and through gpg.exe from Gpg4win, where Kleopatra offers the same operations through a window if you would rather not type them.

Leave a Comment