Gatekkas Corner
My profile
Last Updated @ Apr 22, 2024 17:52

Generating an SSH Key Pair

Last Updated @ Apr 22, 2024 17:54


SSH keys are a pair of public and private keys that are used to establish an encrypted connection using SSH. In order to utilize SSH we’ll need to create a new pair of keys.

Step 1: Generating New Pair Of SSH Keys

In order to generate a new pair of SSH Keys, we’ll need to execute the command ssh-keygen -t rsa -b 4096 -C "your_email@example.com" in the terminal.

  • The -t rsa option specifies the type of key (RSA in this case).
  • The -b 4096 option sets the key length to 4096 bits.
  • The -C "your_email@example.com option adds a comment to identify the key.

Step 2: Choosing Filename For Keys

Next, you’ll be prompted to choose a name for your new pair of keys. Make sure to choose a unique name that doesn’t already exist as you can overwrite an existing key.

Here’s an example in which we’ll create a pair of keys named “id_rsa_example-key”: Enter file in which to save the key (/home/user/.ssh/id_rsa): id_rsa_example-key

After entering the name, a new pair of keys will be created and you’ll be prompted the location of where they’re stored. The key ending in .pub will be your public key, and the other will be your private key.

So far, the creation of these keys don’t mean anything as we using them won’t work. In order to make them work we’ll need to add our new public key to our list of authorized keys.

Step 3: Adding New Public Key To Authorized Keys

To allow our newly generated pair of keys to work, we need to append the contents inside our public key to the authorized_keys file usually located in ~/.ssh/authorized_keys/. We can do this by executing the command cat ~/.ssh/id_rsa_example-key.pub >> ~/.ssh/authorized_keys.

Step 4: Testing SSH Connection

To see if our new key works, we can attempt an SSH by executing the command ssh <user>@<host> -i <private key> where “user” is the username of the server, “host” is the hostname or IP address of the server, and “private key” is the directory of the private key.

Here’s an example that uses a private key to connect to a remote server: ssh chris@example.com -i ~/.ssh/id_rsa_example-key

Sources

Generate an SSH Key Pair