LinuxCommandLibrary

gh-ssh-key

Manage GitHub SSH keys

TLDR

Display help

$ gh ssh-key
copy

List SSH keys for the currently authenticated user
$ gh ssh-key list
copy

Add an SSH key to the currently authenticated user's account
$ gh ssh-key add [path/to/key.pub]
copy

Add an SSH key to the currently authenticated user's account with a specific title
$ gh ssh-key add [[-t|--title]] [title] [path/to/key.pub]
copy

SYNOPSIS

gh ssh-key <subcommand> [<options>]

gh ssh-key add [<key-file> | --public-key <file>] [--title <title>] [--type <type>] [--allow-create-new]
gh ssh-key list
gh ssh-key delete <key-id>

PARAMETERS

add
    Subcommand to add a new SSH public key to your GitHub account.

list
    Subcommand to list all SSH public keys associated with your GitHub account.

delete
    Subcommand to delete an existing SSH public key from your GitHub account.

<key-file>
    (For add) The path to the SSH public key file to be added. Defaults to ~/.ssh/id_rsa.pub if not specified.

--public-key <file>
    (For add) Specifies the path to the SSH public key file. An alternative to providing <key-file> as a positional argument.

--title <title>
    (For add) A descriptive title for the new SSH key. If omitted, a title is generated automatically.

--type <type>
    (For add) Specifies the type of key to add, e.g., "authentication" or "signing". Defaults to "authentication".

--allow-create-new
    (For add) If the specified key file does not exist, this flag allows gh to prompt to create a new SSH key.

<key-id>
    (For delete) The unique ID of the SSH key to be deleted. This ID can be obtained using the list subcommand.

DESCRIPTION

The gh ssh-key command is a powerful subcommand within the GitHub CLI (gh), designed to streamline the management of SSH public keys associated with your GitHub account. It provides a convenient way to perform key operations directly from the terminal, eliminating the need to navigate the GitHub web interface for these tasks.

Users can easily add new SSH public keys, specify a descriptive title for identification, or even choose the key's intended use (e.g., authentication). Additionally, the command allows for listing all registered SSH keys on your account, displaying their unique IDs, titles, and fingerprints. When a key is no longer needed or compromised, gh ssh-key facilitates its secure deletion by referencing its unique ID. This integration simplifies developer workflows, especially for those who heavily rely on SSH for Git operations and want to manage their authentication credentials efficiently.

CAVEATS

Key Considerations:
Requires the GitHub CLI (gh) to be installed and properly authenticated with your GitHub account.
The command only manages public SSH keys on GitHub; private keys remain on your local system.
Deletion of keys on GitHub is irreversible.
Ensure the specified SSH key file exists and is readable.

COMMON USAGE WORKFLOW

A typical workflow involves first generating an SSH key pair using ssh-keygen, then adding the public key to GitHub using gh ssh-key add. To verify, use gh ssh-key list. If a key is no longer needed, gh ssh-key delete can remove it. This command streamlines secure authentication setup for Git operations over SSH.

HISTORY

The gh (GitHub CLI) project was launched in 2020 by GitHub to bring GitHub features to the terminal. The ssh-key subcommand is an integral part of this effort, simplifying common developer tasks related to SSH key management directly from the command line, aligning with the CLI's goal of enhancing productivity and reducing context switching for GitHub users.

SEE ALSO

gh(1), ssh-keygen(1), ssh(1), git(1)

Copied to clipboard