Aller au contenu principal

🔐 Recreate an SSH Key in WSL and Deploy It to Servers

This guide explains how to regenerate an SSH key inside a WSL instance after deleting it, and how to configure remote servers to accept this new key.


1. Generate a New SSH Key Pair

  1. Open a terminal in your WSL instance.
  2. Run the following command:
ssh-keygen -t ed25519 -C "your.email@example.com"
  • -t ed25519: modern and secure key type (recommended).
  • -C: comment to identify the key (optional but useful).
  1. When prompted:
    • File location: press Enter to accept the default ~/.ssh/id_ed25519 path.
    • Passphrase: optional but recommended for added security (for automation, don't set it).

This creates two files:

  • ~/.ssh/id_ed25519private key
  • ~/.ssh/id_ed25519.pubpublic key

2. Start the SSH Agent and Add the Key

To make the key available automatically:

eval "$(ssh-agent -s)"
ssh-add ~/.ssh/id_ed25519

Verify the key is loaded:

ssh-add -l

3. Copy the Public Key to Remote Servers

For each remote server you want to configure, run:

ssh-copy-id -i ~/.ssh/id_ed25519.pub user@server

💡 If ssh-copy-id is not available in your WSL, copy the key manually:

cat ~/.ssh/id_ed25519.pub

Then, on the remote server:

  • Connect using password
  • Append the public key content to ~/.ssh/authorized_keys
  • Ensure correct permissions:
chmod 700 ~/.ssh
chmod 600 ~/.ssh/authorized_keys

4. Test the Connection

From your WSL:

ssh user@server

If everything is configured correctly, you should connect without entering a password.


5. (Optional) Configure ~/.ssh/config

To simplify connections, create or edit the ~/.ssh/config file:

nano ~/.ssh/config

And add:

Host my-server
HostName server.address
User user
IdentityFile ~/.ssh/id_ed25519

You can then connect with:

ssh my-server

✅ Summary

  • Generate SSH key (ssh-keygen)
  • Start the agent and add the key (ssh-agent + ssh-add)
  • Copy the public key to servers (ssh-copy-id or manual)
  • Test and optionally configure ~/.ssh/config

⚠️ Important: Never share your private key (id_ed25519).
It must stay only on your local machine.