Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to get GPG public key in bash?

Tags:

bash

I need to get the public key ID from GPG. Everything on SO discusses importing, exporting, etc. In the example below, I need to get the ABCD1234 value to use in a bash script?

$ gpg --list-keys  
/Users/jblow/.gnupg/pubring.gpg  
---------------------------------  
pub   2048R/ABCD1234 2016-09-20
uid       [ultimate] Joe Blow <[email protected]>
sub   2048R/JDKKHU76 2016-09-20
like image 525
G. Deward Avatar asked Sep 08 '25 09:09

G. Deward


1 Answers

I was facing the same requirement today (extracting the key ID in order to use it together with duplicity in a bash script).

In the man page of gpg I read:

For scripted or other unattended use of gpg make sure to use the machine-parseable interface and not the default interface which is intended for direct use by humans. The machine-parseable inter- face provides a stable and well documented API independent of the locale or future changes of gpg. To enable this interface use the options --with-colons and --status-fd. For certain operations the option --command-fd may come handy too. See this man page and the file `DETAILS' for the specifi- cation of the interface.

I managed to extract the key ID I wanted by doing:

gpg --list-signatures --with-colons | grep 'sig' | grep 'the Name-Real of my key' | head -n 1 | cut -d':' -f5

PS: https://devhints.io/gnupg helped ;)

like image 123
eyettea Avatar answered Sep 10 '25 01:09

eyettea