Skip to content

How to Encrypt Files with age on Linux

A practitioner guide to age encryption on Linux — encrypt a file to an X25519 public key, an SSH recipient, or a passphrase, then decrypt safely with the matching identity.

To encrypt a file with age you first generate a key pair with age-keygen -o key.txt, which prints a public key that starts with age1. You hand that public key to anyone who should be able to send you an encrypted file, and they run age -r age1... -o secrets.txt.age secrets.txt. Only the holder of the matching private key in key.txt can decrypt it, with age -d -i key.txt secrets.txt.age. If you only need a shared secret rather than a key pair, age -p encrypts to a passphrase instead. age is deliberately small: no configuration files, no key-ring daemon, one binary, a handful of flags. This guide covers all three recipient types — public key, SSH key, and passphrase — plus armored output, multi-recipient files, and how to verify a round trip actually worked.

What you’ll accomplish

  • Generate an age X25519 key pair and read your public key.
  • Encrypt a file to one or several public-key recipients, so any one of them can open it.
  • Encrypt to an existing SSH public key, and to a passphrase when you have no key pair.
  • Decrypt safely with the matching identity, and produce text-safe armored output for email or copy-paste.
  • Verify a round trip with diff, and confirm the ciphertext is unreadable without a key.

Prerequisites

  • A Linux or macOS host with a shell. age has no daemon and needs no root.
  • age v1.1 or newer, which bundles the age and age-keygen binaries. Check with age --version.

Install it from your package manager or the Go toolchain:

# Debian / Ubuntu
sudo apt install age

# macOS (Homebrew)
brew install age

# From source, if you have Go 1.21+
go install filippo.io/age/cmd/...@latest

Confirm the binary is on your PATH before continuing:

age --version
v1.2.1

Two encryption modes matter here. In public-key mode, age uses X25519 — a modern elliptic-curve key agreement. You hold a private key (an identity) and publish a matching public key (a recipient); anyone with the public key can encrypt to you, and only your private key decrypts. In passphrase mode there is no key pair at all: age derives the encryption key from a passphrase using scrypt, so anyone who knows the passphrase can decrypt. Public-key mode is the right default whenever the sender and recipient are different people or machines; passphrase mode suits a one-off file you will decrypt yourself.

How it works

age separates two ideas that GPG blurs together. A recipient is a public thing used only to encrypt: an age1... public key, an SSH public key, or a passphrase. An identity is the private counterpart used only to decrypt: the secret key in key.txt, an SSH private key, or that same passphrase. You encrypt to recipients and decrypt with identities.

A single age file can carry several recipients at once. When you pass -r more than once, age wraps the file’s random data key separately for each recipient, so any one of the matching identities can open the file — you do not need all of them. This is how you encrypt one backup that both you and a colleague can decrypt, each with your own key, without sharing a private key.

Output is binary by default, which is compact but not safe to paste into a chat or an email body. The -a (armor) flag wraps the ciphertext in a PEM-style ASCII block that survives copy-paste and text transports. Armored and binary files decrypt identically; the wrapper is the only difference.

age recipients and identities encryption flow

Step-by-step

1. Generate a key pair. age-keygen writes the private key to the file and prints the matching public key to standard error, so you see it immediately.

age-keygen -o key.txt
Public key: age1ql3z2n6nx0k0r4y8xqe5j7d2wq9m4hs4l0v3k2s9p0q7r6t5u4vshzk9mq

The file key.txt now holds your identity. Treat it like an SSH private key.

2. Lock down the key file. age warns loudly if the private key is world-readable, and refuses to trust group- or world-writable key files. Restrict it before you rely on it.

chmod 600 key.txt
# no output on success

3. Encrypt a file to your public key. Pass the recipient with -r and name the output with -o. The input file is left untouched.

age -r age1ql3z2n6nx0k0r4y8xqe5j7d2wq9m4hs4l0v3k2s9p0q7r6t5u4vshzk9mq -o secrets.txt.age secrets.txt
# no output on success; secrets.txt.age now exists

4. Encrypt to a passphrase instead. With -p, age prompts twice for a passphrase and derives the key from it — no key pair involved. This is interactive by design.

age -p -o notes.txt.age notes.txt
Enter passphrase (leave empty to autogenerate a secure one):
Confirm passphrase:

5. Encrypt to an SSH public key. age accepts an existing SSH public key as a recipient with -R, pointing at the .pub file. Anyone who can log in with that key can also decrypt the file, using the private half.

age -R ~/.ssh/id_ed25519.pub -o deploy.env.age deploy.env
# no output on success

6. Encrypt to several recipients at once. Repeat -r (and mix in -R) as many times as you need. Any single matching identity can then decrypt the file.

age -r age1ql3z2n6nx0k0r4y8xqe5j7d2wq9m4hs4l0v3k2s9p0q7r6t5u4vshzk9mq \
    -r age1lggyhqz7d8s9x0w2c4v6d8n0m2k4j6h8g0f2d4s6a8p0q2w4e6vsh3l7m0 \
    -R ~/.ssh/id_ed25519.pub \
    -o release.tar.gz.age release.tar.gz
# no output on success; any one of the three keys decrypts it

7. Decrypt with the matching identity. Pass your private key with -i and the ciphertext as the argument. Send the plaintext to a file with -o, or to standard output by omitting it.

age -d -i key.txt -o secrets.txt secrets.txt.age
# no output on success; secrets.txt is restored

To decrypt an SSH-recipient file, point -i at the private key instead: age -d -i ~/.ssh/id_ed25519 -o deploy.env deploy.env.age. To decrypt a passphrase file, run age -d with no identity and age will prompt for the passphrase.

8. Produce armored (ASCII) output. Add -a at encryption time for a text-safe file you can paste into a ticket or email.

age -a -r age1ql3z2n6nx0k0r4y8xqe5j7d2wq9m4hs4l0v3k2s9p0q7r6t5u4vshzk9mq -o message.age message.txt
-----BEGIN AGE ENCRYPTED FILE-----
YWdlLWVuY3J5cHRpb24ub3JnL3YxCi0+IFgyNTUxOSA...
-----END AGE ENCRYPTED FILE-----

Validate it works

Trust the round trip, not the exit code. Encrypt a known file, decrypt it back to a new name, and compare the two byte for byte.

age -r age1ql3z2n6nx0k0r4y8xqe5j7d2wq9m4hs4l0v3k2s9p0q7r6t5u4vshzk9mq -o rt.age original.txt
age -d -i key.txt -o roundtrip.txt rt.age
diff original.txt roundtrip.txt && echo "round trip OK"
round trip OK

An empty diff output followed by round trip OK means the decrypted file is identical to the original. Now confirm the ciphertext is genuinely unreadable — grep the encrypted file for a string you know is in the plaintext:

grep "password" rt.age
# no match; grep exits non-zero because the plaintext is not present

Finally, prove that the wrong key cannot open the file. Decrypting with an unrelated identity must fail rather than return garbage:

age -d -i other-key.txt rt.age
age: error: no identity matched any of the recipients

Troubleshooting

“no identity matched any of the recipients.” The private key you passed with -i does not correspond to any recipient the file was encrypted to. Confirm you are using the right key.txt, and remember that a file encrypted only to an SSH key needs the SSH private key, not an age key.txt. If you encrypted to several recipients, you only need one matching identity — but it has to be one of them.

age refuses to read key.txt, or warns it is unprotected. age rejects a private key file that is group- or world-writable, and warns when it is world-readable. Run chmod 600 key.txt. If the key lives on a shared host, also check that its parent directory is not writable by others.

Piping a passphrase does not work the way you expect. age reads the passphrase from the terminal, not standard input, so echo pass | age -p does not feed the prompt. For non-interactive passphrase decryption, redirect a file to a specific file descriptor rather than piping — for example provide the passphrase through a file with restrictive permissions and read it back, or better, use public-key mode for anything scripted. Interactive -p is meant for a human at a keyboard.

An SSH key with its own passphrase prompts during decryption. If your SSH private key is itself encrypted, age has to decrypt that key first and will prompt for its passphrase. Load the key into ssh-agent beforehand (ssh-add ~/.ssh/id_ed25519) and age will use the agent, so you are not prompted per file.

“failed to parse header” or a binary file that will not open in a text tool. You are looking at binary age output in a tool that expects text, or trying to decrypt an armored block that was truncated on copy-paste. Re-encrypt with -a if you need to move the file through text channels, and when you copy an armored block make sure you include both the BEGIN and END lines intact.

When this is the wrong approach

age is a file-encryption tool, and it stays in that lane on purpose. It is not a PKI: there is no certificate authority, no revocation, no web of trust, no key expiry. You distribute public keys yourself, out of band, and you trust them because you know where they came from. If you need identity binding, signatures, or revocation, that is GPG or an X.509 setup, not age.

age also does not sign. It gives you confidentiality, not authenticity — a decrypted file tells you it was encrypted to your key, not who encrypted it. When you need to prove authorship or integrity of a public artifact, reach for a signing tool such as minisign or GPG detached signatures alongside age.

Finally, age encrypts discrete files. For a continuously changing dataset, a live database, or a whole volume, per-file encryption is the wrong shape — use disk or filesystem encryption (LUKS, dm-crypt) so the data is protected at rest without an encrypt step per change. For encrypted, deduplicated backups of a directory tree, a backup tool is a better fit than raw age: see restic encrypted backups. And for secrets that live inside a Git repository next to the code that uses them, Ansible Vault is built for that workflow. The official documentation lives at github.com/FiloSottile/age and age-encryption.org.

If this is the kind of work you’d rather never own again, it’s the daily job of Managed Cloud Ops — book an infrastructure review.