DEV Community

Cover image for Setup SSH to with one command
Brian Douglas
Brian Douglas

Posted on

Setup SSH to with one command

When working with a GitHub repository, you'll often need to identify yourself to GitHub. Up until last fall, using your username and password was enough, but that is no longer deemed secure. The alternative is using SSH, secure shell, allowing you to tell GitHub its you.

Setting up SSH is not a trivial thing to do, but now it is a bit more approachable with the GitHub CLI. I recently got a new mac and always need to look up how to set up SSH on new machines, it is part of development you don't do often but benefit a ton when you do. I delayed setting up my new dev machine to ensure I had enough time to set this up correctly the first time, but it turns out all I needed was the GitHub CLI. Before I get into how I set it up with one command, I want to first explain SSH keys.

SSH keys

If you work as an engineer, you've probably set up some SSH keys to login to a service from one machine to another. My first introduction to SSH was through setting up remote git pushes to GitHub.

SSH or Secure Shell keys is the name of the key (or protocol) used when you need to authenticate in order to use a provider. Using SSH protocol is more secure than using a text password on the command line. A bad actor will not be able to login or push remotely without access to your secure keys.

You can generate SSH keys in a few steps from the command line in order to authenticate your GitHub account. This key can get added manually to your GitHub account. Check out the GitHub Docs for how to manually generate keys and add them to your GitHub profile. If you are interested in learning how to do this with one command, continue reading.

GitHub CLI

The GitHub CLI brings GitHub to your terminal. One of those GitHub features is setting up SSH. The CLI allows you to authenticate from the command line using auth command. No need to set this up manually. The GitHub CLI will generate you keys and place in the correct place on GitHub.

Image description

If you have not set SSH, the CLI will this option for you.

gh auth login
Enter fullscreen mode Exit fullscreen mode

Before you start generation SSH keys, you can check for existing keys using the following command:

ls -al ~/.ssh
Enter fullscreen mode Exit fullscreen mode

If you are presented with a list of ssh keys then you can proceed to adding them manually to your GitHub account using the guide in the documentation, but in my recent setup I learned this can now be managed by the GitHub CLI using the following flag in the auth command.

gh auth login --git-protocol ssh
Enter fullscreen mode Exit fullscreen mode

The recommendation from GitHub is using SSH to connect to your repositories. If you have not set this up on your developer machine, consider leveraging the GitHub CLI to confirm your SSH connection is enabled.

If you are interesting in seeing some other tools I use on my developer machine, check out my other post on setting up my new M1 laptop with dev tools in this post.

If you are using the GitHub CLI, let me know which are your go to commands below.

Top comments (3)

Collapse
 
sabderemane profile image
Sarah Abd

Good to know. I will do that the next time I change my Mac.
I don't use often the CLI, but my favorite command from GitHub CLI is to setup secrets easily from .env file with this command :
gh secret set -f <path-of-the-file>/.env

Collapse
 
munnyreol profile image
Munny Reol

I did this auth system. using SSH. But it's still asking for username and password.
When i use terminal from vscode, then i don't need to use username and password.
So now ?m

Collapse
 
devangtomar profile image
Devang Tomar

That was a nice read! Liked, bookmarked and followed, keep the good work! 🙌