Skip to content

Instantly share code, notes, and snippets.

@angela-d
Created April 1, 2018 23:57
Show Gist options
  • Star 56 You must be signed in to star a gist
  • Fork 8 You must be signed in to fork a gist
  • Save angela-d/8b27670bac26e4bf7c431715fef5cc51 to your computer and use it in GitHub Desktop.
Save angela-d/8b27670bac26e4bf7c431715fef5cc51 to your computer and use it in GitHub Desktop.
Move GPG Keys from One Machine to Another

Migrate GPG Keys from One Workstation to Another

Replace [your key] with your key ID

To obtain your key ID

gpg --list-secret-keys --keyid-format LONG

Which returns something like

/home/angela/.gnupg/pubring.kbx
-------------------------------
sec   rsa4096/[your key] 2018-03-30 [SC]
      ABCDEFGHIJKLMNOPQRSTUVWXYZ
uid                 [ unknown] angela (KEY NAME) <user@domain>
ssb   rsa4096/ABCDEFGHIJKL 2018-03-30 [E]

After the key size rsa4096/ is your key ID.


Export the key in preparation to move it

gpg --export -a [your key] > gpg-pub.asc

Prepare the secret key for migration (if password protected, you'll be prompted to enter it)

gpg --export-secret-keys -a [your key] > gpg-sc.asc

Find them

ls -l gpg*.asc

Drag the key pair from the current directory to your USB stick or however else you move them.

Once on the new machine, import them

gpg --import gpg-pub.asc

If password protected, you'll be prompted to enter it

gpg --import gpg-sc.asc

If you need to adjust the trust level

gpg --edit-key [your key]
@trey
Copy link

trey commented Mar 3, 2023

Thank you both! This was a huge time saver. I was about to generate a new one.

@farazhussain
Copy link

Thank you very much!

@agowa
Copy link

agowa commented Jan 8, 2024

Just a note, this doesn't migrate all keys. If GnuPG is used as ssh-agent, it somehow allows keys within private-keys-v1.d to be included via the sshcontrol file even though ti doesn't show them to any other command including gpg --list-secret-keys --keyid-format LONG

I'm currently looking for a way to either extract them from there into a normal format...

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