References
- Associating an email with your GPG key
- Telling Git about your signing key
- How to enable SSH access using a GPG key for authentication
- GPG Keys Management
Creating GPG secret key
Ensure GPG is Installed
| |
Generate or Import a GPG Key
| |
| |
Set the Default GPG Key
| |
| |
Find the key you want to use and note its ID. Set it as the default by adding it to your Maven configuration in the settings.xml file or your pom.xml.
| |
Alternatively, you can set it globally for GPG by running:
| |
To show the associated public key, run this command, replacing
| |
Change identity for a GPG key
to list the long form of the GPG keys for which you have both a public and private key. A private key is required for signing commits or tags.
| |
In this example, the GPG key ID is 3AA5C34371567BD2:
| |
to add the user ID details.
| |
Enter O to confirm your selections.
Enter your key’s passphrase.
To save the changes.
| |
To print the GPG key in ASCII armor format
| |
Copy your GPG key, beginning with -----BEGIN PGP PUBLIC KEY BLOCK----- and ending with -----END PGP PUBLIC KEY BLOCK-----.
Telling Git about your signing key
Telling Git about your GPG key
If you have previously configured Git to use a different key format when signing with --gpg-sign, unset this configuration so the default format of openpgp will be used.
| |
To set your primary GPG signing key in Git
| |
Telling Git about your SSH key
Configure Git to use SSH to sign commits and tags:
| |
To set your SSH signing key in Git
| |
or copy the SSH public key to your clipboard.
| |
To set your SSH signing key in Git
| |
Enable signing in Git
| |
To avoid needing -S every time
| |
Now you can just run
| |
And it will be signed automatically.
Bitbucket Pipelines Setup
The private-key.gpg.enc file is an openssl-encrypted, GPG key file, that contains the key required for signing all files. The unencrypted key file can exported with gpg:
| |
Before checking it into the bitbucket repository, it was encrypted with openssl:
| |
The password value for the openssl encryption and decryption is stored in the OPENSSL_PWD environment variable configured in Bitbucket and used in the bitbucket-pipelines.yml script.
The settings.xml is configured to access environment variables for the authentication to OSSRH as well the GPG signing:
OSSRH_USER_TOKENandOSSRH-PWD_TOKEN: The username and password tokens for your OSSRH account as an alternative to your actual username and password. You can retrieve the token values by logging into OSSRH.GPG_KEY: the name of the GPG key file to use for signing e.g.F784FAB8GPG_PWD: the password to access the GPG key
Copying a GPG key from one account to another
- Export Keys from the Source Account
List Keys to find your Key ID:
| |
Export Public Key:
| |
Export Private Key: This step will require your GPG passphrase.
| |
Export Trust Data (Optional): This preserves your trust settings for the keys.
| |
Transfer the Files Since these files contain your private key, use a secure method like scp
Import Keys to the Destination Account
Import Keys:
| |
Import Trust Data:
| |
- Verify and Set Ultimate Trust If you did not import a trust database, GPG may show “trust undefined.” You should manually set Ultimate Trust for your own key:
a. Run gpg –edit-key YOUR_KEY_ID.
b. Type trust and press Enter.
c. Enter 5 for “ultimate trust” and confirm with y.
d. Type quit to exit.
- Important Considerations
- Permissions: Ensure the .gnupg directory in the new account has correct permissions (700), often requiring chmod 700 ~/.gnupg.
- Git Configuration: Update your Git config on the new account to use the imported key, or you can copy ~/.gitconfig from the old user to the new user.
| |
- Windows/Git Bash: If transferring to Windows, ensure files are not encoded with BOM (Byte Order Mark) to avoid import errors.
- Alternatively, you can copy the entire ~/.gnupg directory from the old user to the new user, ensuring ownership is updated with chown -R.