OSDN Git Service

Regular updates
[twpd/master.git] / gnupg.md
1 ---
2 title: GnuPG
3 category: CLI
4 layout: 2017/sheet
5 tags: []
6 updated: 2017-10-22
7 weight: 0
8 intro: |
9   [GnuPG](https://gnupg.org/) is a complete and free implementation of the OpenPGP standard.
10 ---
11
12 Basics
13 ---------------
14
15 ### Exporting keys
16
17 ```bash
18 gpg -o key.gpg --export <KEY ID>
19 ```
20
21 __Export key in ASCII:__
22
23 ```bash
24 gpg -o key.asc --armor --export <KEY ID>
25 ```
26
27 __Note:__ Omitting the `-o|--output` option will print the key to `stdout`.
28
29 ### Importing keys
30
31 ```bash
32 gpg --import key.gpg
33 gpg --import key.asc
34 ```
35
36 Only merge updates for keys already in key-ring:
37
38 ```bash
39 gpg --import key.asc --merge-options merge-only
40 ```
41
42 ### Managing your keyring
43
44 Generate a new key:
45 {: .-setup}
46
47 ```bash
48 gpg --gen-key
49 # or, generate a new key with dialogs for all options
50 gpg --full-gen-key
51 ```
52
53 List public keys:
54
55 ```bash
56 gpg -k
57 gpg --list-keys
58 ```
59
60 List secret keys:
61
62 ```bash
63 gpg -K
64 gpg --list-secret-keys
65 ```
66
67
68 ### Using a keyserver
69
70 Import keys from keyserver:
71 {: .-setup}
72
73 ```bash
74 gpg --receive-keys <KEY IDS>
75 ```
76
77 Upload keys to keyserver:
78
79 ```bash
80 gpg --send-keys <KEY IDS>
81 ```
82
83 Request updates from keyserver for keys already in your keyring:
84
85 ```bash
86 gpg --refresh-keys
87 ```
88
89 Search keys from keyserver:
90
91 ```bash
92 gpg --search-keys "<SEARCH STRING>"
93 ```
94
95 Override keyserver from `~/.gnupg/gpg.conf`
96
97 ```bash
98 gpg --keyserver <URL> ...
99 ```
100
101 ### Trusting a key
102
103 ```bash
104 gpg --edit-key <KEY ID>
105 # In the interactive prompt:
106 gpg> sign
107 gpg> save
108 ```
109
110 __NOTE:__ You can use the owner's email or name (or part thereof) instead of the key ID for `--edit-key`
111
112
113 Encrypting
114 ---------
115 {: .-two-column}
116
117 ### Public key encryption
118 This will produce an encrypted file, `secret.txt.gpg`, that can only be decrypted by the recipient:
119
120 ```bash
121 gpg -e -o secret.txt.gpg -r <RECIPIENT> secret.txt
122 ```
123
124 For `<RECIPIENT>` you can use their key ID, their email, or their name (or part thereof).
125
126 ```bash
127 gpg -e -r <KEY ID> ...
128 gpg -e -r "Bez" ...
129 gpg -e -r "bezalelhermoso@gmail.com" ...
130 ```
131
132 Specifying multiple recipients
133
134 ```bash
135 gpg -e -r <RECIPIENT> -r <ANOTHER RECIPIENT> ... secret.txt
136 ```
137
138 __NOTE__: Omitting `-o|--output` will produce an encrypted file named `<ORIGINAL FILENAME>.gpg` by default.
139
140 ### Symmetric encryption
141
142 Encrypt file using a shared key. You will be prompted for a passphrase.
143
144 ```bash
145 gpg --symmetric secret.txt
146 # or
147 gpg -c secret.txt
148 ```
149
150 Decrypting
151 ---------
152 {: .-one-column}
153
154 ### Decrypting a file
155
156 ```bash
157 gpg -d -o secret.txt secret.txt.gpg
158 ```
159
160 If the file is encrypted via symmetric encryption, you will be prompted for the passphrase.
161
162 __NOTE__: Omitting `-o|--output` will print the unencrypted contents to `stdout`
163
164 Signing & Verifying
165 ---------
166 {: .-two-column}
167
168 ### Signing
169
170 ```bash
171 gpg -o signed-file.txt.gpg -s file.txt
172 ```
173
174 This can be used during encryption to also sign encrypted files:
175
176 ```bash
177 gpg -s -o secret.txt.gpg \
178   -r <RECIPIENT> secret.txt
179 ```
180
181 ### Verifying a signature
182
183 ```bash
184 gpg --verify file.txt.gpg
185 ```
186
187 ### Viewing content of signed file
188
189 ```bash
190 gpg -d signed-file.txt.gpg
191 ```
192
193 Miscellaneous
194 ----------
195 {: .-two-column}
196
197 ### Components
198
199 List all components:
200 {: .-setup}
201
202 ```bash
203 gpgconf --list-components
204 ```
205
206 Kill a component:
207
208 ```bash
209 gpgconf --kill <COMPONENT> # i.e. gpgconf --kill dirmngr
210 ```
211
212 Kill all components:
213 ```bash
214 gpgconf --kill all
215 ```
216
217 ### Parsing keyring data
218
219 Use `--with-colons` to produce an output that can easily be parsed i.e. with `awk`, `grep`. Fields are colon-separated.
220
221 ```bash
222 gpg -k --with-colons
223 ```
224
225 Field Quick Reference:
226
227 | Field # | Description |
228 | 1       | Record type |
229 | 2       | Validity |
230 | 3       | Key length in bits |
231 | 4       | Public key algorithm |
232 | 5       | Key ID |
233 | 6       | Creation date |
234 | 7       | Expiry date |
235 | 8       | Certificate S/N, UID hash, trust signature info |
236 | 9       | Ownertrust |
237 | 10      | User ID |
238 | 11      | Signature class |
239 | 12      | Key capabilities |
240 | 13      | Issuer fingerprint |
241 | 14      | Flag field |
242 | 15      | S/N of token |
243 | 16      | Hash algorithm |
244 | 17      | Curve name |
245 | 18      | Compliance flags |
246 | 19      | Last update timestamp |
247 | 20      | Origin |
248
249 See [GnuPG Details](https://git.gnupg.org/cgi-bin/gitweb.cgi?p=gnupg.git;a=blob_plain;f=doc/DETAILS) for more details.
250
251