cheatsheets/gnupg.md

251 lines
4.1 KiB
Markdown
Raw Permalink Normal View History

2017-10-19 00:22:27 +00:00
---
title: GnuPG
category: CLI
tags: []
2020-07-04 13:33:09 +00:00
updated: 2017-10-22
2017-10-19 00:22:27 +00:00
weight: 0
intro: |
[GnuPG](https://gnupg.org/) is a complete and free implementation of the OpenPGP standard.
---
Basics
---------------
### Exporting keys
```bash
gpg -o key.gpg --export <KEY ID>
```
2017-10-19 01:05:35 +00:00
__Export key in ASCII:__
2017-10-19 00:22:27 +00:00
```bash
gpg -o key.asc --armor --export <KEY ID>
```
__Note:__ Omitting the `-o|--output` option will print the key to `stdout`.
### Importing keys
```bash
gpg --import key.gpg
gpg --import key.asc
```
2017-10-19 01:05:35 +00:00
Only merge updates for keys already in key-ring:
2017-10-19 00:22:27 +00:00
```bash
2022-11-01 03:30:02 +00:00
gpg --import key.asc --import-options merge-only
2017-10-19 00:22:27 +00:00
```
### Managing your keyring
2017-10-19 01:05:35 +00:00
Generate a new key:
{: .-setup}
2017-10-19 00:22:27 +00:00
```bash
gpg --gen-key
# or, generate a new key with dialogs for all options
gpg --full-gen-key
```
2017-10-19 01:05:35 +00:00
List public keys:
2017-10-19 00:22:27 +00:00
```bash
gpg -k
gpg --list-keys
```
2017-10-19 01:05:35 +00:00
List secret keys:
2017-10-19 00:22:27 +00:00
```bash
gpg -K
gpg --list-secret-keys
```
### Using a keyserver
2017-10-19 01:05:35 +00:00
Import keys from keyserver:
{: .-setup}
2017-10-19 00:22:27 +00:00
```bash
gpg --receive-keys <KEY IDS>
```
2017-10-19 01:05:35 +00:00
Upload keys to keyserver:
2017-10-19 00:22:27 +00:00
```bash
gpg --send-keys <KEY IDS>
```
2017-10-19 01:05:35 +00:00
Request updates from keyserver for keys already in your keyring:
2017-10-19 00:22:27 +00:00
```bash
gpg --refresh-keys
```
2017-10-19 01:05:35 +00:00
Search keys from keyserver:
2017-10-19 00:22:27 +00:00
```bash
gpg --search-keys "<SEARCH STRING>"
```
2017-10-19 01:05:35 +00:00
Override keyserver from `~/.gnupg/gpg.conf`
2017-10-19 00:22:27 +00:00
```bash
gpg --keyserver <URL> ...
```
2017-10-19 01:05:35 +00:00
### Trusting a key
```bash
gpg --edit-key <KEY ID>
# In the interactive prompt:
gpg> trust
2017-10-19 02:06:31 +00:00
gpg> save
2017-10-19 01:05:35 +00:00
```
__NOTE:__ You can use the owner's email or name (or part thereof) instead of the key ID for `--edit-key`
2017-10-19 00:22:27 +00:00
Encrypting
---------
{: .-two-column}
### Public key encryption
This will produce an encrypted file, `secret.txt.gpg`, that can only be decrypted by the recipient:
```bash
gpg -e -o secret.txt.gpg -r <RECIPIENT> secret.txt
```
For `<RECIPIENT>` you can use their key ID, their email, or their name (or part thereof).
```bash
gpg -e -r <KEY ID> ...
gpg -e -r "Bez" ...
gpg -e -r "bezalelhermoso@gmail.com" ...
```
2017-10-19 01:05:35 +00:00
Specifying multiple recipients
2017-10-19 00:22:27 +00:00
```bash
gpg -e -r <RECIPIENT> -r <ANOTHER RECIPIENT> ... secret.txt
```
__NOTE__: Omitting `-o|--output` will produce an encrypted file named `<ORIGINAL FILENAME>.gpg` by default.
### Symmetric encryption
Encrypt file using a shared key. You will be prompted for a passphrase.
```bash
gpg --symmetric secret.txt
# or
gpg -c secret.txt
```
Decrypting
---------
{: .-one-column}
### Decrypting a file
```bash
gpg -d -o secret.txt secret.txt.gpg
```
If the file is encrypted via symmetric encryption, you will be prompted for the passphrase.
__NOTE__: Omitting `-o|--output` will print the unencrypted contents to `stdout`
Signing & Verifying
---------
{: .-two-column}
### Signing
```bash
gpg -o signed-file.txt.gpg -s file.txt
```
2017-10-19 01:05:35 +00:00
This can be used during encryption to also sign encrypted files:
2017-10-19 00:22:27 +00:00
```bash
gpg -s -o secret.txt.gpg \
-r <RECIPIENT> secret.txt
```
### Verifying a signature
```bash
gpg --verify file.txt.gpg
```
### Viewing content of signed file
```bash
gpg -d signed-file.txt.gpg
```
Miscellaneous
----------
{: .-two-column}
### Components
List all components:
{: .-setup}
```bash
gpgconf --list-components
```
Kill a component:
```bash
gpgconf --kill <COMPONENT> # i.e. gpgconf --kill dirmngr
```
Kill all components:
```bash
gpgconf --kill all
```
### Parsing keyring data
2017-10-19 01:05:35 +00:00
Use `--with-colons` to produce an output that can easily be parsed i.e. with `awk`, `grep`. Fields are colon-separated.
2017-10-19 00:22:27 +00:00
```bash
gpg -k --with-colons
```
Field Quick Reference:
2017-10-19 01:05:35 +00:00
| Field # | Description |
| 1 | Record type |
| 2 | Validity |
| 3 | Key length in bits |
2017-10-22 11:08:21 +00:00
| 4 | Public key algorithm |
2017-10-19 01:05:35 +00:00
| 5 | Key ID |
| 6 | Creation date |
| 7 | Expiry date |
2017-10-22 11:08:21 +00:00
| 8 | Certificate S/N, UID hash, trust signature info |
2017-10-19 01:05:35 +00:00
| 9 | Ownertrust |
| 10 | User ID |
| 11 | Signature class |
| 12 | Key capabilities |
| 13 | Issuer fingerprint |
| 14 | Flag field |
| 15 | S/N of token |
| 16 | Hash algorithm |
| 17 | Curve name |
| 18 | Compliance flags |
| 19 | Last update timestamp |
| 20 | Origin |
2017-10-19 00:22:27 +00:00
See [GnuPG Details](https://git.gnupg.org/cgi-bin/gitweb.cgi?p=gnupg.git;a=blob_plain;f=doc/DETAILS) for more details.
2017-10-19 00:22:27 +00:00