MEHDI.
RETURN_TO_INDEX

SSH Keys Explained: From Generation to Deployment

3 min read
#Security#DevOps#SSH#Linux

Introduction to SSH Keys

As a developer, I have found that securing my connections to remote servers is crucial. One of the most effective ways to achieve this is by using SSH keys. 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 widely supported. However, Ed25519 keys are considered more secure and are becoming increasingly popular.

RSA Keys

RSA keys are generated using the ssh-keygen command with the -t option followed by rsa. For example:

ssh-keygen -t rsa -b 4096

This command generates a 4096-bit RSA key pair.

Ed25519 Keys

Ed25519 keys are also generated using the ssh-keygen command with the -t option followed by ed25519. For example:

ssh-keygen -t ed25519

This command generates a 256-bit Ed25519 key pair.

Adding Keys to ssh-agent

After generating the key pair, it is necessary to add the private key to the ssh-agent. This can be done using the ssh-add command. For example:

ssh-add ~/.ssh/id_ed25519

This command adds the private key to the ssh-agent.

Deploying Keys to Servers

To deploy the public key to a server, it is necessary to copy the public key to the server's authorized_keys file. This can be done using the ssh-copy-id command. For example:

ssh-copy-id user@server

This command copies the public key to the server's authorized_keys file.

Common Permission Issues

One common issue when working with SSH keys is permission issues. On Windows, the .ssh directory and its contents should have the correct permissions. The .ssh directory should have a permission of 700 and the authorized_keys file should have a permission of 600. For example:

chmod 700 ~/.ssh
chmod 600 ~/.ssh/authorized_keys

These commands set the correct permissions for the .ssh directory and the authorized_keys file.

Troubleshooting

If you encounter issues with SSH keys, it is often helpful to use the -v option with the ssh command to enable verbose mode. For example:

ssh -v user@server

This command enables verbose mode and can help diagnose issues with SSH keys.

Practical Takeaways

To get the most out of SSH keys, it is essential to understand how to generate, deploy, and troubleshoot them. Here are some key takeaways:

  • Use Ed25519 keys for increased security
  • Use the ssh-add command to add private keys to the ssh-agent
  • Use the ssh-copy-id command to deploy public keys to servers
  • Ensure correct permissions for the .ssh directory and its contents
  • Use verbose mode to troubleshoot issues with SSH keys