The Easiest Way to Add SSH Keys to a Remote Server on MacOS

Adding your SSH keys to a remote server from your Mac can seem like a hassle, but there is a command line tool that will make life extremely easy for you. Plus, once you add your SSH keys to the remote server, you won’t need your password every time (unless you choose to generate a passphrase)! Easy and secure! 🔒

What we will use in this tutorial:

  • MacOS Terminal
  • Homebrew
  • ssh-copy-id

Don’t worry, this is super easy and we will walk through it step by step.

Step One: Generate SSH Keys

Open up your terminal of choice. If you’re not sure how, hit cmd space and then type terminal.

Now, we need to generate our SSH keys. If you’re positive you’ve already done this, you can skip this step. To generate your SSH keys, run the following command in your terminal:

ssh-keygen -t rsa

Hit enter a few times, and you’ll see something that looks like this image courtesy of DigitalOcean:

Sample output of running the keygen command.

Boom! You now have generated an SSH key! 😎

Step Two: Install ssh-copy-id

Next, we are going to install a utility that will make the process of copying and uploading our ssh keys to a remote server super easy. We will use Homebrew to install ssh-copy-id. First, check if you have Homebrew installed by running:

brew --version
Output of running brew --version
If Homebrew is installed, you’ll see some output similar to this image.

If you don’t have it installed, please follow the instructions on the Homebrew website.

Now that we are certain we have Homebrew installed, we will install ssh-copy-id. Run the following in your terminal:

brew install ssh-copy-id

Step Three: Add the key to your server

This is the last and final part! And thanks to ssh-copy-id, it’s going to do all the heavy lifting for us. Normally, we would need to find our keys, copy them exactly, run some commands on our server to get them in the right place. Nah. Let’s skip that!

Instead, just run the following command:

ssh-copy-id username@123.45.678.9

Replace username and the IP address with your username and IP address of your remote server. You’ll be prompted for your password (but for the last time)! This will now automatically copy your key you generated and place it on the right spot on your server.

Test it out!

You’re done, great job! 🎉

To test it out, logout of your server first:

logout

Now try to connect to your server:

ssh username@123.45.678.9

Boom! You won’t be prompted for your password (assuming you didn’t create a passphrase when generating your keys). Now you can log in and out of your server quickly and securely, without having to type your password.

Leave a Reply

Your email address will not be published. Required fields are marked *