Elevating Security with GnuPG: A Comprehensive Guide
Discover the power of GnuPG for securing your communications and data. This guide provides essential insights into configuring GnuPG for new users, generating keys, and managing secure sockets. Learn how to seamlessly integrate GnuPG into your workflow, ensuring your digital security is top-notch. Whether you're backing up keys, exporting public keys, or setting up GnuPG for the first time, this guide has you covered with practical commands and expert tips.
Default options for new users
If you want to setup some default options for new users, put configuration files in /etc/skel/.gnupg/. When the new user is added in system, files from here will be copied to its GnuPG home directory. There is also a simple script called addgnupghome which you can use to create new GnuPG home directories for existing users:
Permission denied errors gets fixed by set the correct permissions
Add to bashrc
# Set GPG TTY
# ----------------
export GPG_TTY=$(tty)
gpg-connect-agent updatestartuptty /bye >/dev/null
unset SSH_AGENT_PID
if [ "${gnupg_SSH_AUTH_SOCK_by:-0}" -ne $$ ]; then
export SSH_AUTH_SOCK="$(gpgconf --list-dirs agent-ssh-socket)"
fi
if ! pgrep -x -u "${USER}" gpg-agent >/dev/null 2>&1; then
gpg-connect-agent /bye >/dev/null 2>&1
fi
Use password directly in commandline by using batch mode
Exporting a public key (The key is exported in a binary format)
Export key in ascii/plain text
The key is exported in a binary format, but this can be inconvenient when the key is to be sent though email or published on a web page. GnuPG therefore supports a command-line option --armor[1] that that causes output to be generated in an ASCII-armored format similar to uuencoded documents. In general, any output from GnuPG, e.g., keys, encrypted documents, and signatures, can be ASCII-armored by adding the --armor option."
Encrypting and decrypting documents
You will get an output file of gpg after this command has been executed
Encrypt a textfile with GnuPG and AES256
Hardcore encrypt a file with twofish encryption
Decrypt file to a specifik file
Encrypt directory with GnuPG and tar
Use a key file instead of passphrases
Importing keys
How to verify an important gpg key
Export Keys
This command will export an ascii armored version of the public key
This command will export an ascii armored version of the secret key
Export all encrypted private keys (which will also include public keys)
Generating a revocation certificate
Generating a revocation certificate
After your keypair is created you should immediately generate a revocation certificate for the primary public key using the option --gen-revoke. If you forget your passphrase or if your private key is compromised or lost, this revocation certificate may be published to notify others that the public key should no longer be used. A revoked public key can still be used to verify signatures made by you in the past, but it cannot be used to encrypt future messages to you. It also does not affect your ability to decrypt messages sent to you in the past if you still do have access to the private key.
Misc
Backup GnuPG configuration/keyring
Example output from metadata
gpg --list-packets gnupg_text_encryption.md.gpg
gpg: encrypted with cv25519 key, ID 0D4CA1775E7FBA1A, created 2026-01-22
"linux-shell <info@linux-shell.se>"
# off=0 ctb=84 tag=1 hlen=2 plen=94
:pubkey enc packet: version 3, algo 18, keyid 0D4CA1775E7FBA1A
data: [263 bits]
data: [392 bits]
# off=96 ctb=d4 tag=20 hlen=3 plen=461 new-ctb
:aead encrypted packet: cipher=9 aead=2 cb=16
length: 461
# off=118 ctb=a3 tag=8 hlen=1 plen=0 indeterminate
:compressed packet: algo=2
# off=120 ctb=ad tag=11 hlen=3 plen=742
:literal data packet:
mode b (62), created 1769042105, name="gnupg_text_encryption.md",
raw data: 712 bytes
Send a signed and encrypted email from the command line
- A very simple command to send a signed and encrypted message from the command line using GPG Keys
tar.gz with gpg-ecryption on the fly
tar.gz with gpg-encryption on the fly
Delete secret keys
Key Servers
Send keys to a server
Add a gpg key to aptitute package manager in a ubuntu system
GnuPG --edit-key
- Edit Options
Tips and Tricks
Add the following to ~/.bashrc and you just have to type crypt filename.txt