GnuPG from Zero: Generate a Key, Encrypt and Decrypt, Sign and Verify — in One Pass
GPG is the standard tool for locking files and messages: encryption uses the other side's public key, signing uses your own private key. This guide starts from zero: generate an ed25519 master key plus a cv25519 encryption subkey, encrypt and decrypt to yourself, sign and verify with a tamper test, export the public key. Every step was run for real on GnuPG 2.4.4 — including the first failed key generation, recorded as it happened.

GnuPG (GPG for short) is the standard tool for "putting a lock on files and messages", and it does only two things: encryption and signing. Encryption makes your content unreadable to everyone except the recipient; signing lets others be sure "this really came from you, and nobody changed it after you sent it". Both rely on the same mechanism: asymmetric keys — one public key, one private key. The public key can be handed to anyone; the private key stays with you alone. Once you understand how the two keys divide the work, every GPG command makes sense. This guide starts from zero: generate a key, encrypt and decrypt, sign and verify — every step run for real in a live environment, with the output in the test record at the end. The environment here is GnuPG 2.4.4, and the demonstration uses a throwaway demo key that is deleted when we are done.
First, pin down the core idea. Encryption uses the recipient's public key: you want to send a message to Ming, so you lock it with Ming's public key, and only Ming's private key in the whole world can open it. Signing uses your own private key: you send a message and sign it with your private key, and anyone can check it with your public key and confirm "this is what you said, and nobody changed it in between". So the public and private keys flow in opposite directions: for encryption the public key belongs to the recipient, for signing the private key belongs to the sender. Remember that correspondence and none of the commands that follow will confuse you.
Why two keys at all? Why not just use a password? The problem with a password is distribution: you and Ming agree on a password, but how does the password itself get to Ming safely? A phone call can be listened to, a message can be intercepted, and handing over a note in person does not scale. Asymmetric encryption solves exactly this: the public key is public, so posting it on the internet costs nothing, because it can only lock, never unlock; the private key never leaves your computer. Two people communicating for the first time need no prior shared secret to establish an encrypted channel. That is the basic design GPG has carried over from the PGP era since 1991, and it is why it became the internet's encryption standard.
1. Generate your first key: the master key signs, a subkey encrypts
The first step in generating a key is giving GPG its own working directory. In our test we set GNUPGHOME to a temporary directory, /tmp/demo276-gpg-home, with permissions set to 700 — readable only by you. That directory holds your private key, and GPG will warn you outright if the permissions are wrong. In production that directory is your ~/.gnupg; for a demo, a temporary directory works, and deleting the whole thing afterwards leaves no trace.
Keys are generated with gpg --batch --gen-key and a parameter file. Ours said: key type eddsa, curve ed25519, name Demo User 276, email demo276@example.invalid, no expiry, and no passphrase (%no-protection). The key that came out looks like this: master key ed25519/E728D835B239EDA0, fingerprint 0391642B26ADADD2F1A4DDECE728D835B239EDA0, created 30 November 2026, user ID [ultimate] Demo User 276. gpg --list-keys shows the public key, gpg --list-secret-keys shows the private key — the two always come as a pair, and if you lose the private key, the public key is only half useful.
Here is the trap beginners hit most often, and one this guide hit for real in testing: on the first attempt we generated only a master key, with no subkey, and encryption failed outright with "Unusable public key". The reason is that the ed25519 master key's usage flags are [SCA] — Sign, Certify, Authenticate — with no Encrypt. The ed25519 curve simply cannot encrypt; it can only sign. With the master key alone you can sign but not encrypt. The correct fix is to add an encryption subkey: subkey type ecdh, curve cv25519, usage flag [E]. After adding the subkey and regenerating, gpg --list-keys showed an extra line, sub cv25519/0FCD763ACACA9781 2026-11-30 [E], and encryption worked immediately. The lesson is practical: when the error says "Unusable public key", look at the key's usage flags first — very often the key is not broken, it simply does not have the encryption capability at all.
Two choices in the demo deserve a note. First, the demo key has no passphrase (%no-protection), so the script can run unattended; a real key must have one — the passphrase is the last lock on the private key, and if the key file is lost, anyone can use it without one. Second, the demo key never expires (Expire-Date: 0); a real key should have an expiry, say one or two years — expiry can be extended, but a leaked key expires on its own, which is free insurance.
2. Encrypt and decrypt: lock it so only the other side can open it
The encryption command is gpg --encrypt --recipient plus the recipient's email. In our test we sent a message to ourselves: first write a plain.txt containing "hello gpg demo 276", then gpg --batch --yes --encrypt --recipient demo276@example.invalid --armor --output plain.txt.asc plain.txt. The --armor flag means the output is ASCII text (starting with -----BEGIN PGP MESSAGE-----) rather than binary, so it can be pasted into an email or a chat window. The --batch --yes flags stop GPG from pausing to ask questions mid-way, which a script needs.
Decryption is the reverse: gpg --batch --yes --decrypt --output plain.dec.txt plain.txt.asc, and GPG opens the file with your own private key. In our test, decryption exited with code 0, and then diff plain.txt plain.dec.txt also exited with code 0 — the two files are byte-for-byte identical, so nothing was lost on the round trip. That diff exit code of 0 is the simplest possible verification: identical before encryption and after decryption means the flow works.
Encrypting to yourself looks pointless — why send anything to yourself? But its value is verification: it proves "this key can encrypt and can decrypt", ruling out problems with the key itself. In a real scenario, encryption is for someone else: you get their public key, import it into your keyring, and name them with --recipient. They receive it and decrypt with their own private key. A public key can be posted anywhere; a private key must never leave your computer — the first commandment of the GPG world.
3. Sign and verify: prove "you sent it" and "nobody changed it"
Signing and encrypting are different jobs, and the commands look different too. Sign with gpg --detach-sign --armor --output plain.txt.sig plain.txt, which creates a .sig signature file separate from the original. Verify with gpg --verify plain.txt.sig plain.txt. In our test, the first verification printed: Signature made Mon Nov 30 11:55:45 2026 UTC, using EDDSA key 0391642B26ADADD2F1A4DDECE728D835B239EDA0, Good signature from "Demo User 276". Those three lines tell you when the signature was made, which key made it, and whether it is valid. "Good signature" is what a pass looks like.
The real value of a signature only shows when a file has been changed. In our test we appended a line, "tampered", to plain.txt and ran gpg --verify again; the output became BAD signature, with exit code 1. Note the exit code: 0 for Good, 1 for BAD. Automated pipelines judge whether a file was altered by that exit code — that is how many software release processes verify packages: download the software and its signature, run gpg --verify, continue installing on exit code 0, stop on exit code 1. What the mechanism guarantees is worth stating precisely: a signature does not guarantee the content is correct, only that it is exactly as the sender sent it. If the sender shipped the wrong thing, the signature still says Good; if a middleman changes a single byte, the signature immediately says BAD.
There is another form of signing, the "cleartext signature" (clearsign), where the signature and the original live in the same file, which suits email bodies; detached signatures suit files and software packages. Which one to use depends on how the other side verifies — software releases are almost always detached signatures, email uses both. One rule covers it: verification needs the signature file and the original file as a pair; missing either one, verification is impossible.
4. Export and share: the public key is meant to be given away
Once a key is generated, the next step is giving the public key to other people. The command is gpg --armor --export plus your email, and the output is a block of text starting with -----BEGIN PGP PUBLIC KEY BLOCK----- — in our test it began with mDMEaw1kLhYJ. Post that text on your homepage, send it to a friend, or upload it to a keyserver, and anyone who has it can send you encrypted messages and verify your signatures. Sharing a public key carries no risk at all — it was designed to be public.
Exporting the private key is another matter: the command is gpg --armor --export-secret-keys, and the output is also a block of text — but that text must never be sent to anyone or posted anywhere public. The right way to back up a private key is to store it in an encrypted backup (an encrypted USB stick or a password manager) and remember its passphrase. Losing a private key has two consequences: with no backup, the key is dead, and everything encrypted to it can never be opened; with a backup that falls into someone else's hands and no passphrase, you have simply handed the key over. The demo key in our test has no backup, because it was generated temporarily and the whole GNUPGHOME directory is deleted when we are done; for a real key, the backup is the first thing to do after generating it.
Once someone gives you their public key, you have to import it before you can use it. They send you the key text, you save it as a file, and run gpg --import on that file — the key enters your keyring. After importing, run gpg --list-keys to confirm: if you can see their name and email, the import worked. If their public key is on a keyserver, GPG can also fetch it directly with gpg --recv-keys plus the key ID. But there is a trust question here: how do you know this public key really belongs to them, and not to an impersonator? GPG's answer is the "web of trust": the key gains trust when someone you trust has signed it, or when you have checked its fingerprint with the owner in person. In our test, our own key shows [ultimate], meaning "absolutely trusted" — because it is our own. Other people's keys are untrusted by default until you confirm them yourself. The design looks cumbersome, but it is the foundation of the whole system's security: encryption only solves "nobody else can read it"; trust solves "who exactly did you lock it for".
5. Three traps: the easiest places for beginners to trip
The first trap: encrypting to a recipient who does not exist. In our test, gpg --encrypt --recipient nobody@example.invalid made GPG first try to find that person's key online (a WKD lookup), fail, and report "encryption failed" with exit code 2. That error means "there is no public key for this person in your keyring" — and encryption requires you to hold the recipient's public key. The fix is to ask them for their public key first and import it with gpg --import before encrypting. Before sending anyone their first encrypted message, confirm their public key is in your keyring; gpg --list-keys showing it is what counts.
The second trap: the master key cannot encrypt. As explained above, the ed25519 master key's usages are signing, certifying, and authenticating — no encryption. The "Unusable public key" error on our first test run happened precisely because there was only a master key and no encryption subkey. Adding a cv25519 encryption subkey fixed it. Remember the correspondence: ed25519 handles signing, cv25519 handles encryption; in gpg --list-keys output, [SCA] is the master key and [E] is the encryption subkey — both lines need to be there for encryption and decryption to work.
The third trap: forgetting the passphrase, or never setting one. A private key with no passphrase can be used by anyone who gets the file; a private key whose passphrase you forgot is a lock that cannot be opened — GPG has no "recover passphrase" feature. When you generate a key, record the passphrase in a password manager on the same day; do not leave it for "later". For a key that already exists, gpg --passwd changes the passphrase. A related trap is GNUPGHOME permissions: GPG requires that directory to be accessible only by you, and will warn or refuse to work if it is too open. In the demo we set it with chmod 700; your real ~/.gnupg should have the same permissions.
6. A complete workflow: your first encrypted message to a friend
String the commands above together and you get the full flow for two people using GPG for the first time. Step one: each of you generates a key with gpg --batch --gen-key, configured with a signing master key plus an encryption subkey, a passphrase, and an expiry. Step two: exchange public keys — export your public key and send it to your friend, receive theirs, and each import the other's key with gpg --import. Step three: check fingerprints — read each other's key fingerprints over the phone or in person, to make sure nobody swapped them in the middle; the fingerprint is the 40-digit hexadecimal string in gpg --list-keys, and checking the first and last few characters is enough. Step four: write a message, encrypt it with your friend's public key, and send it; your friend decrypts it with their own private key and reads the original. Step five: when your friend replies, they sign with their own private key, and you verify with their public key, confirming the letter is from them and unaltered.
Every step has a checkpoint: after generating, check that --list-keys shows both the master key and the subkey; after importing, check that the other person's name and email are in the list; after encrypting, check that the output file starts with -----BEGIN PGP MESSAGE-----; after decrypting, diff the result against the original; after verifying, check for a Good signature. The first time through this flow may take half an hour, but the second time takes a few minutes. Most teams get stuck the first time at the "exchange public keys" step — not because the technology is hard, but because two people first have to agree on how to exchange them; once agreed, everything after that is mechanical.
7. What to practise next
GPG is bigger than this guide, but getting started only needs the main line: generate a key (a signing master key plus an encryption subkey), encrypt and decrypt, sign and verify, export the public key. Get those four fluent, and the natural next steps follow: revocation certificates (when a key is generated, GPG also creates a revocation certificate in openpgp-revocs.d — if a key leaks, that certificate is how you announce to the world "this key is dead"), key expiry management, and uploading to keyservers.
For developers, the most useful next step is signing Git commits. Set git config user.signingkey to your key ID and turn on commit.gpgsign, and every git commit after that is automatically signed with your private key; GitHub shows a Verified badge. Anyone who sees the badge knows the commit really came from you and was not altered in between. Configure it once and benefit for a long time — that is the most common place GPG lands in a development workflow. The GnuPG manual and the Mini Howto are the main sources for this guide; for anything not covered here, check the manual directly rather than searching blogs. The full output is in the test record demo276_gpg_transcript.txt that accompanies this article, including the error from the first failed key generation and how it was fixed.
8. Frequently asked questions
The first question people ask: what is the difference between a GPG key and an SSH key? Both are asymmetric keys, but they serve different purposes: an SSH key logs you into servers and proves "you are you", while a GPG key encrypts messages and signs files. Technically, a GPG key can also be configured to support SSH login (that is what the master key's [A] authentication usage is for), but in daily use it is clearer to keep the two sets of keys separate: SSH keys for login, GPG keys for encryption and signing. Do not use the same private key both to log into servers and to sign files — if it leaks in one place, both are affected.
The second question: what if I forget the passphrase? The answer is: there is no way back. GPG is designed so that a lost passphrase means a lost key; there is no "recover passphrase" back door. The only remedy is to revoke the key, generate a new one, and tell all your contacts to update your public key. So record the passphrase in a password manager on the day you generate the key — do not leave it for "later".
The third question: how do I move my key to a new computer? The right way is to export the private key and the trust database, copy them to the new machine, and import them there. Concretely, two commands: on the old machine, gpg --armor --export-secret-keys to export the private key, and copy the whole ~/.gnupg directory (or at least trustdb.gpg) with it; on the new machine, gpg --import the private key first, then put the trust database back in place. After the move, delete the private key from the old machine — the same private key living on two computers doubles the attack surface. Before migrating, confirm the new machine's GNUPGHOME permissions are 700; that step cannot be skipped.

Comments (0)
Sources
Related articles
On 30 November 2026, Anthropic published four lattice-cryptography papers found by an internal research Claude model, with a full Lean 4 formalisation. The headline result is a quantum algorithm that attacks the maths behind the ML-KEM and ML-DSA post-quantum standards — in theory. Twenty-one independent cryptographers say its cost is 'galactic': even 2^1000 quantum operations fall far short.
cron is the scheduling daemon that has run on Linux for decades: write a crontab file saying 'run this command at this time', and it handles the rest. This guide runs everything for real on Debian cron 3.0pl1-162 — installing a job, waiting one minute for the trigger, verifying the log, removing the job — plus special strings, five classic mistakes, and a real 3 a.m. log-archiving job.
ping does exactly one thing: send an ICMP echo request, wait for the echo reply, and report the round-trip time and packet loss. This guide runs every command for real on iputils ping 20240117 — packet count, interval, quiet mode, packet size, timestamps, IPv4 vs IPv6, the three failure modes, exit codes 0/1/2 — with output pasted verbatim.