Skip to content

GNU Privacy Guard GPG Snippets, Pretty Good Privacy (PGP)

Terminal window
# Show configuration
gpgconf
## List configured directories with -L with --list-dirs
gpgconf -L
# Export gpg keyring to older keyring format
gpg --export > ~/.gnupg/pubring.gpg
# Create a GPG key pair
gpg --full-generate-key
# Generate new GPG keys for Encryption in expert mode
gpg --expert --full-gen-key
# Generate a new key pair, for options:
# - Kind of key: ECC and ECC (Elliptic curve cryptography)
# - elliptic curve: Cure 25519 (ed25519)
# - Key expiry date (good practice): 2 years
## List keys with Key ID with --list-keys
gpg --list-keys --keyid-format LONG
# can also use -k
# List long form of GPG keys
gpg --list-secret-keys --keyid-format=long
# Set home directory of GNUPG, for example on Windows to different directory, then list keys
# - If not used, home directory defaults to ~/.gnupg. Only recognized when given on the command line
# - Overrides any home directory stated through the environment variable GNUPGHOME
# or (on Windows systems) by means of the Registry entry HKCU\Software\GNU\GnuPG:HomeDir.
# https://www.gnupg.org/documentation/manuals/gnupg/Configuration-Options.html
gpg --homedir ~/.gnupg --list-keys
## Windows msys2 D drive
gpg --homedir d/.gnupg --list-keys
# Set home directory of GNUPG with environment variable
export GNUPGHOME="/e/.gnupg"
## Get fingerprint with a key ID
gpg --fingerprint <keyID>
# Prints the GPG key ID, in ASCII armor format using key's ID
gpg --armor --export 3AA5C34371567BD2
# Export your public key, give this file to people you want to communicate with
gpg --armor --export user-id-or-email > pubkey.asc
# Export your keys
## Export an ascii armored version of the public key
gpg --output public.pgp --armor --export username@email
## Export Secret Key - export an ascii armored version of the secret key:
gpg --output private.pgp --armor --export-secret-key username@email
# Import a public key
gpg --import public.gpg
# Import a private key
gpg --import private.pgp
# Export public key for a person
gpg --export --armor alice@example.com
# Sign a file without encryption
gpg --clearsign doc.txt
# Encrypt and sign a document for multiple people
gpg --output doc.txt.gpg --encrypt --sign --recipient alice@example.com --recipient bob@example.com doc.txt
# --output : encrypted file after command
# --encrypt : encrypt a file
# --receipient : email corresponding to the GPG key of the receiver imported previously
# --sign : sign with your key
# Decrypt a file
gpg --output doc.txt --decrypt doc.txt.gpg
# Encrypt file with only passphrase
gpg --symmetric doc.txt
gpg -c doc.txt
# Decrypt file to output -o files doc.txt
gpg -o 'doc.txt' -d 'doc.txt.gpg'
# Export trust of keys (for example for ultimate levels)
gpg --export-ownertrust > trust.txt
# Import trust of keys
gpg --import-ownertrust trust.txt
# Edit key, for example trust level
gpg --edit-key <key-ID>
## Sign someone's public because you know it is from them and trust them locally
gpg --sign-key <keyID>
Terminal window
# Encrypt file or directories as archive
gpgtar -e -r alice@example.com -o mydir.gpg mydir
# -e encrypt
# -r recipient ID
# -o output file
# List contents of archive
gpgtar --list-archive myarchive.gpg
# Decrypt archive
mkdir mydir
gpgtar -d --directory mydir myarchive.gpg