GnuPG Cheatsheet - Encryption

All essential GnuPG commands organized by use case, with 37+ entries you can copy and run directly. Find the right command fast when you need it.

SysOps·37 commands·Last updated 2026-07-21
Back to SysOps

Key Gen & Management 8

gpg --full-generate-key
interactive生成密钥对(optional算法和有效期)
gpg --quick-generate-key "Name <email>"
快速生成密钥
gpg --list-keys
List所有公钥
gpg --list-secret-keys
List所有私钥
gpg --list-keys KEYID
Show指定密钥详情
gpg --edit-key KEYID
interactiveEdit密钥(添加子密钥、Set过期等)
gpg --delete-key KEYID
Delete公钥
gpg --delete-secret-key KEYID
Delete私钥

Key Import/Export 6

gpg --export -a KEYID > pubkey.asc
Export公钥为 ASCII File
gpg --export-secret-keys -a KEYID > private.asc
Export私钥(务必妥善保管)
gpg --import pubkey.asc
Import公钥
gpg --import private.asc
Import私钥
gpg --send-keys KEYID --keyserver keyserver.ubuntu.com
上传公钥到密钥Service器
gpg --recv-keys KEYID
从密钥Service器Get公钥

Encrypt & Decrypt 6

gpg -e -r recipient@domain.com secret.txt
用收件人公钥加密File
gpg -e -r user@domain.com -o secret.txt.gpg secret.txt
加密并指定OutputFile名
gpg -d secret.txt.gpg > secret.txt
解密File并Output到指定File
gpg -c secret.txt
对称加密(仅用密码,无需公钥)
gpg -d secret.txt.gpg
解密对称加密File
echo "message" | gpg -e -r user@domain.com
加密pipeInput的文本

Sign & Verify 6

gpg -s document.txt
生成签名(生成 .gpg 二进制File)
gpg --clear-sign document.txt
生成明文签名(可读且含签名)
gpg -b document.txt
生成分离签名(.sig File,原文不变)
gpg --verify document.txt.sig document.txt
验证分离签名
gpg --verify document.txt.asc
验证明文签名File
gpg --detach-sign -a file.tar.gz
生成 ASCII 分离签名

Trust & Keyserver 5

gpg --edit-key KEYID\n> trust
Set密钥信任级别(interactive)
gpg --sign-key KEYID
对他人公钥签名(信任背书)
gpg --refresh-keys
从密钥Service器刷新所有密钥
gpg --search-keys "user@domain.com"
在密钥Service器Search公钥
gpg --fingerprint KEYID
Show密钥指纹用于核对身份

Git 6

git config --global user.signingkey KEYID
Set Git 使用的签名密钥
git config --global commit.gpgsign true
开启所有提交自动签名
git commit -S -m "message"
Create GPG 签名提交
git tag -s v1.0 -m "release"
Create GPG 签名标签
git verify-commit HEAD
验证最新提交的签名
gpgconf --kill gpg-agent
Restart gpg-agent(解决密码cache问题)

💡 Tips

  • Git commit 签名:git config --global commit.gpgsign true,配合 GPG 密钥使用。
  • gpg --armor (-a) 可以生成 ASCII 格式的密钥或签名,方便文本传输。
  • 密钥backup:Export私钥 gpg --export-secret-keys -a KEYID > private.asc,妥善保管。
  • gpg-agent 可以cache密码,避免每次解密都Input密码。
  • gpgconf --kill gpg-agent 可以Restart agent,解决密码cacheexception问题。
  • 密钥指纹是验证密钥真伪的重要方式,Import他人公钥后务必核对指纹。

Official References

Commands are compiled from the official docs below. Click to verify the latest usage.

Maintained by LaoHand

Publicly updated on Jul 21, 2026, continuously proofread against official docs.

Found an error? Report it

Wrong command or description? Open an issue to help us fix it.

Found an error? Report it