How to Create SSH Key on Windows
Securing your SSH connections can be greatly enhanced by utilizing SSH keys. While SSH is enabled by default in most Linux distributions and macOS, Windows users may need to install an SSH client. Let's guide you through creating SSH keys on Windows.
First, ensure you have OpenSSH installed on your Windows machine. It comes pre-installed starting with Windows 10 version 1809 and Windows Server 2019. If you're using an earlier version, you can install it via the Microsoft Store or the optional features in the Windows Control Panel.
Generating SSH Key Pair
The first step is generating a key pair, which consists of a public and a private key. The public key remains on your remote server, while the private key stays secure on your local machine.
Open PowerShell as an administrator and type the following command to generate a new key pair: `ssh-keygen -t rsa -b 4096 -C "[your_email@example.com]"`. This will create a new 4096-bit RSA key with your email as the comment.
Choosing a File Location and Passphrase
By default, the keys are saved in `~\.ssh\` directory in your user profile. You may choose a different location, but ensure you remember it for future use. You'll be prompted to enter a passphrase for added security. It's strongly recommended to use a strong passphrase.
Press Enter to accept the default file locations and passphraseога Cao Store your keys in `C:\Users[YourUsername]\.ssh` folder.
Adding Your Key to SSH Agent
To use your key for authentication, you'll need to add it to your SSH agent. Open PowerShell and start the Windows SSH Agent service: `Start-Service ssh-agent`. Then, add your key to the agent: `ssh-add ~/.ssh/id_rsa`. Replace `id_rsa` with the name of your private key file if it's different.
For persistent agent functionality, you might want to add these commands to your PowerShell profile so they run automatically each time you open PowerShell.
Copying the Public Key to the Remote Server
Now that your keys are set up locally, you'll need to copy the public key to your remote server. The public key is appended with `.pub`.
Use the following command to copy your public key to the `~/.ssh/authorized_keys` file on your server: `ssh-copy-id username@server_ip`. Replace `username` with your remote username and `server_ip` with your server's IP address. You'll be prompted to enter your remote server's password.
Verifying Your SSH Key
To verify that your key is working, disconnect and then reconnect to your remote server using SSH: `ssh username@server_ip`. If you've set up your key correctly, you should not be prompted for a password.
For added security, you should ensure that the `~/.ssh/` directory on your remote server is only accessible to you.
Remember to manage your keys responsibly. Regularly update your keys and keep your passphrases secret. Now, you're ready to enjoy secure SSH connections on your Windows machine. Happy coding!