Skip to content

Hướng dẫn cấu hình GPG

GPG (GNU Privacy Guard) là tiêu chuẩn truyền thống để ký commit:

Tại sao ký GPG?

  • Huy hiệu xác minh trên GitHub, GitLab và Bitbucket.
  • Mạng tin cậy — người khác có thể ký khóa của bạn.
  • Hết hạn và thu hồi khóa — quản lý vòng đời tích hợp.
  • Mã hóa — cùng một khóa mã hóa tệp và email.

Điều kiện tiên quyết

  • GnuPG 2.2+ (gpg --version)
  • Git 2.0+
  • GitHub / GitLab

Bước 1: Tạo khóa GPG

bash
# Generate a new GPG key (RSA 4096-bit)
gpg --full-generate-key
  1. (1) RSA and RSA
  2. 4096
  3. 1y
  4. Your Full Name
  5. your@email.com
bash
# List secret keys with key IDs
gpg --list-secret-keys --keyid-format=long
text
sec   rsa4096/ABC123DEF4567890 2026-03-08 [SC] [expires: 2027-03-08]
      ABCDEF1234567890ABCDEF1234567890ABCDEF12
uid                 [ultimate] Your Name <your@email.com>
ssb   rsa4096/1234567890ABCDEF 2026-03-08 [E] [expires: 2027-03-08]

Bước 2: Cấu hình Git

bash
# Set your signing key
git config --global user.signingkey ABC123DEF4567890

# Enable automatic commit signing
git config --global commit.gpgsign true

# Enable automatic tag signing
git config --global tag.gpgsign true

# Set the GPG program
git config --global gpg.program gpg
ini
[user]
    signingkey = ABC123DEF4567890
[commit]
    gpgsign = true
[tag]
    gpgsign = true
[gpg]
    program = gpg

Bước 3: Cấu hình GPG agent

bash
# Create or edit gpg-agent.conf
mkdir -p ~/.gnupg
cat > ~/.gnupg/gpg-agent.conf << 'EOF'
default-cache-ttl 28800
max-cache-ttl 28800
pinentry-program /usr/bin/pinentry-curses
EOF

# Restart the agent
gpgconf --kill gpg-agent
gpg-agent --daemon

macOS

bash
# Install pinentry-mac
brew install pinentry-mac
echo "pinentry-program $(which pinentry-mac)" > ~/.gnupg/gpg-agent.conf
gpgconf --kill gpg-agent

GPG TTY

bash
export GPG_TTY=$(tty)

Bước 4: Ký và xác minh

bash
# Commits are now signed automatically
git commit -m "feat: add new feature"

# Or sign explicitly
git commit -S -m "feat: signed commit"

# Sign a tag
git tag -s v1.0.0 -m "Release v1.0.0"

# Verify the last commit
git log --show-signature -1

# Verify a tag
git tag -v v1.0.0

Bước 5: Xuất và đăng ký khóa

bash
# Export in ASCII format
gpg --armor --export ABC123DEF4567890

GitHub

  1. Settings → SSH and GPG keys → New GPG key
  2. -----BEGIN PGP PUBLIC KEY BLOCK-----

GitLab

  1. Preferences → GPG Keys

Keyserver

bash
gpg --keyserver keys.openpgp.org --send-keys ABC123DEF4567890

Quản lý khóa

bash
# Extend expiration
gpg --edit-key ABC123DEF4567890
# expire → save

# Backup secret key
gpg --armor --export-secret-keys ABC123DEF4567890 > gpg-secret-key.asc

# Backup trust
gpg --export-ownertrust > gpg-trust.txt

# Restore
gpg --import gpg-secret-key.asc
gpg --import-ownertrust gpg-trust.txt

Tích hợp Chezmoi

bash
# Add GPG config files
chezmoi add ~/.gnupg/gpg.conf
chezmoi add ~/.gnupg/gpg-agent.conf
chezmoi add ~/.gitconfig
ini
[user]
    signingkey = {{ .gpg_key_id }}
[commit]
    gpgsign = true
[tag]
    gpgsign = true
[gpg]
    program = gpg
toml
[data]
gpg_key_id = "ABC123DEF4567890"

Khắc phục sự cố

ProblemSolution
gpg: signing failed: No secret keyuser.signingkeygpg --list-secret-keys
gpg: signing failed: Inappropriate ioctlexport GPG_TTY=$(tty)
No passphrase promptpinentry-mac / pinentry-curses
error: gpg failed to sign the datagpgconf --kill gpg-agent

Ghi chú nền tảng

FeaturemacOSLinuxWSL
GnuPG 2.2+
pinentry-macN/AN/A
pinentry-curses
Keyserver
Chezmoi

Liên quan