If you're sick of entering your password every time you need to remotely connect to a machine or work with a repository on Github, you're at the right spot.

How does this work? Basically, we will create a public and private key-pair. You then give out your public key to other machines / services. Now, when you try to connect your machine to the other ones (or to Github), your private key is automatically used to authenticate you. Cool, right?

Generating a key-pair

First, let's create a pair of keys. If you're running macOS or Linux, just follow the steps below. If you're on Windows, I recommend using Git Bash to run the following commands. They will not work natively on cmd or Powershell.

Open up a terminal and run the following:

ssh-keygen -t ecdsa

<aside> 💡 The -t ecdsa is optional, but it uses a more secure cryptographic function to generate the key pair. By default, it uses -t rsa, which will generate files called id_rsa and id_rsa.pub

</aside>

Follow the prompts to create the keys. I recommend keeping them in their default location ~/.ssh/ (Or C:\\Users\\username\\.sshon Windows). Note that if you choose to set-up a passphrase here, you will need to use that every time you are authenticating yourself using the private key. Next, run the following to view your public key, and copy it to your clipboard:

cat ~/.ssh/id_ecdsa.pub

<aside> ⚠️ Make sure you're not doing this with your private key, called id_ecdsa without the .pub extension. You should never give out this key to any person or website.

</aside>

Setting up a remote machine (automated)

If your machine has the ssh-copy-id utility installed (just try running it), this step is easy. Just run

ssh-copy-id <user>@<server>

and enter in your password one last time. That's it - you never need to enter an SSH password again

Setting up a remote machine (manually)

To setup your public key on a remote machine, first SSH into it (using your password for the last time!). Once on the remote machine, create the ~/.ssh directory if it doesn't already exist:

mkdir ~/.ssh/

Then, paste your public key (which you should have copied from earlier) into a file called authorized_keys inside the directory ~/.ssh. Running the following command should show you your public key:

cat ~/.ssh/authorized_keys

If you want to add other public keys (to be able to SSH in from multiple machines), they should all be added in the same file on new lines.