Private archive copy · Unofficial site · Content copyright belongs to Runoob Tutorial and the original authors · View Original

Linux gpg Command

Linux gpg Command

Linux Command Reference Linux Command Reference


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:


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:

  1. Key type (usually choose the default RSA and RSA)
  2. Key length (4096 bits recommended)
  3. Key expiration
  4. User ID information (name and email)
  5. Passphrase

2. Encrypt a file

gpg --encrypt --recipient alice@example.com --output secret.txt.gpg secret.txt

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

  1. Import someone else's public key:

    gpg --import bob.pub.asc
  2. Verify the key fingerprint:

    gpg --fingerprint bob@example.com
  3. 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?

  1. Generate a revocation certificate:

    gpg --gen-revoke your@email.com > revoke.asc
  2. 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

  1. Private key protection: Private key files should be kept safe; a strong passphrase is recommended
  2. Key backup: Regularly back up your keyring and revocation certificates
  3. Algorithm selection: Strong encryption algorithms such as AES-256 and RSA-4096 are recommended
  4. Key expiration: Set a reasonable key validity period and renew regularly
  5. Metadata leakage: Do not retain metadata such as file names when encrypting; you can use --throw-keyids Option

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 Linux Command Reference