# 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.

**<mark>Step 1: Generate SSH Key Pair</mark>**

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

```plaintext
ssh-keygen
```

This will create two files: a private key (`~/.ssh/id_rsa`) and a public key (`~/.ssh/id_`[`rsa.pub`](http://rsa.pub)).

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1726759846482/351610d1-10fe-4c3d-a5c2-8696597295d3.png align="center")

**<mark>Step 2: Copy the Public SSH Key</mark>**

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

```plaintext
cat ~/.ssh/id_rsa.pub
```

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

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1726760540969/3a68a08e-c8be-4e2a-a4c2-f7ed2e94cbe5.png align="center")

**<mark>Step 3: Add Your Public Key to the Remote Server</mark>**

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

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1726763578374/7322effb-f70b-44a1-aaa5-f54eb55aa940.png align="center")

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

**<mark>Step 4: Test the SSH Connection</mark>**

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

```plaintext
ssh user@172.31.13.54
```

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

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1726763902192/b38690a2-42b5-4031-abad-9154dce71ecf.png align="center")
