Advanced
Searching inHow-to guides
How to search
  • Type the task in your own words (find port, open firewall, disk full) or paste an error message in quotes.
  • Choose "Quick answer" to see only the commands, or an area to stay in one field.
  • "two words" finds the exact phrase.
  • Plain words find names, texts and examples.
  • ↑ ↓ to move · Enter to open · Esc to close · / to search
Try:

How-to 83Security · Secrets

How to encrypt a file with GPG or age

A database dump to a partner, a backup in the cloud, a .env file to a colleague — files with sensitive data should travel and rest encrypted. age is the modern, simple tool (one key, one command); GPG is everywhere and works with existing company keys. This how-to encrypts for a person (public key) and with a passphrase, encrypts a whole folder, and decrypts again.

  • 5 Lessons
  • 8 Commands

$ run as a normal user# run as root (or with sudo)

83.1

The quick answer

age-keygen -o ~/.config/age/key.txt
Public key: age1examplepublickeyexamplepublickeyexamplepublickeyxx
age -r age1examplepublickeyexamplepublickeyexamplepublickeyxx -o dump.sql.age dump.sql
age -d -i ~/.config/age/key.txt -o dump.sql dump.sql.age
gpg -c --cipher-algo AES256 report.pdf
gpg -d report.pdf.gpg > report.pdf

-r = encrypt for a public key (only the owner of the private key can open it); gpg -c = with a passphrase.

83.2

Step by step

  1. Install: dnf install age gnupg2 (age from EPEL on RHEL) or apt-get install age gnupg.
  2. age with keys — each person makes a key pair once, and shares only the public key (age1…):
mkdir -p ~/.config/age && (umask 077; age-keygen -o ~/.config/age/key.txt)
age -r age1alice… -r age1bob… -o shop-db.sql.age shop-db.sql

Several -r = several people can open it. age can also use SSH keys: age -R ~/.ssh/id_ed25519.pub …, and open with age -d -i ~/.ssh/id_ed25519 ….

  1. age with a passphrase (no keys — for a one-time exchange):
age -p -o notes.txt.age notes.txt

Send the passphrase over another channel (phone, Signal), not in the same email.

  1. GPG with a person's public key (companies with GPG keys):
gpg --import alice.asc
gpg --list-keys alice@example.com
gpg --encrypt --recipient alice@example.com --output report.pdf.gpg report.pdf
gpg --decrypt --output report.pdf report.pdf.gpg
  1. A whole folder — pack and encrypt in one stream (no plain archive on disk):
tar -czf - ./exports | age -r age1alice… > exports.tar.gz.age
age -d -i ~/.config/age/key.txt exports.tar.gz.age | tar -xzf -
  1. Remove the plain file after you checked the encrypted one opens:
shred -u dump.sql

(On SSDs and copy-on-write file systems shred is not a guarantee — better never write the plain file at all, like in step 5.)

  1. Keys are everything: lose the private key or passphrase = lose the data. Keep a backup of the key in a safe place (password manager, offline). For secrets in Git use SOPS (built on age/GPG).

83.3

Check that it worked

file dump.sql.age
dump.sql.age: age encrypted file, X25519 recipient
age -d -i ~/.config/age/key.txt dump.sql.age | sha256sum
sha256sum dump.sql

The encrypted file can not be read without the key, and the decrypted data has the same checksum as the original.

83.4

Common problems

ProblemCauseFix
age: error: no identity matched any of the recipientsWrong key file for this file.Use the private key that matches the -r used for encryption.
GPG asks for the passphrase but no prompt appears (SSH)pinentry can not find the terminal.export GPG_TTY=$(tty).
gpg: … No public keyThe recipient's key is not imported.gpg --import KEY.asc; check the fingerprint with the person.
gpg: … There is no assurance this key belongs to the named userKey not trusted/signed.Verify the fingerprint, then sign or trust it.
age: command not found on RHELNot in the base repos.Enable EPEL.
Encrypted file can never be openedLost key or passphrase.No recovery — back up keys; encrypt for two recipients (you + a backup key).

83.5

Go further

1You must send a database dump to two colleagues with age. How?

age -r PUBKEY1 -r PUBKEY2 -o dump.sql.age dump.sql — each can open it with their own private key.

2What happens if you lose the age private key?

The files encrypted only for it can never be opened again.

All commands in this chapter