MEHDI.
RETURN_TO_INDEX

SSH Keys Explained: From Generation to Deployment

3 min read
#Security#DevOps#SSH#Linux

Introduction

As a developer, I, Mehdi Diouri, have found that using SSH keys is a crucial part of managing servers securely. SSH keys provide a secure way to access servers without having to enter a password every time. In this article, I will explain the process of generating SSH keys, adding them to the ssh-agent, deploying them to servers, and troubleshooting common permission issues.

Key Types

There are two main types of SSH keys: RSA and Ed25519. RSA keys are the traditional choice and are still widely supported. However, Ed25519 keys are considered more secure and are becoming increasingly popular.

RSA Keys

RSA keys are generated using the RSA algorithm and typically have a key size of 2048 bits or 4096 bits. To generate an RSA key, you can use the following command:

ssh-keygen -t rsa -b 2048

Ed25519 Keys

Ed25519 keys are generated using the Ed25519 algorithm and are considered more secure than RSA keys. To generate an Ed25519 key, you can use the following command:

ssh-keygen -t ed25519

Adding to ssh-agent

After generating an SSH key, you need to add it to the ssh-agent. The ssh-agent is a program that runs in the background and manages your SSH keys. To add a key to the ssh-agent, you can use the following command:

ssh-add ~/.ssh/id_ed25519

Deploying to Servers

To deploy an SSH key to a server, you need to copy the public key to the server's authorized_keys file. You can do this using the following command:

ssh-copy-id user@server

Replace user with your username and server with the server's IP address or hostname.

Common Permission Issues

One common issue with SSH keys is permission errors. The SSH key files should have the correct permissions to prevent unauthorized access. The private key file should have a permission of 600, and the public key file should have a permission of 644. You can change the permissions using the following command:

chmod 600 ~/.ssh/id_ed25519
chmod 644 ~/.ssh/id_ed25519.pub

On Windows, you may encounter issues with the SSH key files being stored in a directory with incorrect permissions. Make sure that the directory where you store your SSH key files has the correct permissions.

Practical Takeaways

To use SSH keys effectively, remember to:

  • Generate SSH keys using a secure algorithm such as Ed25519
  • Add your SSH key to the ssh-agent to manage it securely
  • Deploy your SSH key to servers using the ssh-copy-id command
  • Check the permissions of your SSH key files to prevent unauthorized access
  • Use the correct commands to change the permissions of your SSH key files if necessary