DEV Community

Serah Nderi
Serah Nderi

Posted on

How to create an SSH RSA Key Pair and Connect to a Remote Host

SSH Essentials and SSH Config File

Secure Shell (SSH) protocol is a secure way of connecting to a remote machine via the internet. It’s used to encrypt the connection between two machines.

Image description Source: Christopher Gower

How SSH Works

When you SSH to another machine, you open a connection between those two machines. SSH breaks the data down into a series of packets. In networking, Packets are small segments of messages. Packets contain:

  • Packet length — about 4 bytes.
  • Padding amount and the
  • Payload.
  • Message or authentication code.

Then the above packet is encrypted and sent out. It will then be decrypted by the server. To authenticate using SSH you can use:

  • Password — default way of authenticating. The syntax is ssh user@local ip address.
    ssh user@local ip address.

  • Public/Private Key Pair — This one bypasses the password.

  • Host based.

For this particular tutorial and for the purposes of my project I’m going to be connecting to an ubuntu server using an RSA key and not a password. I’d already generated the RSA key.
To Generate an RSA Key, run this command on your terminal:
ssh-keygen

This generates a public and private key that you’ll need to save on your local machine. It will create a private key and save it in a .ssh/id_rsa(private key) and a public key .ssh/id_rsa.pub both in a .SSH folder.

I’m using macOS so setting this up might be different for Windows. For windows users, follow this tutorial, or upgrade to Windows 10 or, use git bash.

For linux and macOS users, follow this tutorial .

How to connect to a remote host using an SSH RSA key pair

For you to connect to the server with SSH, the server has to have SSHD(SSH Daemon) installed and running or you will not be able to connect using SSH.
To login to our local server we run this command :

ssh <nameusedwhencreatingrsakey>@ip address

Continue connecting and authenticate with the server password.


While connecting with my SSH key, I got this error:
Permission denied (publickey)

To fix this I opened my macOS terminal and run this command while in the home directory:

$ cd ~/.ssh

Then I run the ls -la command to see if the private and public key existed here, which they didn’t. I hadn't named my public key name as id_rsa which is the default name that SSH checks for when attempting to make an SSH connection.

To change this, go into your config file by running the command after running the ls -la command:

$ nano config

If you didn’t use the default name, add this into your config file:
IdentityFile ~/.ssh/Serahs-MacBook-Pro

Of course you’d change from Serah’s Macbook Pro to you MacBook’s name. After this, quit nano and this time run this command to try and see what error messages get raised when you SSH. This didn’t solve my problem.

In the end, what worked was :

  • Changing into my home directory.
  • Creating new keys in the home directory(calling ssh-keygen and , saving the keys with the default names ie id-rsa and id-rsa.pub)
  • Updating the server with the new public key.
  • Calling my ./file-name to check if the SSH connection was made.

Sources

Fix for SSH Permission Denied (Public Key)

SSH Crash Course | With Some DevOps
What is SSH Public Key Authentication?

You may also like:

Introduction to Python for Data Engineering
Getting Started with Github as a Technical Writer

This article first appeared on Medium

Top comments (0)