Importing GnuPG Keys From an Existing .gnupg Folder

Many of us computery people, myself included, use GNU Privacy Guard‘s (GnuPG’s) implementation of Pretty Good Privacy (PGP) to do things like signing and encrypting files on our systems. I have some mixed opinions about PGP and just how practical webs of trust have proven to be in 2020, but I’m setting them aside for this short post.

I recently installed elementaryOS on my home desktop, and I’m loving it so far as a daily driver. One hitch I had, though, and also the topic of this post, was importing my PGP private key that I use for signing my git commits (2DEC0103). This was a time where I was annoyed with my past self, as I had done a pretty ham-fisted job with backing up data from my last OS. I basically just shoveled files and folders onto another hard drive, and I didn’t make any effort to export my private keys. I just threw the whole .gnupg folder in there! (I have other copies of my key, but they’re inconvenient to access.)

I said this would be a short post, so let’s just jump to the command.

gpg --export-secret-keys --homedir=/path/to/another/.gnupg | gpg --import

This command uses the flag --homedir, which was the crucial bit here. I spent a good half hour looking around for an exact solution to my conundrum, but ultimately the GnuPG documentation came through (imagine that 🤦‍♂️). This flag specifies an alternative .gnupg folder to use for that particular GnuPG process. In total, the command says to export all secret keys from /path/to/another/.gnupg and import them defaultly (probably to ~/.gnupg).

By the way, defaultly needs to be a word. I’ve had to use it a few times recently…

Leave a Reply

Your email address will not be published. Required fields are marked *