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:
gpg --version
# macOSbrew install gnupg
# Debian and Ubuntusudo apt install gnupgGenerate or import a key
Generate a new key pair. --full-gen-key asks for the key type, size, expiry, name and email address:
gpg --full-gen-keyOr import an existing private key:
gpg --import /path/to/your/private-key.ascSet the default key
List your secret keys in long ID format, optionally filtered by email address:
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] D5E4F29F3275DC0CDA8FFC8730F2B65B9246B6CAuid [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:
gpg --default-key <YOUR_KEY_ID> --sign file.txtTo make the key the default for every command, add this line to ~/.gnupg/gpg.conf:
default-key <YOUR_KEY_ID>Print the public key in ASCII-armored form, for example to paste into GitHub or GitLab:
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:
$ 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-10In this example the key ID is 3AA5C34371567BD2. Open the key for editing:
gpg --edit-key 3AA5C34371567BD2At the gpg> prompt, add the new user ID and fill in the details:
gpg> adduidReal name: OctocatEmail address: octocat@github.comComment: GitHub keyChange (N)ame, (C)omment, (E)mail or (O)kay/(Q)uit?Enter O to confirm, then your key’s passphrase. Save the changes:
gpg> saveExport the updated public key and upload it again wherever you published it:
gpg --armor --export 3AA5C34371567BD2Copy 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:
git config --global --unset gpg.formatSet your GPG signing key:
git config --global user.signingkey 3AA5C34371567BD2Sign with an SSH key
Git 2.34 and later can sign with an SSH key instead. Set the format to SSH:
git config --global gpg.format sshThen point user.signingkey at the public key file:
git config --global user.signingkey ~/.ssh/id_ed25519.pubOr give the key itself. On macOS you can copy it to the clipboard first:
pbcopy < ~/.ssh/id_ed25519.pubPaste the key in quotes, since it contains spaces, and prefix it with key:::
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:
git commit -S -m "My signed commit"To sign every commit without -S:
git config --global commit.gpgSign trueFrom then on a plain git commit is signed automatically:
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:
gpg -a --export-secret-key KEYID > private-key.gpgEncrypt 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:
openssl aes-256-cbc -md sha512 -pbkdf2 -iter 100000 -salt -pass env:OPENSSL_PWD -in private-key.gpg -out private-key.gpg.encIn the pipeline, decrypt it with the same options plus -d, then import private-key.gpg into the build’s keyring:
openssl aes-256-cbc -d -md sha512 -pbkdf2 -iter 100000 -salt -pass env:OPENSSL_PWD -in private-key.gpg.enc -out private-key.gpgDo 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_TOKENandOSSRH_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 exampleF784FAB8.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:
gpg --list-secret-keysExport the public key:
gpg --armor --export YOUR_KEY_ID > public.ascExport the private key. GnuPG asks for the passphrase:
gpg --armor --export-secret-keys YOUR_KEY_ID > private.ascOptionally, export the trust settings:
gpg --export-ownertrust > trust.txt2. 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:
gpg --import public.ascgpg --import private.ascImport the trust settings, if you exported them:
gpg --import-ownertrust trust.txt4. Set ultimate trust
Without the trust data, GnuPG shows the key’s trust as “unknown”. Set ultimate trust for your own key:
- Run
gpg --edit-key YOUR_KEY_ID. - Type
trustand press Enter. - Enter
5for “I trust ultimately” and confirm withy. - Type
quitto exit.
Things to check
-
Permissions: the
~/.gnupgdirectory 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
~/.gitconfigfrom 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
~/.gnupgdirectory to the new account and change its ownership withchown -R.