DEV Community

Cover image for Add a New GitHub SSH Key Automatically πŸ”‘
maxam πŸ§‘πŸ»β€πŸ’»
maxam πŸ§‘πŸ»β€πŸ’»

Posted on • Edited on

Add a New GitHub SSH Key Automatically πŸ”‘

Every time you get a new machine and want to connect GitHub with SSH, it's so annoying to set up. Generate the SSH key after googling for a while and then manually copy & paste it to GitHub on their webpage.

Is it possible to do this whole process on the command line? Yes!

Today I'll introduce this brilliant CLI tool - gh-ssh-keygen.

How to use gh-ssh-keygen

There're two ways available.

Simply run this command if you don't want to install anything.

/bin/bash -c "$(curl -fsSL https://raw.githubusercontent.com/maxam2017/gh-ssh-keygen/HEAD/gh-ssh-keygen.sh)"
Enter fullscreen mode Exit fullscreen mode

Otherwise, you can install the whole command.

$ /bin/bash -c "$(curl -fsSL https://raw.githubusercontent.com/maxam2017/gh-ssh-keygen/HEAD/install.sh)"
Enter fullscreen mode Exit fullscreen mode

If this command has already been installed, it'll skip or upgrade if the new version is available.

Then, run

$ gh-ssh-keygen
Enter fullscreen mode Exit fullscreen mode

After a few interactive steps, SSH key would be added to your GitHub account πŸŽ‰

If you want to know more about how to use it, checkout this video for detail!

How this command works

Internally, it is powered by Official GitHub CLI which provides API to upload ssh keys.

$ gh ssh-key add [<key-file>] [flags]
Enter fullscreen mode Exit fullscreen mode

Except that, I connect the whole process with the MacOS (and Linux) package manager brew and OpenSSH auth utils ssh-keygen together.

Also, this command handles lots of package installation things and implement some extra validation for user input. Therefore, feel free to use it if you need to add SSH key to your GitHub account next time!


Welcome to follow me or star ⭐ ️my repo if you're interested in it.
GitHub Repo: https://github.com/maxam2017/gh-ssh-keygen
GitHub Account: https://github.com/maxam2017

Top comments (0)