Setup SSH to with one command
Tags: ssh, github, cli
Published: 2022-09-13 21:38:49

When working with a GitHub repository, you'll often need to identify yourself to GitHub. Up [until last fall](https://github.blog/changelog/2021-08-12-git-password-authentication-is-shutting-down/), 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](https://cli.github.com/). 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](https://docs.github.com/en/authentication/connecting-to-github-with-ssh) 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](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/zj71gbapf534jn3nshz4.png) 

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

```sh
gh auth login
```
Before you start generation SSH keys, you can check for existing keys using the following command:

```sh
ls -al ~/.ssh
```
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](https://docs.github.com/en/authentication/connecting-to-github-with-ssh/adding-a-new-ssh-key-to-your-github-account), but in my recent setup I learned this can now be managed by the GitHub CLI using the following flag in the auth command.  

```sh
gh auth login --git-protocol ssh
``` 

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. 

{% link https://dev.to/bdougieyo/the-first-two-dev-tools-installed-on-my-m1-mac-50l8 %}

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