How to Generate SSH Private and Public Keys on Windows and Linux (FAQ Style)
1. What is an SSH Key?
SSH keys are cryptographic key pairs used for authenticating secure connections between your local machine and a remote server. They consist of:
-
Private Key: Kept secret on your local machine. It should never be shared.
-
Public Key: Shared with the remote server to authenticate the private key.
2. How Do I Generate SSH Keys on Windows?
Follow these steps to generate SSH keys on Windows using OpenSSH:
Prerequisites:
-
Windows 10 (version 1809 and later) comes with OpenSSH installed. If you're using an older version, you may need to install OpenSSH manually.
Steps:
-
Open PowerShell:
-
Press
Win + Xand select "Windows PowerShell (Admin)".
-
-
Generate the Key Pair:
-
Run the following command:
ssh-keygen -t rsa -b 4096 -C "your_email@example.com"This creates an RSA key pair with a length of 4096 bits.
-
-
Choose the Location to Save the Key:
-
When prompted, press
Enterto save the key pair in the default location:C:\Users\YourUsername\.ssh\id_rsa.
-
-
Set a Passphrase (Optional):
-
You can set a passphrase for additional security. If you don’t want to use a passphrase, press
Entertwice.
-
-
Locate Your Keys:
-
Your private key will be at:
C:\Users\YourUsername\.ssh\id_rsa -
Your public key will be at:
C:\Users\YourUsername\.ssh\id_rsa.pub
-
-
Add Your Public Key to the Remote Server:
-
Copy the content of the public key file (
id_rsa.pub) to the remote server's~/.ssh/authorized_keysfile.
-
3. How Do I Generate SSH Keys on Linux?
To generate SSH keys on Linux, you can use the ssh-keygen command.
Steps:
-
Open Terminal:
-
Press
Ctrl + Alt + Tor search for “Terminal” in your applications.
-
-
Generate the Key Pair:
-
Run the following command:
ssh-keygen -t rsa -b 4096 -C "your_email@example.com"This creates an RSA key pair with a length of 4096 bits.
-
-
Choose the Location to Save the Key:
-
You will be prompted for the file in which to save the key. By default, it saves to
~/.ssh/id_rsa.
PressEnterto confirm.
-
-
Set a Passphrase (Optional):
-
You can set a passphrase for additional security, or press
Enterto skip.
-
-
Locate Your Keys:
-
Your private key will be in
~/.ssh/id_rsa. -
Your public key will be in
~/.ssh/id_rsa.pub.
-
-
Add Your Public Key to the Remote Server:
-
Copy the content of the public key (
id_rsa.pub) to the remote server's~/.ssh/authorized_keysfile.
-
4. What If I Already Have SSH Keys?
You can check if you already have existing SSH keys by running:
Windows:
dir C:\Users\YourUsername\.ssh\
Linux:
ls ~/.ssh/
If the files id_rsa and id_rsa.pub (or similar) exist, you already have an SSH key pair.
5. How Do I Use My SSH Key for Authentication?
Once you've generated the SSH key pair and added the public key to the remote server:
-
SSH into the remote server:
-
Use the following command:
ssh username@hostname -
The system will automatically use the private key for authentication.
-
-
If Your Key is Not Default (
id_rsa):-
Specify the key file with the
-iflag:ssh -i ~/.ssh/my_custom_key username@hostname
-
6. How Do I Copy My Public Key to a Remote Server?
You can manually copy your public key using ssh-copy-id (on Linux) or manually copy/paste it.
Linux:
-
Run the following command:
ssh-copy-id username@hostnameThis will automatically append your public key to the
authorized_keysfile on the remote server.
Windows:
-
Manually copy the contents of
id_rsa.puband add it to the remote server’s~/.ssh/authorized_keysfile.
7. How Can I Test My SSH Key Authentication?
After setting up SSH key authentication, test by connecting to the remote server:
ssh username@hostname
If everything is set up correctly, you should log in without being asked for a password (unless you set a passphrase).
8. How Do I Change or Replace My SSH Key?
-
Generate a new key pair using the
ssh-keygencommand as shown above. -
Remove the old public key from the
~/.ssh/authorized_keysfile on the remote server. -
Add the new public key to the server's
authorized_keys.
9. How Do I Delete or Revoke an SSH Key?
-
To revoke an SSH key, simply remove the corresponding public key from the remote server's
~/.ssh/authorized_keysfile. -
You can also delete your local private and public keys:
rm ~/.ssh/id_rsa ~/.ssh/id_rsa.pub
Feel free to reach out if you need any more details!