When you are setting up a coding environment on a new machine or has just found that your GPG keys were missing for some reasons (it really happened on my Windows PC), you may need to transfer your GPG secret keys elsewhere.
It takes advantage of gpg --export-secret-key
, gpg --import
and pipe syntax. This method is an excerpt of a StackOverflow answer.
If you're on the machine that already has the key:
gpg --export-secret-key SOMEKEYID | ssh othermachine gpg --import
If you're on the machine that needs the key:
ssh othermachine gpg --export-secret-key SOMEKEYID | gpg --import
On most of the occasions Method #1 can work well, but it failed on my Windows PC (#1 worked when I transferred keys from Windows to Linux). In another article, the transfer can be performed by a temporary private key file.
On the machine that already has the key, export the key and write it into a file:
gpg --export-secret-key SOMEKEYID > private.key
You can use file
command to check the metadata of the key:
$ file private.key
private.key: OpenPGP Secret Key Version 4, Created <date>, RSA (Encrypt or Sign, 4096 bits); User ID; Signature; OpenPGP Certificate
Then you can transfer it to the target machine. Just run the command on it:
scp othermachine:/path/to/private.key .
gpg --import private.key
Have a look into the key list:
gpg --list-secret-keys --keyid-format=long
If there isn't any problem, DO remember to delete the temporary key files.