How to Manage GitHub SSH keys (with examples)

How to Manage GitHub SSH keys (with examples)

GitHub SSH keys are used to securely authenticate and establish a connection between a user’s computer and their GitHub account. The gh ssh-key command allows users to manage and manipulate these SSH keys directly from the command line, providing a convenient and efficient way to organize and control access to a GitHub account. This article will walk through several examples of how to use the gh ssh-key command in different scenarios.

Use case 1: Display help

Code:

gh ssh-key

Motivation: Displaying help is useful when users are unfamiliar with the command or need a refresher on the available options and arguments.

Explanation: By executing the gh ssh-key command without any additional arguments, the command will display a help message providing an overview of the command’s purpose and the available subcommands. The help message includes a link to the GitHub CLI manual for more detailed information.

Example output:

Manage GitHub SSH keys

Usage:
  gh ssh-key [command]

Available Commands:
  add         Add one or more SSH keys
  get         Get detailed information about a SSH key
  help        Help about any command
  list        List SSH keys
  remove      Remove a SSH key

Flags:
      --version   Show the version information

Additional help topics:
  gh ssh-key help

Use case 2: List SSH keys for the currently authenticated user

Code:

gh ssh-key list

Motivation: Listing the SSH keys associated with a user’s GitHub account is useful when needing to keep track of the existing keys or verifying that the keys are correctly set up.

Explanation: Running the gh ssh-key list command retrieves a list of SSH keys linked to the currently authenticated GitHub user. This includes the unique key ID, title, and fingerprint of each key.

Example output:

ID            Title                    Fingerprint
1234567       My Key                   ab:cd:ef:12:34:56:78:90:ab:cd:ef:12:34:56:78:90
2345678       Another Key              12:34:56:78:90:ab:cd:ef:12:34:56:78:90:ab:cd:ef

Use case 3: Add an SSH key to the currently authenticated user’s account

Code:

gh ssh-key add path/to/key.pub

Motivation: Adding an SSH key to a GitHub account allows users to establish authenticated connections and perform operations securely.

Explanation: Executing the gh ssh-key add command followed by the path to the public key file, path/to/key.pub, adds the provided SSH key to the currently authenticated user’s GitHub account. This process authenticates the key and establishes a secure connection between the user’s computer and GitHub.

Example output:

Key added successfully.

Use case 4: Add an SSH key to the currently authenticated user’s account with a specific title

Code:

gh ssh-key add --title title path/to/key.pub

Motivation: Assigning a descriptive title to an SSH key helps users differentiate and manage multiple keys associated with their GitHub account.

Explanation: By appending the --title flag and the desired title (title) to the gh ssh-key add command, users can provide a custom name for the SSH key being added. This title is displayed in the list of SSH keys associated with the GitHub account.

Example output:

Key with title "My Custom Key" added successfully.

Conclusion:

The gh ssh-key command provides a straightforward and efficient way to manage GitHub SSH keys directly from the command line. Whether users need to add, list, or remove keys, the command offers a convenient interface to perform these operations seamlessly. By leveraging the gh ssh-key command, users can easily control access to their GitHub account and maintain secure connections for their workflows.

Related Posts

How to use the command uwfmgr (with examples)

How to use the command uwfmgr (with examples)

The uwfmgr command is used to manage and configure the Unified Write Filter (UWF) on Windows systems.

Read More
How to use the command logcat (with examples)

How to use the command logcat (with examples)

The logcat command is a useful tool for analyzing and troubleshooting issues in Android applications.

Read More
How to use the command git bisect (with examples)

How to use the command git bisect (with examples)

Git bisect is a command in Git that allows users to perform a binary search to find the commit that introduced a bug.

Read More