How to enable SSH access using a GPG key for authentication (2024)

Many of us are familiar with Secure Shell (SSH), which allows us to connect to other systems using a key instead of a password. This guide will explain how to eliminate SSH keys and use a GNU Privacy Guard (GPG) subkey instead.

Using GPG does not make your SSH connections more secure. SSH is a secure protocol, and SSH keys are secure. Instead, it makes certain forms of key distribution and backup management easier. It also will not change your workflow for using SSH. All commands will continue to work as you expect, except that you will no longer have SSH private keys and you will unlock your GPG key instead.

By having SSH authenticated by your GPG key, you will reduce the number of key files you need to secure and back up. This means that your key management hygiene still has to be good, which means choosing good passphrases and using appropriate key preservation strategies. Remember, you shouldn't back your private key up to the cloud!

Additionally, today SSH keys are distributed by hand and oftentimes directly. If you want to grant me access to a machine, you have to ask me for my SSH key. You may get lucky and find one posted on my website. However, you still have to decide if you trust my website. If I use a GPG key for SSH, you can select a known, good key for me using the GPG web of trust from a public keyserver. This is what The Monkeysphere Project is working on. Otherwise, nothing you do here affects the web of trust used for GPG encryption and signing.

What is a GPG subkey?

A GPG key is actually a collection of keys. There is one primary key, which is typically used only for signing and certification. The suggested usage of GPG is to create a subkey for encryption. This subkey is a separate key that, for all intents and purposes, is signed by your primary key and transmitted at the same time. This practice allows you to revoke the encryption subkey on its own, such as if it becomes compromised, while keeping your primary key valid.

The important thing to realize is that a GPG key contains multiple keys. For backup and storage purposes, you can operate them as though they are one key, but when it is time to use a key, you can use them independently.

This exercise will use a subkey that has been created for authentication to complete SSH connections. This authentication subkey will completely replace the keypair you may have generated in the past with ssh key-gen. You can create as many of these as you want if you need multiple SSH keys.

Create an authentication subkey

You should already have a GPG key. If you don't, read one of the many fine tutorials available on this topic. You will create the subkey by editing your existing key. You need to edit your key in expert mode to get access to the appropriate options.

The workflow adds a new key where you can choose its capabilities—specifically, you want to toggle its capabilities to just have authentication. SSH typically uses a 2048-bit RSA key that does not expire (type 8 in the options below).

Below is an edited version of the workflow. This and all other commands were tested on Fedora 29.

$ gpg2 --expert --edit-key <KEY ID>gpg> addkeyPlease select what kind of key you want: (3) DSA (sign only) (4) RSA (sign only) (5) Elgamal (encrypt only) (6) RSA (encrypt only) (7) DSA (set your own capabilities) (8) RSA (set your own capabilities) (10) ECC (sign only) (11) ECC (set your own capabilities) (12) ECC (encrypt only) (13) Existing keyYour selection? 8Possible actions for a RSA key: Sign Encrypt Authenticate Current allowed actions: Sign Encrypt (S) Toggle the sign capability (E) Toggle the encrypt capability (A) Toggle the authenticate capability (Q) FinishedYour selection? sYour selection? eYour selection? aPossible actions for a RSA key: Sign Encrypt Authenticate Current allowed actions: Authenticate (S) Toggle the sign capability (E) Toggle the encrypt capability (A) Toggle the authenticate capability (Q) FinishedYour selection? qRSA keys may be between 1024 and 4096 bits long.What keysize do you want? (2048) Requested keysize is 2048 bitsPlease specify how long the key should be valid. 0 = key does not expire <n> = key expires in n days <n>w = key expires in n weeks <n>m = key expires in n months <n>y = key expires in n yearsKey is valid for? (0) Key does not expire at allIs this correct? (y/N) yReally create? (y/N) ysec rsa2048/8715AF32191DB135 created: 2019-03-21 expires: 2021-03-20 usage: SC trust: ultimate validity: ultimatessb rsa2048/150F16909B9AA603 created: 2019-03-21 expires: 2021-03-20 usage: E ssb rsa2048/17E7403F18CB1123 created: 2019-03-21 expires: never usage: A [ultimate] (1). Brian Exelbierdgpg> quitSave changes? (y/N) y

Enable the GPG subkey

When you use SSH, a program called ssh-agent is used to manage the keys. To use a GPG key, you'll use a similar program, gpg-agent, that manages GPG keys. To get gpg-agent to handle requests from SSH, you need to enable support by adding the line enable-ssh-support to the ~/.gnupg/gpg-agent.conf.

$ cat .gnupg/gpg-agent.conf enable-ssh-support

Optionally, you may want to pre-specify the keys to be used for SSH so you won't have to use ssh-add to load the keys. To do this, specify the keys in the ~/.gnupg/sshcontrol file. The entries in this file are keygrips—internal identifiers gpg-agent uses to refer to keys. Unlike a key hash, a keygrip refers to both the public and private key. To find the keygrip, use gpg2 -K --with-keygrip, as shown below. Then add that line to the sshcontrol file.

$ gpg2 -K --with-keygrip /home/bexelbie/.gnupg/pubring.kbx------------------------------sec rsa2048 2019-03-21 [SC] [expires: 2021-03-20] 96F33EA7F4E0F7051D75FC208715AF32191DB135 Keygrip = 90E08830BC1AAD225E657AD4FBE638B3D8E50C9Euid [ultimate] Brian Exelbierdssb rsa2048 2019-03-21 [E] [expires: 2021-03-20] Keygrip = 5FA04ABEBFBC5089E50EDEB43198B4895BCA2136ssb rsa2048 2019-03-21 [A] Keygrip = 7710BA0643CC022B92544181FF2EAC2A290CDC0E$ echo 7710BA0643CC022B92544181FF2EAC2A290CDC0E >> ~/.gnupg/sshcontrol

Last, you need to tell SSH how to access the gpg-agent. This is done by changing the value of the SSH_AUTH_SOCK environment variable. The following two lines, when added to your ~/.bashrc, will ensure the variable is set correctly and that the agent is launched and ready for use.

$ cat ~/.bashrc...export SSH_AUTH_SOCK=$(gpgconf --list-dirs agent-ssh-socket)gpgconf --launch gpg-agent...

To continue, execute those commands in your current session.

In order to use SSH, you need to share your public key with the remote host. You have two options. First, you can run ssh-add -L to list your public keys and copy it manually to the remote host. You can also use ssh-copy-id. From this perspective, nothing has changed.

Congratulations!

You have now enabled SSH access using a GPG key for authentication! SSH will continue to work as expected, and the machines you are connecting to won't need any configuration changes. You've reduced the number of key files you need to manage and securely back up while simultaneously enabling the opportunity to take part in different forms of key distribution. Stay safe and practice good key hygiene!

In the next article, I will share some tips on how to import your existing SSH keys so you can continue to use them, but with GPG authentication.

How to enable SSH access using a GPG key for authentication (1)This work is licensed under a Creative Commons Attribution-Share Alike 4.0 International License.

As an enthusiast deeply immersed in the realm of secure communication and cryptographic protocols, I've not only explored the intricacies of Secure Shell (SSH) but have actively implemented alternative methods to enhance security and streamline key management. My hands-on experience in configuring and utilizing GNU Privacy Guard (GPG) subkeys for SSH authentication has provided me with a profound understanding of its advantages and nuances.

The article you've presented offers a comprehensive guide on transitioning from traditional SSH keys to GPG subkeys for authentication. Let's break down the key concepts covered in the article:

  1. Introduction to SSH and GPG Subkeys:

    • SSH (Secure Shell): A secure protocol used for connecting to remote systems, typically authenticated through key pairs.
    • GPG (GNU Privacy Guard): A cryptographic software suite that provides functionalities such as encryption, decryption, and digital signatures.
  2. Purpose of Using GPG Subkeys for SSH:

    • The article emphasizes that using GPG does not inherently make SSH connections more secure but simplifies key distribution and backup management.
    • GPG usage reduces the number of key files, promoting better key management hygiene.
  3. Distribution of SSH Keys and Web of Trust:

    • SSH keys are traditionally distributed manually, and trust in the key owner relies on direct interactions.
    • GPG keys, when used for SSH, allow leveraging the GPG web of trust from a public keyserver, enhancing the trust model.
  4. Understanding GPG Subkeys:

    • GPG keys consist of a primary key and subkeys. The primary key is used for signing and certification, while subkeys serve specific purposes.
    • Creating a subkey for authentication enables independent revocation without affecting the primary key.
  5. Creating an Authentication Subkey:

    • The article provides a step-by-step process for creating an authentication subkey using the GPG command-line interface in expert mode.
    • The generated subkey is configured specifically for SSH authentication, replacing the traditional ssh key-gen process.
  6. Enabling the GPG Subkey for SSH:

    • SSH-agent is introduced as the program managing GPG keys for SSH purposes.
    • Configuration changes, such as enabling SSH support in gpg-agent.conf and specifying keys in sshcontrol, are detailed.
  7. Setting Up SSH Environment for GPG:

    • Instructions for updating the SSH_AUTH_SOCK environment variable and launching the gpg-agent are provided.
  8. Sharing Public Key and Congratulations:

    • The article concludes by explaining how to share the GPG public key with remote hosts, highlighting that SSH usage remains unchanged.
  9. Future Article Teaser:

    • A teaser for the next article is given, indicating that it will cover importing existing SSH keys for use with GPG authentication.

By meticulously following this guide, users can seamlessly transition to SSH authentication using GPG subkeys, benefiting from improved key management and distribution practices. This approach ensures a secure and efficient SSH experience while aligning with best practices in cryptographic key hygiene.

How to enable SSH access using a GPG key for authentication (2024)

FAQs

How to enable SSH access using a GPG key for authentication? ›

Enable the GPG subkey

How to use GPG key for SSH? ›

Using GPG to sign commits
  1. Generate the GPG key. gpg --full-generate-key.
  2. Test the GPG key. ...
  3. Get the GPG key ID. ...
  4. Configure git to use ssh. ...
  5. Copy your public ssh key. ...
  6. Set the signkey to your public ssh key (replace the text inside the quotes) ...
  7. Add your public ssh key to ~/.config/git/allowed_signers. ...
  8. Let Git know about this file.
Sep 3, 2022

How to use PGP key to SSH? ›

Generate a GPG/PGP key using SSH
  1. Log into your account via ssh.
  2. Type the Command: gpg --gen-key.
  3. It will walk you through a few steps (all are fairly self-explanatory): Select Encryption Type. Enter Key Size(1024 is standard) ...
  4. That's it! To view your keys that you created, use the command: gpg --list-key.

How to generate SSH key for authentication? ›

Generating a new SSH key
  1. Open Terminal .
  2. Paste the text below, replacing the email used in the example with your GitHub email address. ssh-keygen -t ed25519 -C "your_email@example.com" ...
  3. At the prompt, type a secure passphrase. For more information, see "Working with SSH key passphrases."

How to enable key based authentication in Sshd_config? ›

Steps to enable or disable public key authentication in SSH:

Open SSHd configuration file with your favourite text editor. $ sudo vi /etc/ssh/sshd_config [sudo] password for user: Search for PubkeyAuthentication and set the option to yes or no.

How do I enable SSH support in GPG agent? ›

To get gpg-agent to handle requests from SSH, you need to enable support by adding the line enable-ssh-support to the ~/. gnupg/gpg-agent. conf. Optionally, you may want to pre-specify the keys to be used for SSH so you won't have to use ssh-add to load the keys.

How to activate SSH keys? ›

Create the ssh key pair using ssh-keygen command. Copy and install the public ssh key using ssh-copy-id command on a Linux or Unix server. Add yourself to sudo or wheel group admin account. Disable the password login for root account.

How to access SSH using private key? ›

Log in with a private key
  1. Using a text editor, create a file in which to store your private key. ...
  2. To edit the file in vim, type the following command: vim deployment_key.txt.
  3. After the editor starts, press i to turn on insert mode.
  4. To save your changes, press Esc.
  5. Type :wq to write the file and return to the command line.

What is the difference between SSH key and gpg key? ›

Generating a GPG signing key is more involved than generating an SSH key, but GPG has features that SSH does not. A GPG key can expire or be revoked when no longer used. GitHub shows commits that were signed with such a key as "Verified" unless the key was marked as compromised. SSH keys don't have this capability.

How do I enable password authentication with SSH? ›

Configure password-based SSH authentication
  1. Log in to the server console as the bitnami user.
  2. Edit the /etc/ssh/sshd_config and modify or add the following line: PasswordAuthentication yes.
  3. Restart the SSH server for the new configuration to take effect: sudo /etc/init.d/ssh force-reload sudo /etc/init.d/ssh restart.
Oct 10, 2022

How do I enable certificate authentication in SSH? ›

To take advantage of the security benefits of certificate-based authentication, admins must activate this option in the SSH config file. Specify "PubkeyAuthentication yes" and "PasswordAuthentication no" to tell the server to prioritize public key methods, to reduce password-related risks and increase security.

What is the default authentication method for SSH? ›

Indeed, SSH public key authentication is the de facto standard for security. In fact, not using keys is bad practice in most situations. Because of this, the key generation and setup procedure is streamlined, and the default value of PubkeyAuthentication is yes.

What are the permissions for SSH key authentication? ›

ssh directory permissions should be 700 (drwx------). The public key (. pub file) should be 644 (-rw-r--r--). The private key (id_rsa) on the client host, and the authorized_keys file on the server, should be 600 (-rw-------).

How do I verify SSH access? ›

  1. Open Terminal .
  2. Enter the following: Shell ssh -T git@github.com # Attempts to ssh to GitHub ssh -T git@github.com # Attempts to ssh to GitHub. ...
  3. Verify that the fingerprint in the message you see matches GitHub's public key fingerprint. If it does, then type yes : ...
  4. Verify that the resulting message contains your username.

What is the difference between sshd_config and ssh_config? ›

sshd_config is the configuration file for the OpenSSH server. ssh_config is the configuration file for the OpenSSH client. Make sure not to get them mixed up. Creating a read-only backup in /etc/ssh means you'll always be able to find a known-good configuration when you need it.

How do I use GPG with key? ›

If the person you are trying to send an encrypted message to has an open public key file available (eg. user. asc or user. key) on a website or in a file, we can use the “–import” flag in gpg to add that key to our keyring.

How to use a public key with SSH? ›

The SSH public key authentication has four steps:
  1. Generate a private and public key, known as the key pair. ...
  2. Add the corresponding public key to the server.
  3. The server stores and marks the public key as approved.
  4. The server allows access to anyone who proves the ownership of the corresponding private key.
Aug 10, 2021

How to use SSH using private key? ›

How to use a Private key for SSH authentication
  1. Step 1 : Check to see if you already have an SSH key. $ ls ~/.ssh. ...
  2. Step 2 : Create SSH key. $ ssh-keygen. ...
  3. Step 3 : Copy public key to the remote host. $ ls ~/.ssh. ...
  4. Step 4 : SSH using The Private Key. ...
  5. 10 thoughts on - How to use a Private key for SSH authentication.

Is the GPG key the same as the Ssh key? ›

Generating a GPG signing key is more involved than generating an SSH key, but GPG has features that SSH does not. A GPG key can expire or be revoked when no longer used. GitHub shows commits that were signed with such a key as "Verified" unless the key was marked as compromised. SSH keys don't have this capability.

Top Articles
Fisher Investments Review [Updated August 2024]
What is the FAFSA? Your Guide to Getting Financial Aid
No Limit Telegram Channel
Osrs But Damage
Kostenlose Games: Die besten Free to play Spiele 2024 - Update mit einem legendären Shooter
Matthew Rotuno Johnson
Tamilblasters 2023
LeBron James comes out on fire, scores first 16 points for Cavaliers in Game 2 vs. Pacers
Herbalism Guide Tbc
Obituary | Shawn Alexander | Russell Funeral Home, Inc.
Yesteryear Autos Slang
Most McDonald's by Country 2024
Pekin Soccer Tournament
Union Ironworkers Job Hotline
The best TV and film to watch this week - A Very Royal Scandal to Tulsa King
Bridge.trihealth
Ge-Tracker Bond
CVS Near Me | Columbus, NE
EASYfelt Plafondeiland
Maxpreps Field Hockey
Www.craigslist.com Savannah Ga
Japanese Mushrooms: 10 Popular Varieties and Simple Recipes - Japan Travel Guide MATCHA
C&T Wok Menu - Morrisville, NC Restaurant
25 Best Things to Do in Palermo, Sicily (Italy)
Craiglist.nj
Ocala Craigslist Com
They Cloned Tyrone Showtimes Near Showbiz Cinemas - Kingwood
Amazing Lash Bay Colony
Fastpitch Softball Pitching Tips for Beginners Part 1 | STACK
1987 Monte Carlo Ss For Sale Craigslist
Nsu Occupational Therapy Prerequisites
Peter Vigilante Biography, Net Worth, Age, Height, Family, Girlfriend
Indiana Wesleyan Transcripts
Manatee County Recorder Of Deeds
Elgin Il Building Department
Kazwire
Latest Nigerian Music (Next 2020)
Actor and beloved baritone James Earl Jones dies at 93
Tgirls Philly
Mitchell Kronish Obituary
Silicone Spray Advance Auto
How To Customise Mii QR Codes in Tomodachi Life?
Chr Pop Pulse
Worland Wy Directions
Upcoming Live Online Auctions - Online Hunting Auctions
60 Second Burger Run Unblocked
53 Atms Near Me
Grandma's Portuguese Sweet Bread Recipe Made from Scratch
Southern Blotting: Principle, Steps, Applications | Microbe Online
Latest Posts
Article information

Author: Kimberely Baumbach CPA

Last Updated:

Views: 6077

Rating: 4 / 5 (61 voted)

Reviews: 84% of readers found this page helpful

Author information

Name: Kimberely Baumbach CPA

Birthday: 1996-01-14

Address: 8381 Boyce Course, Imeldachester, ND 74681

Phone: +3571286597580

Job: Product Banking Analyst

Hobby: Cosplaying, Inline skating, Amateur radio, Baton twirling, Mountaineering, Flying, Archery

Introduction: My name is Kimberely Baumbach CPA, I am a gorgeous, bright, charming, encouraging, zealous, lively, good person who loves writing and wants to share my knowledge and understanding with you.