Adding a GPG Key
What is a GPG keypair?
GPG is an open-source cryptographic software application that implements the OpenPGP standard. With a GPG keypair, you can sign or encrypt text. Git allows you to sign your commits, so other collaborators can be assured it was you who created them.
Checking for an Existing GPG keypair
If you have your public key in an easy-to-find location, great! You can skip to adding it to your account. If not, we will be using the GnuPG software to check.
- Download and install GnuPG.
If you are using Linux, this might already be installed. Check by typing gpg --version
in the terminal.
If you are using Windows, please use Git Bash instead of Powershell. Powershell will have issues with verifying the key against Forgejo, and you will need to regenerate the key anyway in Git Bash. Kleopatra and other Windows GPG tools do not work either.
- Type
gpg --list-secret-keys --keyid-format LONG
into your terminal, and it will list all the keys that you have both a public and a private key for. - Look through the output for a key you want. Continue to Generating a GPG key if there are none, or if there are none that you want to use. If you wish to use one, go to adding your GPG key to Forgejo.
Make sure that your selected key uses the same email as your Forgejo account.
Generating a GPG keypair
If you haven't already, be sure to install GnuPG, as you will be using it to generate your keys.
- Type
gpg --full-generate-key
into your terminal. - Type
1
and press Enter to selectRSA and RSA
. - Enter your desired key size; we recommend 4096 bits. Press Enter to confirm.
- Choose the amount of time you want it to be valid for; we recommend 1-2 years for increased security, but you can type 0 if you don't want it expire. Press Enter to confirm.
- Verify your selections are correct, then type
y
and press Enter to confirm. - Enter your information; be sure to use the same email as your Forgejo account.
- Type in a passphrase; make sure you write it down somewhere safe. You'll need it later to add your key to Git or revoke it if it's compromised.
Adding your public GPG key to Forgejo
- Type
gpg --list-secret-keys --keyid-format LONG
into the terminal. - Select the key you would like to use (most likely the one you just generated). In this example, the GPG key ID is
3AA5C34371567BD2
:
$ gpg --list-secret-keys --keyid-format LONG
/home/knut/.gnupg/pubring.kbx
--------------------------
sec rsa4096/3AA5C34371567BD2 2021-06-06 [SC] [expires: 2022-06-06]
6CD8F2B4F3E2E8F08274B563480F8962730149C7
uid [ultimate] foo <foo@cei-engineering.com>
ssb rsa4096/42B317FD4BA89E7A 2021-06-06 [E] [expires: 2022-06-06]
- Type
gpg --armor --export <GPG KEY ID>
into the terminal. This will output your public key. - Copy the output beginning with
-----BEGIN PGP PUBLIC KEY BLOCK-----
and ending with-----END PGP PUBLIC KEY BLOCK-----
. - Go to the SSH/GPG Keys tab in your Forgejo settings.
- Click Add Key in the Manage GPG Keys section, then paste in your public key and click the Add Key button.
Verifying your public GPG key
Anyone can add a random public key; but fortunately, Forgejo provides a mechanism to verify that the key belongs to you. Every keypair consists of a public and a private key that are connected to one another. Using this private key, you can sign the provided message. If the signed message is valid, Forgejo can confirm that the added key is yours.
- Go to the SSH/GPG Keys tab in your Forgejo settings.
- Click on the Verify button by the GPG key you would like to verify.
- Forgejo will show a token. Under its text box you can copy the correct command, and paste it into your terminal.
- Copy the output including the
-----BEGIN PGP SIGNATURE-----
and-----END PGP SIGNATURE-----
. - Paste it into the large text box and click the Verify button.
Telling Git about your GPG key
You will need to tell Git about your key and have it sign new commits for you.
- Open your terminal
- Type
git config --global user.signingkey <GPG KEY ID>
- Type
git config --global commit.gpgsign true
Just in case you have a similar error to the one below when signing a commit.
Then you need to execute the following command in order to add the GPG_TTY
variable to your ~/bashrc
file.
After that, log out your session (there's no need to reboot) and you will be able to sign your commits.
Attribution
This guide is derived from the Codeberg Documentation, used under CC-BY-SA 4.0.