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.
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
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]
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
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
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
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)