Session and utilities

The gpg command

Encrypts and decrypts files with GnuPG. With -c a passphrase is enough, no keys needed.

Syntax

gpg [-c archivo | -d archivo.gpg | -k]Try it in the terminal →

Most useful options

-c, --symmetricEncrypts with a passphrase. The default symmetric cipher is AES-128; --cipher-algo lets you change it.
-d, --decryptDecrypts the file and writes the result to standard output, or to the file given with --output.
-e, --encryptEncrypts for the public keys of the recipients given with --recipient.
-o, --output <archivo>Writes the result to that file instead of standard output.
-k, --list-keysLists the public keys in the keyring. Its format is for people, not for scripts.
-K, --list-secret-keysLists the private keys you have in the keyring.

Copy-ready examples

$ gpg -c nota.txtencrypts nota.txt with a passphrase that gpg will ask you for.
$ gpg -d nota.txt.gpgshows the decrypted content on screen. Without --output no new file is created.
$ gpg -o nota.txt -d nota.txt.gpgdecrypts to nota.txt instead of showing it on screen.
$ gpg -Klists the private keys in the keyring.

You can try them in the browser terminal: it does not touch your computer.

Common errors

MessageCauseFix
gpg: can't open 'nota.txt.gpg': No such file or directoryThe encrypted file is not in the current folder, or the name is misspelled.List the folder with ls and use the exact name.
gpg: decryption failed: Bad session keyThe passphrase you typed is not the one used to encrypt.Type it again carefully. Without the right passphrase there is no way to recover the content.
gpg: no valid OpenPGP data found.The file is not encrypted with gpg, or it is damaged.Check what it is with file and download it again if it is incomplete.

Typical mistakes

  • gpg -c does not delete the original: the plain file stays on disk until you delete it.
  • gpg -d without -o writes to the screen. If you want a file, use -o.
  • The -c passphrase cannot be recovered: if you forget it, the encrypted file cannot be opened.

Differences from similar commands

  • openssl: openssl enc also encrypts with a password, but it does not manage keys or signatures. gpg does.
  • sha256sum: computes fingerprints to check that a file has not changed. It encrypts nothing.
  • zip: zip -e encrypts with its own method, different from OpenPGP. An encrypted .zip is not decrypted with gpg.

Related commands

FAQ

Which passphrase does gpg -c ask for?

The one you choose when encrypting. It does not depend on your keyring, so anyone who knows it can decrypt with gpg on any machine.

Can I decrypt without the passphrase?

No. gpg may remember the passphrase used for encryption during the session, unless you use --no-symkey-cache.

What is --output for when using -d?

So the result goes to a file instead of the screen, for example -o nota.txt.

Learn to actually use it

The basic Linux course takes you from zero to console fluency with 24 self-grading lessons.

Start the free course