# Rsa keys in ssh

### Authentication process using RSA keys in SSH:

1. Key Generation: On the client side, a key pair is generated consisting of a public key and a corresponding private key. The private key is stored securely on the client machine, while the public key can be shared.
    
2. Key Distribution: The public key is copied to the remote server. This can be done by adding the public key to the server's authorized keys file.
    
3. SSH Connection: When the client attempts to connect to the server using SSH, the server sends a randomly generated challenge to the client.
    
4. Key-based Authentication: The client uses its private key to sign the challenge received from the server. This signature is sent back to the server.
    
5. Signature Verification: The server uses the stored public key to verify the signature sent by the client. If the signature is valid, the server allows the SSH connection.
    

This authentication process ensures that the client possesses the private key corresponding to the public key stored on the server. It provides a secure way to authenticate the client without transmitting sensitive information like passwords over the network.

### How to generate SSH key pair in your terminal(Ubuntu)

```bash
ssh-keygen -t rsa -b 4096
```

Let's break down the command:

* `ssh-keygen`: This is the command-line utility for generating SSH key pairs.
    
* `-t rsa`: Specifies the type of key algorithm to use, in this case, RSA.
    
* `-b 4096`: Specifies the key size, in bits. In this example, the key size is set to 4096 bits. While 4096 bits is not the standard or mandatory, it is generally recommended for high-security environments or situations where you want to future-proof your SSH key.
    

When you run this command, `ssh-keygen` will prompt you for a file location to save the generated key pair and optionally a passphrase to encrypt the private key. By default, the private key will be saved in the file `id_rsa` in the `~/.ssh/` directory, and the public key will be saved in a file with the same name but with the `.pub` extension.

Here's an example of how the command prompt might look when generating the key pair:

```bash
$ ls
Generating public/private rsa key pair.
Enter file in which to save the key (/home/user/.ssh/id_rsa):
Enter passphrase (empty for no passphrase):
Enter same passphrase again:
Your identification has been saved in /home/user/.ssh/id_rsa.
Your public key has been saved in /home/user/.ssh/id_rsa.pub.
The key fingerprint is:
SHA256:XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX user@hostname
The key's randomart image is:
+---[RSA 4096]----+
|          XXXX   |
|          XXXX   |
|          XXXX   |
|          XXXX   |
|          XXXX   |
|          XXXX   |
|          XXXX   |
|          XXXX   |
|          XXXX   |
+----[SHA256]-----+
```

After generating the key pair, you can use the generated public key (`id_rsa.pub`) to authenticate with remote servers or services that support SSH key-based authentication.
