Your Web News in One Place

Help Webnuz

Referal links:

Sign up for GreenGeeks web hosting
April 23, 2023 04:17 pm GMT

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. Its 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 Im going to be connecting to an ubuntu server using an RSA key and not a password. Id 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 youll 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.

Im 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 didnt. 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 didnt use the default name, add this into your config file:
IdentityFile ~/.ssh/Serahs-MacBook-Pro

Of course youd change from Serahs Macbook Pro to you MacBooks name. After this, quit nano and this time run this command to try and see what error messages get raised when you SSH. This didnt 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


Original Link: https://dev.to/mundianderi/how-to-create-an-ssh-rsa-key-pair-and-connect-to-a-remote-host-23bp

Share this article:    Share on Facebook
View Full Article

Dev To

An online community for sharing and discovering great ideas, having debates, and making friends

More About this Source Visit Dev To