DEV Community

Mario García
Mario García

Posted on

Backup and Restore GitHub GPG and SSH keys

GitHub

Last year I reinstalled Manjaro on my Acer Aspire E11 to try i3 after 5 years using Xfce. After that I configured my development environment for Python and Rust and also restored some configuration files.

While doing this I remembered that I had to configured Git. Many of my repositories are on GitHub and I usually sign every commit using GPG and push changes through SSH.

Here are the instructions I followed to back up and then restore my GPG & SSH keys. It could be useful if you’re moving to another computer or reinstalling operating system.

Backup

  • Copy both id_rsa and id_rsa.pub from ~/.ssh/ to a USB drive.
  • Identify the private key by executing the following command.
$ gpg --list-secret-keys --keyid-format LONG
Enter fullscreen mode Exit fullscreen mode

It will show something similar to this.

sec   4096R/3AA5C34371567BD2 2016-03-10 [expires: 2017-03-10]
Enter fullscreen mode Exit fullscreen mode

Characters after the slash are the ID of the private key.

  • Export the private key.
gpg --export-secret-keys $ID > my-private-key.asc
Enter fullscreen mode Exit fullscreen mode
  • Copy my-private-key.asc to a USB drive.

Restore

  • Copy both id_rsa and id_rsa.pub to ~/.ssh/
  • Change file permissions and ownership of both files.
$ chown user:user ~/.ssh/id_rsa*
$ chmod 600 ~/.ssh/id_rsa
$ chmod 644 ~/.ssh/id_rsa.pub
Enter fullscreen mode Exit fullscreen mode
  • Start the ssh-agent.
$ exec ssh-agent bash
Enter fullscreen mode Exit fullscreen mode
  • Add your SSH private key to the ssh-agent.
$ ssh-add ~/.ssh/id_rsa
Enter fullscreen mode Exit fullscreen mode
  • Import your GPG key
$ gpg --import my-private-key.asc
Enter fullscreen mode Exit fullscreen mode

Now you’re ready to use Git and update your repositories.

Top comments (0)