190 lines
6.5 KiB
Markdown
190 lines
6.5 KiB
Markdown
# Git with GPG
|
|
|
|
## install
|
|
|
|
`sudo apt install gnupg`
|
|
|
|
## Сгенерировать ключи
|
|
|
|
Опции
|
|
|
|
1. RSA & RSA
|
|
2. 4096
|
|
3. реальное имя
|
|
4. адрес почты, используемый на гитхабе
|
|
5. пароль на ключи
|
|
|
|
```bash
|
|
artem@artem-tb:~$ gpg --full-generate-key
|
|
gpg (GnuPG) 2.4.4; Copyright (C) 2024 g10 Code GmbH
|
|
This is free software: you are free to change and redistribute it.
|
|
There is NO WARRANTY, to the extent permitted by law.
|
|
|
|
gpg: directory '/home/artem/.gnupg' created
|
|
gpg: keybox '/home/artem/.gnupg/pubring.kbx' created
|
|
Please select what kind of key you want:
|
|
(1) RSA and RSA
|
|
(2) DSA and Elgamal
|
|
(3) DSA (sign only)
|
|
(4) RSA (sign only)
|
|
(9) ECC (sign and encrypt) *default*
|
|
(10) ECC (sign only)
|
|
(14) Existing key from card
|
|
Your selection? 1
|
|
RSA keys may be between 1024 and 4096 bits long.
|
|
What keysize do you want? (3072) 4096
|
|
Requested keysize is 4096 bits
|
|
Please specify how long the key should be valid.
|
|
0 = key does not expire
|
|
<n> = key expires in n days
|
|
<n>w = key expires in n weeks
|
|
<n>m = key expires in n months
|
|
<n>y = key expires in n years
|
|
Key is valid for? (0) 4m
|
|
Key expires at Tue 16 Feb 2027 05:20:36 PM MSK
|
|
Is this correct? (y/N) y
|
|
|
|
GnuPG needs to construct a user ID to identify your key.
|
|
|
|
Real name: Artem Trunov
|
|
Email address: datamove@gmail.com
|
|
Comment: gpgkey1
|
|
You selected this USER-ID:
|
|
"Artem Trunov (gpgkey1) <datamove@gmail.com>"
|
|
|
|
Change (N)ame, (C)omment, (E)mail or (O)kay/(Q)uit? O
|
|
We need to generate a lot of random bytes. It is a good idea to perform
|
|
some other action (type on the keyboard, move the mouse, utilize the
|
|
disks) during the prime generation; this gives the random number
|
|
generator a better chance to gain enough entropy.
|
|
We need to generate a lot of random bytes. It is a good idea to perform
|
|
some other action (type on the keyboard, move the mouse, utilize the
|
|
disks) during the prime generation; this gives the random number
|
|
generator a better chance to gain enough entropy.
|
|
gpg: /home/artem/.gnupg/trustdb.gpg: trustdb created
|
|
gpg: directory '/home/artem/.gnupg/openpgp-revocs.d' created
|
|
gpg: revocation certificate stored as '/home/artem/.gnupg/openpgp-revocs.d/4307B923877023C087AFB08286EA6415A234F542.rev'
|
|
public and secret key created and signed.
|
|
|
|
pub rsa4096 2026-10-19 [SC] [expires: 2027-02-16]
|
|
4307B923877023C087AFB08286EA6415A234F542
|
|
uid Artem Trunov (gpgkey1) <datamove@gmail.com>
|
|
sub rsa4096 2026-10-19 [E] [expires: 2027-02-16]
|
|
|
|
|
|
```
|
|
|
|
### посмотреть ID ключа
|
|
|
|
```bash
|
|
artem@artem-tb:~$ gpg --list-secret-keys --keyid-format=long
|
|
gpg: checking the trustdb
|
|
gpg: marginals needed: 3 completes needed: 1 trust model: pgp
|
|
gpg: depth: 0 valid: 1 signed: 0 trust: 0-, 0q, 0n, 0m, 0f, 1u
|
|
gpg: next trustdb check due at 2027-02-16
|
|
/home/artem/.gnupg/pubring.kbx
|
|
------------------------------
|
|
sec rsa4096/86EA6415A234F542 2026-10-19 [SC] [expires: 2027-02-16]
|
|
4307B923877023C087AFB08286EA6415A234F542
|
|
uid [ultimate] Artem Trunov (gpgkey1) <datamove@gmail.com>
|
|
ssb rsa4096/C1215132348CE608 2026-10-19 [E] [expires: 2027-02-16]
|
|
```
|
|
|
|
идентификатор - в стороке, начинающийсяя с sec, e.g. 86EA6415A234F542
|
|
|
|
## Конфигурация git на сервере
|
|
|
|
```bash
|
|
git config --global user.signingkey ВАШ ID
|
|
```
|
|
|
|
опция автоматически подписывать все коммиты
|
|
|
|
```bash
|
|
git config --global commit.gpgsign true
|
|
```
|
|
|
|
## Добавление ключа в гит
|
|
|
|
```bash
|
|
artem@artem-tb:~$ gpg --armor --export 86EA6415A234F542
|
|
-----BEGIN PGP PUBLIC KEY BLOCK-----
|
|
|
|
mQINBGrWJ4YBEADKiI3o5lTQo6PFgWsNW0IZv81lmMRCMDz7DtmUNAd74QN/FjcA
|
|
Yqdwv/bqNzec8dtbSMOZJov029+tUsHR+X4A41KDZWRyJm6FtwYQfH9VL3tI2ru+
|
|
...
|
|
```
|
|
|
|
скопируйте в буфер обмена и вставьте в разделе Settings->SSH and GPG Keys->New GPG Key в вашеv профилt на гитхабе.
|
|
|
|
## Проверяем
|
|
|
|
сделайте изменение, git теперь должен предложить вам ввести пароль.
|
|
|
|
|
|
Если вы получили ошибку вроде этой:
|
|
|
|
```bash
|
|
datamove@magisterslinux:~/practice-repo2$ git commit -S -m "test gpg 4" -a
|
|
error: gpg failed to sign the data:
|
|
[GNUPG:] KEY_CONSIDERED E9B7F602D5E2ECAD23928CF32091915CC4EB3A74 2
|
|
[GNUPG:] BEGIN_SIGNING H10
|
|
[GNUPG:] PINENTRY_LAUNCHED 215511 curses 1.2.1 - xterm-256color - - 1041/1003 -
|
|
gpg: signing failed: Inappropriate ioctl for device
|
|
[GNUPG:] FAILURE sign 83918950
|
|
gpg: signing failed: Inappropriate ioctl for device
|
|
|
|
fatal: failed to write commit object
|
|
```
|
|
|
|
это потому что гит не смог так же просто вывести окно для ввода пароля, как сам gpg сделал это псевдографикой на вашем терминале. Лечится конфигурацией:
|
|
|
|
```bash
|
|
export GPG_TTY=$(tty)
|
|
```
|
|
|
|
после этого можно добавить установку этой переменной среды автоматически при запуске оболочки:
|
|
|
|
```bash
|
|
echo 'export GPG_TTY=$(tty)' >> ~/.bashrc
|
|
```
|
|
|
|
## проверка
|
|
|
|
локально
|
|
|
|
```bash
|
|
datamove@magisterslinux:~/practice-repo2$ git log --show-signature --oneline
|
|
0a2dc97 (HEAD -> main, origin/main, origin/HEAD) gpg: Signature made Mon Oct 19 15:26:18 2026 UTC
|
|
gpg: using RSA key E9B7F602D5E2ECAD23928CF32091915CC4EB3A74
|
|
gpg: Good signature from "Artem Trunov (gpgkey2) <datamove@gmail.com>" [ultimate]
|
|
test gpg 4
|
|
```
|
|
|
|
либо неверифицированный коммит
|
|
|
|
```bash
|
|
46add3a test gpg 3
|
|
56b61f6 gpg: Signature made Mon Oct 19 15:00:44 2026 UTC
|
|
gpg: using RSA key 4307B923877023C087AFB08286EA6415A234F542
|
|
gpg: Can't check signature: No public key
|
|
```
|
|
если он сделан на другой машине и подписан другим приватный ключем GPG. Можно добавить публичный ключ с того компьютера на этот и коммит будет верифицироваться.
|
|
|
|
После push ваши коммиты видны на сайте как верифицированные:
|
|
|
|

|
|
|
|
|
|
## GPG agent
|
|
|
|
так как как в случае с SSH вы можете не вводить пароль каждый раз, агент запомнит его!
|
|
|
|
|
|
```bash
|
|
ps aux | grep gpg-agent
|
|
gpgconf --kill gpg-agent
|
|
gpg-connect-agent reloadagent /bye
|
|
```
|
|
|