Linux gpg Command
GPG (GNU Privacy Guard) is an implementation of the OpenPGP standard developed by the GNU project, used for encrypting and decrypting data and for creating and verifying digital signatures. It is an open-source alternative to PGP (Pretty Good Privacy), widely used in scenarios such as file encryption, email security, and software package signature verification.
Basic syntax structure
The basic syntax of the gpg command is as follows:
gpg [选项] [命令] [文件名]
Main components:
- Option: Various options that control GPG's behavior
- Command: Specify the type of operation to perform
- File Name: The file to process (optional)
Common command parameters
Key management
| Parameters | Description |
|---|---|
--gen-key |
Generate a new key pair |
--list-keys |
List all public keys |
--list-secret-keys |
List all private keys |
--delete-key |
Delete a public key |
--delete-secret-key |
Delete a private key |
--import |
Import a key |
--export |
Export a key |
Encryption/decryption operations
| Parameters | Description |
|---|---|
--encrypt (-e) |
Encrypt a file |
--decrypt (-d) |
Decrypt a file |
--sign (-s) |
Create a signature |
--verify |
Verify a signature |
--armor (-a) |
Generate ASCII-formatted output |
Other common options
| Parameters | Description |
|---|---|
--recipient (-r) |
Specify the recipient's key |
--output (-o) |
Specify the output file |
--passphrase |
Specify the passphrase |
Practical Application Examples
1. Generate a key pair
gpg --gen-key
After execution, it will ask interactively:
- Key type (usually choose the default RSA and RSA)
- Key length (4096 bits recommended)
- Key expiration
- User ID information (name and email)
- Passphrase
2. Encrypt a file
gpg --encrypt --recipient alice@example.com --output secret.txt.gpg secret.txt
--recipientSpecify the recipient's public key (identified by email)--outputSpecify the encrypted output file- The last argument is the original file to encrypt
3. Decrypt a file
gpg --decrypt --output plain.txt secret.txt.gpg
The system will prompt you for the private key passphrase
4. Create and verify signatures
Create a signature:
gpg --sign --output document.sig document.txt
Verify a signature:
gpg --verify document.sig document.txt
5. Export a public key
gpg --armor --export alice@example.com > alice.pub.asc
--armor option to generate an ASCII-formatted public key file
Key management practices
Keyring operation flow

Setting up key trust relationships
-
Import someone else's public key:
gpg --import bob.pub.asc
-
Verify the key fingerprint:
gpg --fingerprint bob@example.com
-
Sign the key to establish trust:
gpg --sign-key bob@example.com
Frequently Asked Questions
1. How can I avoid entering the password every time I decrypt?
Use gpg-agent to cache the password:
gpg --use-agent --decrypt file.gpg
2. How can I revoke a lost key?
-
Generate a revocation certificate:
gpg --gen-revoke your@email.com > revoke.asc
-
Publish the revocation certificate:
gpg --import revoke.asc
3. What is the best practice for encrypting large files?
Use symmetric encryption combined with asymmetric encryption:
gpg --symmetric --cipher-algo AES256 largefile.iso
Security considerations
- Private key protection: Private key files should be kept safe; a strong passphrase is recommended
- Key backup: Regularly back up your keyring and revocation certificates
- Algorithm selection: Strong encryption algorithms such as AES-256 and RSA-4096 are recommended
- Key expiration: Set a reasonable key validity period and renew regularly
- Metadata leakage: Do not retain metadata such as file names when encrypting; you can use
--throw-keyidsOption
By mastering the gpg command, you can effectively protect sensitive data and achieve secure file transfer and authentication. It is recommended to familiarize yourself with each operation in a test environment before using it in practice.
Linux Command Reference