Skip to content

Instantly share code, notes, and snippets.

@LittleYe233
Last active March 27, 2022 04:01
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save LittleYe233/ee3f54c21ae8f4fd6da7b4c06b50e7c6 to your computer and use it in GitHub Desktop.
Save LittleYe233/ee3f54c21ae8f4fd6da7b4c06b50e7c6 to your computer and use it in GitHub Desktop.
Transfer GPG secret keys to another machine (Linux/Windows)

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.

Method #1: Transfer directly

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

Method #2: Use a temporary private key file

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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment