Creating and Using SSH Key Pairs in Linux

Creating and Using SSH Key Pairs in Linux

An SSH key is a cryptographic key used in the Secure Shell (SSH) protocol to establish a secure connection between a client and a server. SSH keys come in pairs:

  1. Public Key: This key can be shared with anyone and is typically added to the ~/.ssh/authorized_keys file on the server you want to access. It allows the server to recognize your client as a trusted user.

  2. Private Key: This key is kept secret and should never be shared. It resides on your client machine and is used to authenticate your identity when connecting to the server.

Key Features

  • Security: SSH keys provide a secure method of authentication, eliminating the need for passwords, which can be more vulnerable to interception.

  • Ease of Use: Once set up, SSH keys allow for passwordless login, making access to remote systems easier and faster.

  • Encryption: SSH keys help encrypt the data being transferred between the client and server, ensuring privacy and data integrity.

How It Works

When you try to connect to a server using SSH:

  1. The server sends a challenge to the client.

  2. The client uses its private key to sign the challenge and sends the response back.

  3. The server verifies the response using the corresponding public key. If it matches, the connection is established.

SSH keys are widely used for secure remote logins, secure file transfers, and automating system management tasks.

Step 1: Generate SSH Key Pair

First, generate an SSH key pair on your local machine. This key pair will allow passwordless authentication to the remote server.

ssh-keygen

This will create two files: a private key (~/.ssh/id_rsa) and a public key (~/.ssh/id_rsa.pub).

Step 2: Copy the Public SSH Key

Display the contents of your public SSH key on the local machine:

cat ~/.ssh/id_rsa.pub

This will output your public key. Copy the entire output.

Step 3: Add Your Public Key to the Remote Server

Log in to the remote server and paste the public key into the authorized_keys file:

Make sure to replace "your_public_key_here" with the actual content of your public key.

Step 4: Test the SSH Connection

Try logging back into the remote server from the local machine without entering a password:

ssh user@172.31.13.54

If you successfully log in without a password prompt, your SSH key setup is working.