Debian/Ubuntu Tips and Tricks


Debian/Ubuntu Tips and Tricks

How-To: Import/Export GPG key pair

Posted by chantra on April 15th, 2008

This tutorial will show how you can export and import a set of GPG keys from one computer to another.
This way, you can sign/encrypt the same way one different computer.

A simple way of doing it would be to:

$ scp -r ~/.gnupg user@remotehost:~/

but this would import all your keyring.

If you want to import only one set of key, you first have to get the listing of your keys and find the one you want to export:

1. Export the GPG key

$gpg --list-keys
pub 1024D/ABCDFE01 2008-04-13
uid firstname lastname (description)
sub 2048g/DEFABC01 2008-04-13

In this case, I am going to import key ABCDFE01.

$ gpg --output mygpgkey_pub.gpg --armor --export ABCDFE01
$ gpg --output mygpgkey_sec.gpg --armor --export-secret-key ABCDFE01

Then copy thos files over to the remote host:

$ scp mygpgkey_pub.gpg mygpgkey_sec.gpg user@remotehost:~/

2. Import the GPG key

Now, log in the remote host:

$ ssh user@remotehost

And then import:

user@remotehost:~$ gpg --import ~/mygpgkey_pub.gpg
user@remotehost:~$ gpg --allow-secret-key-import --import ~/mygpgkey_sec.gpg

user@remotehost:~$ gpg --list-keys
pub 1024D/ABCDFE01 2008-04-13
uid firstname lastname (description)
sub 2048g/DEFABC01 2008-04-13

and then clean up:

user@remotehost:~$ rm ~/mygpgkey_sec.gpg ~/mygpgkey_pub.gpg

That's it :)

4 Responses to “How-To: Import/Export GPG key pair”

  1. You could also use redirect the output of your local gpg process to the input of a remote gpg process like this.

    $ gpg --export ABCDFE01 | ssh -l user remotehost 'gpg --import'

  2. Oops ... forgot to say why this was problematic. The encryption/decryption routines must be identical from machine to machine. This confirmation prompt appears on one machine - but not on the other. So I need to be able to either suppress all prompts ... or pass in the "y" and cause it to be disregarded on the machine which doesn't prompt. Ideas?

Leave a Reply

XHTML: You can use these tags: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>