If you develop software on Ubuntu Linux or commit code to GitHub, you likely use a GPG (GNU Privacy Guard) key to cryptographically sign your work. This key proves that the code came from you and hasn’t been tampered with. If your laptop’s hard drive fails and you lose this key, you can never again prove your identity to the open-source community.
Your GPG key is actually split into two parts: a “public” key (which you share with the world) and a “private” key (which is locked inside your machine). Before disaster strikes, you must safely extract the highly sensitive private key and back it up to an offline USB drive.
How to Safely Extract an Ubuntu GPG Private Key
You must use the `gpg` command-line utility to export the secret cryptographic block.
Warning: Anyone who obtains your private key can mathematically impersonate you. Never email this file or upload it to cloud storage.
- Open your Terminal application (Ctrl + Alt + T).
- First, you must find the exact 16-character ID of your private key. Type this command and press Enter:
gpg --list-secret-keys --keyid-format=long - The terminal will output a block of text. Look for the line starting with
sec. The alphanumeric string directly following the slash (e.g.,rsa4096/3AA5C34371567BD2) is your Key ID. In this example, the ID is 3AA5C34371567BD2. - To extract the raw private key and save it to a file on your desktop, carefully type the following command (replacing the ID with your own):
gpg --export-secret-keys -a 3AA5C34371567BD2 > ~/Desktop/my_private_key.asc - Press Enter. The system will likely prompt you to type the passphrase you created when you first generated the key.
Look at your Ubuntu desktop. You will see a new text file named my_private_key.asc. If you open it, you will see a massive block of text starting with -----BEGIN PGP PRIVATE KEY BLOCK-----. Instantly move this file to an encrypted USB flash drive and delete it from your computer. You have successfully backed up your digital identity.