LinuxCommandLibrary

gh-ssh-key

Manage GitHub SSH keys

TLDR

List SSH keys for the currently authenticated user

$ gh ssh-key [[ls|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

Display help
$ gh ssh-key
copy

SYNOPSIS

gh ssh-key {add [<key-file>] | list | delete <id>} [flags]

PARAMETERS

add [<key-file>]
    Add SSH public key to account from file or stdin

list
    List all SSH public keys for account

delete <id>
    Delete SSH public key by its ID

-t, --title <string>
    Descriptive title for new key (add)

-p, --public-key-file <file>
    Path to public key file; defaults to <key-file>.pub (add)

--show-public-key
    Include public key in list output (list)

--limit <int>
    Max keys to list; default 30 (list)

-y, --yes
    Skip confirmation prompts (add, delete)

DESCRIPTION

The gh ssh-key command, part of the GitHub CLI, allows users to manage SSH public keys linked to their GitHub account from the terminal. It supports adding new keys from files or stdin, listing existing keys with optional public key display, and deleting keys by ID. This facilitates secure SSH authentication for Git operations without browser interaction.

Key management requires GitHub CLI authentication, typically via a personal access token with write:public_key scope for modifications. Supported key types include Ed25519, RSA, and ECDSA in OpenSSH format. Ideal for automation in scripts or CI/CD, it reads private keys indirectly via public files and uploads them to GitHub.

Output for list includes key ID, title, fingerprint, and creation date. Confirmations prevent accidental deletions.

CAVEATS

Requires GitHub CLI auth with write:public_key scope for add/delete. Keys must be OpenSSH public format; private keys not uploaded. Rate-limited by GitHub API.

EXAMPLES

Add key:
gh ssh-key add ~/.ssh/id_ed25519.pub -t "Work Laptop"

List with keys:
gh ssh-key list --show-public-key

Delete:
gh ssh-key delete 123ABC

HISTORY

Introduced in GitHub CLI v0.11.0 (2020); stabilized in v1.0.0 (Dec 2020). Expanded with delete/rotate in v2.x for better key lifecycle management.

SEE ALSO

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

Copied to clipboard