How to export a GPG private key and public key to a file

219,212

Solution 1

Export Public Key

This command will export an ascii armored version of the public key:

gpg --output public.pgp --armor --export username@email

Export Secret Key

This command will export an ascii armored version of the secret key:

gpg --output private.pgp --armor --export-secret-key username@email


Security Concerns, Backup, and Storage

A PGP public key contains information about one's email address. This is generally acceptable since the public key is used to encrypt email to your address. However, in some cases, this is undesirable.

For most use cases, the secret key need not be exported and should not be distributed. If the purpose is to create a backup key, you should use the backup option:

gpg --output backupkeys.pgp --armor --export-secret-keys --export-options export-backup user@email

This will export all necessary information to restore the secrets keys including the trust database information. Make sure you store any backup secret keys off the computing platform and in a secure physical location.

If this key is important to you, I recommend printing out the key on paper using paperkey. And placing the paper key in a fireproof/waterproof safe.

Public Key Servers

In general, it's not advisable to post personal public keys to key servers. There is no method of removing a key once it's posted and there is no method of ensuring that the key on the server was placed there by the supposed owner of the key.

It is much better to place your public key on a website that you own or control. Some people recommend keybase.io for distribution. However, that method tracks participation in various social and technical communities which may not be desirable for some use cases.

For the technically adept, I personally recommend trying out the webkey domain level key discovery service.

Solution 2

  • List the keys you have: gpg --list-secret-keys
  • Export the key: gpg --export-secret-key name > ~/my-key.asc
  • Copy it on another machine;
  • Import the key: gpg --import my-key.asc

Solution 3

To export SOMEKEYID public key to an output file:

gpg --output public.pgp --export SOMEKEYID

When working with secret keys it's generally preferable not to write them to files and, instead, use SSH to copy them directly between machines using only gpg and a pipe:

gpg --export-secret-key SOMEKEYID | ssh othermachine gpg --import

If you must, however, output your secret key to a file please make sure it's encrypted. Here's how to accomplish that using AES encryption using the Dark Otter approach:

gpg --output public.gpg --export SOMEKEYID && \
gpg --output - --export-secret-key SOMEKEYID |\
    cat public.gpg - |\
    gpg --armor --output keys.asc --symmetric --cipher-algo AES256

The last approach is ideal if you want to create a physical back-up of your public and private keys to safeguard against a disk failure when no other way exists to regain access to your keys.

Note: If you only have a copy of your private key but not your public key it is possible to recovery your public key by reimporting the private key, trusting it, and then re-exporting.

See Moving GPG Keys Privately for additional considerations.

Share:
219,212

Related videos on Youtube

rocky
Author by

rocky

Updated on September 18, 2022

Comments

  • rocky
    rocky 8 months

    I have generated keys using GPG, by executing the following command

    gpg --gen-key
    

    Now I need to export the key pair to a file; i.e., private and public keys to private.pgp and public.pgp, respectively. How do I do it?

    • robertspierre
      robertspierre over 2 years
      P.S. in GPG 2 please use gpg --full-generate-key to have a full interface for generating keys
  • Funkwecker
    Funkwecker over 4 years
    Is the exported key (second command) encrypted or do I need to encrypt it by myself before storing it on a.g. a USB drive?
  • RubberStamp
    RubberStamp over 4 years
    @Julian ... The exported secret key has the same protection as the secret key that was exported. If there was a passphrase, the passphrase is required to import the secret key.
  • OMGtechy
    OMGtechy almost 4 years
    I made a backup using the above method, but foolishly forgot to test it. Unless I'm missing something, I can't seem to recover my public key from the backup method specified (--export-options export-backup, etc). Am I missing something, or did I misunderstand the kind of backup it was making?
  • jarno
    jarno over 3 years
    The documentation knows --export-secret-keys, but not --export-secret-key.
  • jarno
    jarno over 3 years
    @OMGtechy How did you try to recover the key(s)? I could restore public keys by gpg --import-options restore --import backupkeys.pgp, but that does not restore secret keys, only the public ones, if backupkeys.pgp was created by gpg --output backupkeys.pgp --armor --export --export-options export-backup. In that --armor is not necessary and export-backup could be replaced by backup.
  • Weihang Jian
    Weihang Jian almost 3 years
    Note that .asc stands for ASCII, but the output of gpg --list-secret-keys is binary.
  • Zoe stands with Ukraine
    Zoe stands with Ukraine almost 3 years
    Note that Keybase has since been bought by Zoom, who have very close ties to China.
  • Anshu
    Anshu over 2 years
    Actually, .asc is for ASCII armored and the output is enciphered text. You can safely cat it and see for yourself. Also, like most linux files, the file extension is also arbitrary, doesn't technically have to be asc. @WeihangJian
  • Anshu
    Anshu over 2 years
    It would be a good idea to remove the key file after it is imported and tests successfully. If the file is sitting there it could be used maliciously.
  • wheeler
    wheeler over 2 years
    Does the backup option also export subkeys?
  • chovy
    chovy over 2 years
    how do i get this thing on my iphone from linux?
  • robertspierre
    robertspierre over 2 years
    @jarno On my GPG 2 both --export-secret-key and --export-secret-keys work
  • robertspierre
    robertspierre over 2 years
    I have tried gpg --output backupkeys.pgp --armor --export-secret-keys --export-options export-backup but only the private key is exported
  • nisc
    nisc almost 2 years
    What do keyserver operators do when there's a GDPR Right-to-be-Forgotten request?
  • ianpojman
    ianpojman over 1 year
    For those looking at how to import the exported backup file, this worked for me: gpg --import-options restore --import ./backupkeys.pgp
  • Y00
    Y00 over 1 year
    simply use -a option will export it in ASCII format, gpg -a --export-secret-keys name
  • Max Barraclough
    Max Barraclough about 1 year
    I was able to restore from private.pgp using gpg --import ./private.gpg, as shown in another answer. If someone reading this is able to, I suggest editing the answer to include this.
  • Admin
    Admin 10 months
    Does the secret key also include the public key?