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.
Back to SysOpsKey Gen & Management 8
gpg --full-generate-keyinteractive生成密钥对(optional算法和有效期)
gpg --quick-generate-key "Name <email>"快速生成密钥
gpg --list-keysList所有公钥
gpg --list-secret-keysList所有私钥
gpg --list-keys KEYIDShow指定密钥详情
gpg --edit-key KEYIDinteractiveEdit密钥(添加子密钥、Set过期等)
gpg --delete-key KEYIDDelete公钥
gpg --delete-secret-key KEYIDDelete私钥
Key Import/Export 6
gpg --export -a KEYID > pubkey.ascExport公钥为 ASCII File
gpg --export-secret-keys -a KEYID > private.ascExport私钥(务必妥善保管)
gpg --import pubkey.ascImport公钥
gpg --import private.ascImport私钥
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> trustSet密钥信任级别(interactive)
gpg --sign-key KEYID对他人公钥签名(信任背书)
gpg --refresh-keys从密钥Service器刷新所有密钥
gpg --search-keys "user@domain.com"在密钥Service器Search公钥
gpg --fingerprint KEYIDShow密钥指纹用于核对身份
Git 6
git config --global user.signingkey KEYIDSet 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-agentRestart 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