How to create an SSH RSA Key Pair and Connect to a Remote Host
Tags: ssh, beginners, tutorial, devops
Published: 2023-04-23 16:17:22

## 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](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/ouzjyy3vot3ifer2roo7.jpg) [Source: Christopher Gower](https://unsplash.com/@cgower?utm_source=medium&utm_medium=referral)

## 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](https://docs.rackspace.com/support/how-to/generating-rsa-keys-with-ssh-puttygen/), or upgrade to Windows 10 or, use git bash.

For linux and macOS users, [follow this tutorial](https://askubuntu.com/questions/61557/how-do-i-set-up-ssh-authentication-keys) .

##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)
](https://youtu.be/A9CSSbten_s)
[SSH Crash Course | With Some DevOps](https://youtu.be/hQWRp-FdTpc)
[What is SSH Public Key Authentication?](https://www.ssh.com/academy/ssh/public-key-authentication)

##You may also like:
[Introduction to Python for Data Engineering](https://medium.com/blog-write-heal/introduction-to-python-for-data-engineering-353df41b6e63)
[Getting Started with Github as a Technical Writer](https://medium.com/blog-write-heal/getting-started-with-github-as-a-technical-writer-c7d6eda1ea4c)


This article first appeared on [Medium](https://medium.com/blog-write-heal/how-to-create-an-ssh-rsa-key-pair-and-connect-to-a-remote-host-fadedc49140)