Skip to content

GnuPG (GPG)

Create and manage GPG keys, sign Git commits with GPG or SSH, sign Maven artifacts in CI and move keys to another account.

6 min readUpdated

GnuPG (gpg) is the free implementation of the OpenPGP standard. Developers use it mainly to sign Git commits and tags and to sign artifacts published to Maven Central.

This note covers creating or importing a key, choosing a default key, adding an identity, configuring Git to sign with a GPG or SSH key, using a key in a Bitbucket Pipelines build, and copying a key to another user account.

Create or import a key

Install GnuPG

Check whether gpg is installed, and install it if not:

Terminal window
gpg --version
# macOS
brew install gnupg
# Debian and Ubuntu
sudo apt install gnupg

Generate or import a key

Generate a new key pair. --full-gen-key asks for the key type, size, expiry, name and email address:

Terminal window
gpg --full-gen-key

Or import an existing private key:

Terminal window
gpg --import /path/to/your/private-key.asc

Set the default key

List your secret keys in long ID format, optionally filtered by email address:

Terminal window
gpg --list-secret-keys --keyid-format LONG <EMAIL>

The output looks like this. The key ID is the part after the slash on the sec line, here 30F2B65B9246B6CA:

sec rsa4096/30F2B65B9246B6CA 2017-08-18 [SC]
D5E4F29F3275DC0CDA8FFC8730F2B65B9246B6CA
uid [ultimate] Mr. Robot <your_email>
ssb rsa4096/B7ABC0813E4028C0 2017-08-18 [E]

For Maven, set the key that the maven-gpg-plugin signs with as a property in settings.xml or pom.xml:

<properties>
<gpg.keyname>YOUR_KEY_ID</gpg.keyname>
</properties>

For GnuPG itself, --default-key selects the key for a single command:

Terminal window
gpg --default-key <YOUR_KEY_ID> --sign file.txt

To make the key the default for every command, add this line to ~/.gnupg/gpg.conf:

~/.gnupg/gpg.conf
default-key <YOUR_KEY_ID>

Print the public key in ASCII-armored form, for example to paste into GitHub or GitLab:

Terminal window
gpg --armor --export <YOUR_KEY_ID>

Add an identity to a key

A key can carry several user IDs (name and email address). Add one when you sign commits with a different email address, for example a new work address.

List the keys for which you have both a public and a private key; a private key is required for signing commits and tags:

Terminal window
$ gpg --list-secret-keys --keyid-format=long
/Users/hubot/.gnupg/secring.gpg
------------------------------------
sec 4096R/3AA5C34371567BD2 2016-03-10 [expires: 2017-03-10]
uid Hubot <hubot@example.com>
ssb 4096R/4BB6D45482678BE3 2016-03-10

In this example the key ID is 3AA5C34371567BD2. Open the key for editing:

Terminal window
gpg --edit-key 3AA5C34371567BD2

At the gpg> prompt, add the new user ID and fill in the details:

Terminal window
gpg> adduid
Real name: Octocat
Email address: octocat@github.com
Comment: GitHub key
Change (N)ame, (C)omment, (E)mail or (O)kay/(Q)uit?

Enter O to confirm, then your key’s passphrase. Save the changes:

Terminal window
gpg> save

Export the updated public key and upload it again wherever you published it:

Terminal window
gpg --armor --export 3AA5C34371567BD2

Copy the whole block, from -----BEGIN PGP PUBLIC KEY BLOCK----- to -----END PGP PUBLIC KEY BLOCK-----.

Configure Git to sign commits

Sign with a GPG key

If you previously configured Git to sign with another key format, unset that setting so the default format, openpgp, is used:

Terminal window
git config --global --unset gpg.format

Set your GPG signing key:

Terminal window
git config --global user.signingkey 3AA5C34371567BD2

Sign with an SSH key

Git 2.34 and later can sign with an SSH key instead. Set the format to SSH:

Terminal window
git config --global gpg.format ssh

Then point user.signingkey at the public key file:

Terminal window
git config --global user.signingkey ~/.ssh/id_ed25519.pub

Or give the key itself. On macOS you can copy it to the clipboard first:

Terminal window
pbcopy < ~/.ssh/id_ed25519.pub

Paste the key in quotes, since it contains spaces, and prefix it with key:::

Terminal window
git config --global user.signingkey 'key::ssh-ed25519 AAAAC3(...) user@example.com'

Git still accepts the key without the key:: prefix, but that form is deprecated.

Turn on signing

Sign a single commit with -S:

Terminal window
git commit -S -m "My signed commit"

To sign every commit without -S:

Terminal window
git config --global commit.gpgSign true

From then on a plain git commit is signed automatically:

Terminal window
git commit -m "My commit"

Sign Maven artifacts in Bitbucket Pipelines

A CI build that publishes signed artifacts needs the private key, but the key must not be stored in the repository in plain form. One approach: commit the key encrypted with OpenSSL and keep the password in a protected pipeline variable.

Export the private key:

Terminal window
gpg -a --export-secret-key KEYID > private-key.gpg

Encrypt it with OpenSSL before committing it as private-key.gpg.enc. -pass env:OPENSSL_PWD reads the password from the environment variable, so it does not appear in the process list or the shell history:

Terminal window
openssl aes-256-cbc -md sha512 -pbkdf2 -iter 100000 -salt -pass env:OPENSSL_PWD -in private-key.gpg -out private-key.gpg.enc

In the pipeline, decrypt it with the same options plus -d, then import private-key.gpg into the build’s keyring:

Terminal window
openssl aes-256-cbc -d -md sha512 -pbkdf2 -iter 100000 -salt -pass env:OPENSSL_PWD -in private-key.gpg.enc -out private-key.gpg

Do not add -p to these commands: it prints the derived key and IV, which would end up in the build log.

OPENSSL_PWD is a secured repository variable in Bitbucket, used by bitbucket-pipelines.yml. The Maven settings.xml reads the other credentials from environment variables as well:

  • OSSRH_USER_TOKEN and OSSRH_PWD_TOKEN: the user token for the repository server, used instead of your real username and password. You generate the token after logging in to the repository’s web interface.
  • GPG_KEY: the ID of the GPG key used for signing, for example F784FAB8.
  • GPG_PWD: the passphrase of the GPG key.

Note: Sonatype’s OSSRH service reached end of life on 30 June 2025. New Maven Central publishing goes through the Central Portal, which issues its own user tokens; the encrypted-key setup above stays the same.

Copy a key to another account

To use the same key under another user account or on another machine, export it, transfer the files securely and import them.

1. Export the key from the source account

Find the key ID:

Terminal window
gpg --list-secret-keys

Export the public key:

Terminal window
gpg --armor --export YOUR_KEY_ID > public.asc

Export the private key. GnuPG asks for the passphrase:

Terminal window
gpg --armor --export-secret-keys YOUR_KEY_ID > private.asc

Optionally, export the trust settings:

Terminal window
gpg --export-ownertrust > trust.txt

2. Transfer the files

The files contain your private key. Move them with a secure method such as scp, and delete them from both machines when you are done.

3. Import the key in the destination account

Import the keys:

Terminal window
gpg --import public.asc
gpg --import private.asc

Import the trust settings, if you exported them:

Terminal window
gpg --import-ownertrust trust.txt

4. Set ultimate trust

Without the trust data, GnuPG shows the key’s trust as “unknown”. Set ultimate trust for your own key:

  1. Run gpg --edit-key YOUR_KEY_ID.
  2. Type trust and press Enter.
  3. Enter 5 for “I trust ultimately” and confirm with y.
  4. Type quit to exit.

Things to check

  • Permissions: the ~/.gnupg directory in the new account must be readable only by its owner: chmod 700 ~/.gnupg.

  • Git: set the signing key in the new account, or copy ~/.gitconfig from the old account:

    Terminal window
    git config --global user.signingkey YOUR_KEY_ID
  • Windows and Git Bash: make sure the exported files are not saved with a byte order mark (BOM), which causes import errors.

  • Alternative: instead of exporting, copy the whole ~/.gnupg directory to the new account and change its ownership with chown -R.

References

Related notes

2 min read

Trivy

Install Trivy and scan project files, Git repositories, container images and Kubernetes workloads for vulnerabilities and misconfigurations.

  • Security
  • DevOps & Containers
3 min read

JSON Web Tokens (JWT)

How access and refresh tokens work in JWT authentication, the endpoints a server needs, and where a client should keep each token.

  • Security
  • APIs & Integration
3 min read

SSL/TLS certificates

What the usual certificate files contain, how to convert CRT to PEM, and how to issue, list, renew and delete Let's Encrypt certificates with Certbot.

  • Security

Type to search the whole site.