DEV Community

Ridae HAMDANI
Ridae HAMDANI

Posted on

Seamless GPG Key Migration: Moving Your Keys Across Machines

I am using gpg key for a while to encrypt my files as well as sign my git commit, recently I migrated to a new virtual machine, but I come accross this challenge, how can I migrate my gpg keys to my new beast machine ?

In the following article , I will share with you how I migrated my gpg keys.

GPG Key Migration

There is a number of ways to backup or migrate your gpg keys :

Easiest way - Migrate all

The first and the easiest way is to zip and copy all the gpg folder from one machine to an other, this will move all your configuration/keys, it is a good idea when the target machine is a newly fresh machine with no gpg keys yet.

Usually the gpg configuration files are located in the directory ~/.gnupg

Migrate only specific keys

The second way is to move only a specific key.
1- Start by finding the key(s) id you want to migrate by using this command:

gpg --list-secret-keys --keyid-format LONG
Enter fullscreen mode Exit fullscreen mode

It should returns something like:

sec   rsa4096/[**YOUR KEY ID**] 2024-03-30 [SC]
      ABCDEFGHIJKLMNOPQRSTUVWXYZ
uid                 [ unknown] username (KEY NAME) <user@domain>
ssb   rsa4096/ABCDEFGHIJKL 2024-03-30 [E]
Enter fullscreen mode Exit fullscreen mode

After the key size rsa4096/ is your key ID.

2- Export the key in preparation to move it

gpg --export -a [your key id] > gpg-pub.asc
Enter fullscreen mode Exit fullscreen mode

3- 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
Enter fullscreen mode Exit fullscreen mode

4- Drag the key pair from the current directory to your USB stick or however else you move them (you can copy/past the first as they are just a text files).

5- Once on the new machine, import them

$ gpg --import gpg-pub.asc
$ gpg --import gpg-sc.asc
Enter fullscreen mode Exit fullscreen mode

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

⭐⭐⭐ Enjoy your learning….!!! ⭐⭐⭐

Support My Blog ☕️
If you enjoy my technical blog posts and find them valuable, please consider buying me a coffee at here. Your support goes a long way in helping me produce more quality articles and content. If you have any feedback or suggestions for improving my code, please leave a comment on this post or send me a message on my LinkedIn.

Top comments (0)