Skip to main content

Security and hardening · Intermediate · 22 min

Encrypt secrets in a Git repository with SOPS and age

By Padding · Updated 18 Nov 2026
Ubuntu 26.04 · SOPS 3.13.3 · age 1.3.2

You will commit encrypted YAML and .env files that only named people and servers can open, decrypt them at deploy time into a private file, and add, remove and rotate keys with SOPS and age.

On this page

Keeping secrets out of Git, as in keep-secrets-out-of-git, works until a team needs to share them. Then the database password travels by chat message, lives in three laptops' .env files and drifts out of date. SOPS offers a different deal: the secrets file is committed, but every value in it is encrypted, so the repository shows which settings exist and who changed them while only listed people and servers can read the values.

SOPS (Secrets OPerationS) does the file handling. It encrypts each value with one random data key, then encrypts that data key separately for every recipient. age is the encryption tool and key format SOPS uses here: a recipient is a short public key starting age1, and the matching private key is one line in a text file. There is no key server and no cloud account. Encrypting and decrypting happen on your machine, offline, which also suits teams on prepaid data or without a cloud key management service.

You will build a small project with an encrypted YAML file and an encrypted .env file, give a server access, decrypt at deploy time, add and remove a teammate, and rotate the data key. Every value in the examples is a made-up demo value.

Step 1 · Install age and SOPS from their releases

Neither tool comes from the Ubuntu archive at these versions, so you download the official release builds and check them before installing. First the tools you need for that:

Bash
sudo apt update
sudo apt install -y curl git

Download SOPS and its checksums file into a fresh temporary folder. dpkg --print-architecture prints amd64 or arm64, which matches the release file names for both tools.

Bash
SOPS_VERSION=3.13.3
AGE_VERSION=1.3.2
ARCH=$(dpkg --print-architecture)
cd "$(mktemp -d)"
curl -fsSLO "https://github.com/getsops/sops/releases/download/v${SOPS_VERSION}/sops-v${SOPS_VERSION}.linux.${ARCH}"
curl -fsSLO "https://github.com/getsops/sops/releases/download/v${SOPS_VERSION}/sops-v${SOPS_VERSION}.checksums.txt"
sha256sum --ignore-missing -c "sops-v${SOPS_VERSION}.checksums.txt"

The last line must read sops-v3.13.3.linux.amd64: OK (or arm64). age does not publish a checksums file; its GitHub release page lists a SHA-256 digest next to each download instead. The two lines below are the digests for the 1.3.2 Linux builds, copied from that page:

Bash
curl -fsSLO "https://github.com/FiloSottile/age/releases/download/v${AGE_VERSION}/age-v${AGE_VERSION}-linux-${ARCH}.tar.gz"
cat > age-sha256.txt <<'EOF'
cbe24006683f8eb669266162894b9a522a1af52f2665fbc63a4bb032ed26ac10  age-v1.3.2-linux-amd64.tar.gz
6b8dc4333c53a5a57c9e5834e3a48f92605d7154014cd07269ff3327db5d37f4  age-v1.3.2-linux-arm64.tar.gz
EOF
sha256sum --ignore-missing -c age-sha256.txt

Again you want OK. If either check prints FAILED, delete the folder and download again; do not install a file that fails. Then unpack age and install the three programs:

Bash
tar -xzf "age-v${AGE_VERSION}-linux-${ARCH}.tar.gz"
sudo install -m 0755 age/age age/age-keygen /usr/local/bin/
sudo install -m 0755 "sops-v${SOPS_VERSION}.linux.${ARCH}" /usr/local/bin/sops

Check it worked

Bash
sops --version --disable-version-check
age --version

The output is sops 3.13.3 and v1.3.2. Without --disable-version-check, sops --version also asks GitHub whether a newer release exists.

Upgrading later

Programs in /usr/local/bin get no apt security updates. Watch the two projects' release pages (GitHub lets you watch releases only), and to upgrade, repeat this step with the new version numbers. For SOPS the checksums file covers the new version automatically; for age, replace the two digest lines with the ones shown on the new release page. Installing over the old binaries is safe; your keys and encrypted files do not change.

Step 2 · Create a key for yourself and one per server

Each person and each server gets its own age key. Sharing one key between people defeats the point: you could not remove one person without changing it for everyone.

SOPS looks for your private keys in ~/.config/sops/age/keys.txt on Linux. Create the folder private first, then generate the key, but only if you do not have one already:

Bash
install -d -m 700 ~/.config/sops/age
if [ -e ~/.config/sops/age/keys.txt ]; then
  echo "Keeping your existing key file"
else
  age-keygen -o ~/.config/sops/age/keys.txt
fi
age-keygen -y ~/.config/sops/age/keys.txt

age-keygen prints Public key: age1... and writes the file with mode 600. It refuses to overwrite an existing file, with file exists, which protects a key you already rely on. age-keygen -y reads the private key and prints its public half, the recipient. The public key is safe to share and commit; the AGE-SECRET-KEY- line in the file is not. Back that file up somewhere safe, such as a password manager: if every recipient loses their key, nobody can decrypt the files again.

On macOS the default location is ~/Library/Application Support/sops/age/keys.txt instead. Set XDG_CONFIG_HOME=$HOME/.config in your shell profile if you want the Linux path there too.

Your server and a teammate need keys of their own. Each would run age-keygen on their own machine and send you only the age1... line. To follow along on one machine, create stand-ins for both in a demo folder. Each stand-in folder mirrors the sops/age/keys.txt layout, so you can point SOPS at it later and see exactly what that machine would see:

Bash
install -d -m 700 ~/sops-demo-keys/server/sops/age ~/sops-demo-keys/teammate/sops/age
age-keygen -o ~/sops-demo-keys/server/sops/age/keys.txt
age-keygen -o ~/sops-demo-keys/teammate/sops/age/keys.txt

Keep the three public keys in shell variables for the next steps:

Bash
ME=$(age-keygen -y ~/.config/sops/age/keys.txt)
SERVER=$(age-keygen -y ~/sops-demo-keys/server/sops/age/keys.txt)
TEAMMATE=$(age-keygen -y ~/sops-demo-keys/teammate/sops/age/keys.txt)

Check it worked

Bash
printf 'me:       %s\nserver:   %s\nteammate: %s\n' "$ME" "$SERVER" "$TEAMMATE"

Three different lines, each starting age1. If you open a new terminal later, run the three ME=, SERVER= and TEAMMATE= lines again.

Step 3 · Write the creation rules in .sops.yaml

SOPS reads .sops.yaml in the repository to decide, for each file path, who the recipients are and what to encrypt. Commit this file: it holds only public keys, and it means nobody has to remember the right recipients when they add a secret.

Bash
mkdir -p ~/sops-demo/secrets
cd ~/sops-demo
git init
cat > .sops.yaml <<EOF
# Recipients are public age keys, safe to commit.
#   me:     $ME
#   server: $SERVER
creation_rules:
  - path_regex: secrets/.*\.yaml$
    encrypted_regex: ^(password|api_key)$
    age: $ME,$SERVER
  - path_regex: secrets/.*\.env$
    age: $ME,$SERVER
EOF

Each rule has three parts:

  • path_regex is a regular expression matched against the file's path. SOPS uses the first rule that matches.
  • age is a comma-separated list of recipients. Both rules list you and the server.
  • encrypted_regex limits encryption to keys whose names match. Here only password and api_key values are encrypted in YAML files, so hosts and ports stay readable in reviews. Without it, as in the .env rule, SOPS encrypts every value. Either way the key names themselves stay in plain text.

The heredoc has no quotes around EOF, so the shell fills in the three variables. Nothing else in it starts with $ followed by a name, so the regular expressions arrive unchanged.

Check it worked

Bash
cat .sops.yaml
grep -c 'age1' .sops.yaml

The file shows your two real public keys in place of the variable names, and the count is 4: the two comment lines and the two age: lines, each holding both keys.

Step 4 · Encrypt a YAML file and an .env file

Write the two plaintext files. A file that holds secrets is created with mode 600 first and only then filled, so it is never readable by other users, even for a moment:

Bash
install -m 600 /dev/null secrets/app.yaml
cat > secrets/app.yaml <<'EOF'
database:
  host: db.internal
  port: 5432
  user: shop
  password: demo-db-pass-1
payments:
  api_key: demo-pay-key-1
EOF
install -m 600 /dev/null secrets/app.env
cat > secrets/app.env <<'EOF'
DATABASE_PASSWORD=demo-db-pass-1
MAIL_API_TOKEN=demo-mail-token-1
EOF

Now encrypt both in place. SOPS picks the format from the extension, .yaml or .env, and the recipients from the matching rule:

Bash
sops encrypt -i secrets/app.yaml
sops encrypt -i secrets/app.env

For a new secrets file in daily work, sops edit secrets/new.yaml on a path that does not exist yet opens your editor on a starter document and saves it encrypted, so no plaintext copy touches the disk.

Check it worked

Bash
cat secrets/app.yaml
grep -q 'port: 5432' secrets/app.yaml
grep -q 'password: ENC\[AES256_GCM' secrets/app.yaml
if grep -q 'demo-db-pass-1' secrets/app.yaml secrets/app.env; then echo "Plaintext still present"; false; fi

The YAML keeps host: db.internal and port: 5432 readable, while password and api_key now hold ENC[AES256_GCM,data:...]. SOPS rewrites the file with four-space indentation and adds a sops: section at the end: one enc block per recipient (the data key, encrypted for that age key), a mac that detects tampering, and the SOPS version. In secrets/app.env, both values are ENC[...] and the same metadata sits in sops_...= lines. The last command prints nothing, because neither file contains the demo password any more.

Decrypt to your terminal to see that your key opens both files:

Bash
sops decrypt secrets/app.yaml
sops decrypt --extract '["database"]["password"]' secrets/app.yaml; echo

The first prints the original YAML. --extract pulls out one value, demo-db-pass-1, without a trailing newline, hence the echo. Do not print real secrets while sharing your screen.

Step 5 · Commit the files and edit them in place

The encrypted files and .sops.yaml are safe to commit:

Bash
git add .sops.yaml secrets/app.yaml secrets/app.env
git commit -m "Add encrypted secrets"

To change a secret, use sops edit. It decrypts into a temporary file, opens the editor named in $EDITOR (set export EDITOR=nano in your profile if you prefer it to vim), and re-encrypts when you save and quit. Change demo-db-pass-1 to demo-db-pass-2 under database::

Bash
sops edit secrets/app.yaml

A plain git diff of the result is a wall of changed ciphertext. Git can show the decrypted difference instead: tell it to run sops decrypt on files under secrets/ before comparing. The .gitattributes line is committed; the git config line goes in each clone's own settings, and only works for people whose key can decrypt the files.

Bash
printf 'secrets/** diff=sopsdiffer\n' > .gitattributes
git config diff.sopsdiffer.textconv "sops decrypt"
git diff -- secrets/app.yaml

Check it worked

Bash
git diff -- secrets/app.yaml | grep -- '+    password: demo-db-pass-2'
git add .gitattributes secrets/app.yaml
git commit -m "Change database password"

The diff shows one line removed, password: demo-db-pass-1, and one added, password: demo-db-pass-2, with the other settings as context. The commit records only ciphertext.

Step 6 · Decrypt on the server at deploy time

On the server, the deploy pulls the repository and decrypts with the server's own key into a file only the app's user can read. Most apps and systemd's EnvironmentFile= read a NAME=value file, which is what sops decrypt prints for an .env file.

The stand-in server key lives under ~/sops-demo-keys/server. Setting XDG_CONFIG_HOME to that folder makes SOPS look for sops/age/keys.txt there instead of in your ~/.config, so the command sees only the server's key. On the server itself the key sits in the deploy user's own ~/.config/sops/age/keys.txt and you drop the XDG_CONFIG_HOME=... prefix.

Bash
install -d -m 700 ~/sops-demo-deploy
install -m 600 /dev/null ~/sops-demo-deploy/app.env
XDG_CONFIG_HOME=~/sops-demo-keys/server sops decrypt secrets/app.env > ~/sops-demo-deploy/app.env

The target file is created with mode 600 before the decrypted text is written into it, the same rule as for the plaintext files earlier. If you would rather not write a decrypted file at all, sops exec-env decrypts into the environment of a single command:

Bash
XDG_CONFIG_HOME=~/sops-demo-keys/server sops exec-env secrets/app.env 'test -n "$DATABASE_PASSWORD" && echo "DATABASE_PASSWORD is set"'

It prints DATABASE_PASSWORD is set. In a deploy script, the quoted command would start your app instead.

Check it worked

Bash
stat -c '%a %n' ~/sops-demo-deploy/app.env
cut -d= -f1 ~/sops-demo-deploy/app.env
grep -q '^DATABASE_PASSWORD=demo-db-pass-1$' ~/sops-demo-deploy/app.env

stat prints 600 and the path, and cut lists only the names, DATABASE_PASSWORD and MAIL_API_TOKEN, so the check does not print the values. Now try the teammate's key, which is not a recipient yet:

Bash
if XDG_CONFIG_HOME=~/sops-demo-keys/teammate sops decrypt secrets/app.env; then
  echo "Teammate can read the file"; false
else
  echo "Teammate has no access yet"
fi

SOPS prints Failed to get the data key required to decrypt the SOPS file. followed by one FAILED entry per recipient, and the last line is Teammate has no access yet.

Step 7 · Add a teammate with sops updatekeys

To give someone access, add their public key to .sops.yaml, then let SOPS encrypt the existing data key for them. Rewrite the file with the teammate added to both rules:

Bash
cat > .sops.yaml <<EOF
# Recipients are public age keys, safe to commit.
#   me:       $ME
#   server:   $SERVER
#   teammate: $TEAMMATE
creation_rules:
  - path_regex: secrets/.*\.yaml$
    encrypted_regex: ^(password|api_key)$
    age: $ME,$SERVER,$TEAMMATE
  - path_regex: secrets/.*\.env$
    age: $ME,$SERVER,$TEAMMATE
EOF
sops updatekeys -y secrets/app.yaml secrets/app.env

For each file, updatekeys lists the recipient changes, with +++ before the teammate's key, then reports that the file was synced with new keys. Without -y it asks before changing each file, which is what you want when you run it by hand. You need to be a current recipient to run it, because SOPS must decrypt the data key to re-encrypt it for the newcomer. The secret values are not re-encrypted; only a new enc block is added.

Check it worked

Bash
grep -c 'recipient:' secrets/app.yaml
XDG_CONFIG_HOME=~/sops-demo-keys/teammate sops decrypt secrets/app.env
git add .sops.yaml secrets/app.yaml secrets/app.env
git commit -m "Give teammate access to secrets"

The count is 3, and the teammate's key now prints both NAME=value lines.

Step 8 · Remove the teammate and rotate the data key

When someone leaves, removing their key is not enough on its own. updatekeys keeps the same data key, and anyone who could decrypt the file could have saved that data key. So do it in this order: remove the recipient, then rotate the data key, so the new key is never encrypted for the person you removed.

Bash
cat > .sops.yaml <<EOF
# Recipients are public age keys, safe to commit.
#   me:     $ME
#   server: $SERVER
creation_rules:
  - path_regex: secrets/.*\.yaml$
    encrypted_regex: ^(password|api_key)$
    age: $ME,$SERVER
  - path_regex: secrets/.*\.env$
    age: $ME,$SERVER
EOF
sops updatekeys -y secrets/app.yaml secrets/app.env
sops rotate -i secrets/app.yaml
sops rotate -i secrets/app.env

updatekeys now shows --- before the teammate's key. sops rotate -i generates a fresh data key, re-encrypts every value with it and writes the file back in place.

Check it worked

Bash
if XDG_CONFIG_HOME=~/sops-demo-keys/teammate sops decrypt secrets/app.env; then
  echo "Teammate can still read the file"; false
else
  echo "Teammate access removed"
fi
XDG_CONFIG_HOME=~/sops-demo-keys/server sops decrypt secrets/app.env
git add .sops.yaml secrets/app.yaml secrets/app.env
git commit -m "Remove teammate and rotate data key"

The teammate gets the Failed to get the data key message and the line Teammate access removed, while the server key still prints both values.

Step 9 · Check what Git history still holds, then clean up

Rotation protects future versions of the file. The commits from when the teammate was a recipient are still in the repository, encrypted for their key, and they have a clone. Show it by reading the previous commit's file with the teammate's key:

Bash
install -m 600 /dev/null ~/sops-demo-keys/old-app.env
git show HEAD~1:secrets/app.env > ~/sops-demo-keys/old-app.env
XDG_CONFIG_HOME=~/sops-demo-keys/teammate sops decrypt ~/sops-demo-keys/old-app.env

The teammate's key prints DATABASE_PASSWORD=demo-db-pass-1 again. Rewriting Git history does not change this, because their clone already has the old commits. The only real fix is to change the secrets themselves at their source: set a new database password, issue a new mail API token, then store the new values with sops edit. Rotate a leaked secret without downtime shows how to do that without breaking the running app. Treat every secret a departing person could read as one to rotate, starting with the most powerful.

Check it worked

Bash
git log --oneline

Four commits, newest first: removing the teammate, giving them access, changing the password and adding the secrets. Each is a point in history a recipient of that time can still decrypt.

Clean up

Delete the demo project, the stand-in keys and the deployed file:

Bash
cd ~
rm -rf ~/sops-demo ~/sops-demo-keys ~/sops-demo-deploy

Keep your own key in ~/.config/sops/age/keys.txt if you will use SOPS for real projects. If you created it only for this tutorial and no file you care about is encrypted to it, remove it, and the programs if you no longer want them:

Bash
rm ~/.config/sops/age/keys.txt
sudo rm /usr/local/bin/sops /usr/local/bin/age /usr/local/bin/age-keygen

Next steps

Need this done for you?

Padding does this work for growing companies, from the first diagnosis to a system that keeps working.

Platform Engineering

Get new tutorials

New tutorials, about twice a month.