Import and Export GPG key pair (Linux)

PROBLEM: Need to keep keys to be able restore backups later.
SOLUTION: Simple export keys and save where you want to.

1.0 – Export

Use gpg –list-keys to show all keys

user@server:~# gpg --list-keys
/user/.gnupg/pubring.kbx
------------------------
pub   rsa3072 2019-01-15 [SC] [expires: 2021-01-14]
uid           [ultimate] myFavKey
sub   rsa3072 2019-01-15 [E] [expires: 2021-01-14]

My only key is myFavKey, so I will export this one using

user@server:~# gpg --output mykey_pub.gpg --armor --export myFavKey
user@server:~# gpg --output mykey_sec.gpg --armor --export-secret-key myFavKey

And there you go! Yyou should both files export to same safe place 🙂

2.0 – Import

Copy both files to server where you would like to restore files and import both keys.

user@another-server:~# gpg --import ~/mykey_pub.gpg
user@another-server:~# gpg --allow-secret-key-import --import ~/mykey_sec.gpg

And it is done! You should erase both files with keys, just for safety purposes!

Leave a Reply

Your email address will not be published. Required fields are marked *