How to create SSH Keys - Complete Tutorial

SSH keys are a pair of cryptographic keys used to authenticate a user or device in the Secure Shell (SSH) protocol. They provide a more secure and convenient alternative to password-based logins for accessing remote servers and services. In this guide, we will learn how to create ssh keys using PuTTYgen and command line.
Components of SSH Keys
-
Private Key: This key remains on the client machine and should be kept secret. It is used to decrypt messages that were encrypted with the corresponding public key and to sign messages, ensuring the authenticity of the client.
-
Public Key: This key is placed on the server you want to access. It is used to encrypt messages that only the corresponding private key can decrypt, and to verify signatures made with the corresponding private key.
How SSH Keys Work
When you attempt to connect to a server using SSH keys, the following steps occur:
-
Key Pair Generation: You generate a pair of keys (private and public) on your local machine using a tool like
ssh-keygen
. -
Public Key Placement: You copy the public key to the remote server's
~/.ssh/authorized_keys
file. This grants access to the user associated with that public key. -
Authentication Process:
- The client requests access to the server.
- The server sends a challenge (a random string) encrypted with the client’s public key.
- The client decrypts the challenge using its private key and sends it back to the server.
- The server verifies the response. If the response is correct, the server grants access.
Benefits of SSH Keys
-
Security: SSH keys are much harder to crack compared to passwords. They use strong encryption algorithms, making brute-force attacks impractical.
-
Convenience: Once set up, SSH keys allow passwordless logins, making it easier to script and automate tasks that require SSH access.
-
No Need for Password Sharing: Public keys can be shared openly without compromising security, unlike passwords.
How to Generate SSH Keys
1. Generate the SSH Key Pair using ssh-keygen:
ssh-keygen -t rsa -b 4096 -C "[email protected]"
-t ed25519
: Specifies the type of key to create (ed25519 is a modern algorithm).-C "[email protected]"
: Adds a comment (often used to identify the key).
~/.ssh/id_rsa
) and optionally set a passphrase for extra security.- Go to https://putty.org and download the latest putty installer and proceed to install it on windows.
- Once installed, you will see puttygen available with putty.
- Now click on PuTTYgen and open it.
- Click on Generate button and hover around blank area to generate randomness of your ssh keys.
- On Key Comment, enter the name of your ssh key.
- On Key Passphrase, enter a strong password and re-enter it on confirm passphrase input field.
- Now save your public and private keys using their respective buttons. Private keys have .ppk and public keys have .pub extension when created from PuTTYgen.
Conclusion
SSH keys provide a secure and efficient way to authenticate to remote servers. By using a key pair (private and public keys), we can enhance security, streamline access, and enable secure automation of tasks.