skip to content
Astro build wallpaper

Create Export Import SSH and GPG key

/ 3 min read

SSH Key

Create SSH Key

  1. Generate SSH Key with ed25519 algorithm.
    ssh-keygen -t ed25519 -C "johndoe@example.com"
    
  2. See the generated key.
    cat ~/.ssh/id_ed25519.pub
    # ssh-ed25519 AAAAC3NzaC1lZDI1NTE5AAAAIIB8J6 "johndoe@example.com"
    

Export SSH Key

  1. Export the public key.
    cat ~/.ssh/id_ed25519.pub
    # ssh-ed25519 AAAAC3NzaC1lZDI1NTE5AAAAIIB8J6 "johndoe@example.com"
    
  2. Export the private key.
    cat ~/.ssh/id_ed25519
    # -----BEGIN OPENSSH PRIVATE KEY-----
    # b3BlbnNzaC1rZXktdjEAAAAABG5vbmUAAAAEbm9uZQAAAAAAAAABAAABlwAAAAdzc2gtcnNhAAAAAwEAAQAAAYEAq3zvz
    # ...
    
  3. Copy the public key and private key to a safe place.
  4. Restart the ssh-agent.
    killall ssh-agent; eval `ssh-agent`
    

Import SSH Key

  1. Create a new file with the name id_ed25519.pub.
  2. Paste the public key to the file.
  3. Create a new file with the name id_ed25519.
  4. Paste the private key to the file.

Config SSH Key

  1. Add the following configuration to ~/.ssh/config.
    Host github.com
        Hostname github.com
        User git
        IdentityFile ~/.ssh/id_ed25519
    
  2. Test the ssh connection.
    ssh -T git@github.com
    # Hi johndoe! You've successfully authenticated, but GitHub does not provide shell access.
    

GPG Key

Create GPG Key

  1. Generate GPG Key.
    gpg --full-generate-key
    
  2. List the GPG Key.
    gpg --list-secret-keys --keyid-format=long
    # sec   4096R/3AA5C34371567BD2 2016-03-10 [expires: 2027-04-10]
    # uid                          John Doe <johndoe@example.com>
    # ssb   4096R/4BB6D45482678BE3 2016-03-10
    
  3. Show the public key.
    gpg --armor --export 3AA5C34371567BD2
    # -----BEGIN PGP PUBLIC KEY BLOCK-----
    # ...
    

Export GPG Key

  1. Export the public key.
    gpg --export --export-options backup --output public.gpg johndoe@example.com
    
  2. Export the private key.
    gpg --export-secret-keys --export-options backup --output private.gpg johndoe@example.com
    
  3. Export trust database.
    gpg --export-ownertrust > trust.gpg
    
  4. List the exported files.
    ls -hl *.gpg
    # -rw-r--r-- 1 johndoe johndoe 1.5K Apr 26 10:00 public.gpg
    # -rw-r--r-- 1 johndoe johndoe 3.5K Apr 26 10:00 private.gpg
    # -rw-r--r-- 1 johndoe johndoe  1.0 Apr 26 10:00 trust.gpg
    
  5. Copy the exported files to a safe place.

Import GPG Key

  1. Import the public key.
    gpg --import public.gpg
    
  2. Import the private key.
    gpg --import private.gpg
    
  3. Import trust database.
    gpg --import-ownertrust trust.gpg
    
  4. List the imported keys.
    gpg --list-secret-keys --keyid-format=long
    # sec   4096R/3AA5C34371567BD2 2016-03-10 [expires: 2027-04-10]
    # uid                          John Doe <johndoe@example.com>
    # ssb   4096R/4BB6D45482678BE3 2016-03-10
    

Config Git with Signing Key

  1. Set the GPG Key to git.
    git config --global user.signingkey johndoe@example.com
    
  2. Set the GPG Program to git. (Windows)
    git config --global gpg.program "C:/Program Files (x86)/GnuPG/bin/gpg.exe"
    
  3. Set the commit to sign.
    git config --global commit.gpgsign true
    
  4. Set the tag to sign.
    git config --global tag.gpgSign true
    
  5. Try to sign the commit.
    git commit -S -m "Signed commit"
    
  6. Show commit signature
    git show --show-signature -1
    # gpg: Signature made Sat 27 Apr 2024 06:40:30 AM WIB
    # gpg:                using RSA key XXXXXXXXXXXXXXXXXXXXXXX
    # gpg:                issuer "johndoe@example.com"
    # gpg: Good signature from "John Dow (johndoe GPG Key) <johndoe@example.com>" [ultimate]
    # Author: johndoe <johndoe@example.com>
    # Date:   Sat Apr 27 06:40:30 2024 +0700
    

Referece