This tutorial will guide you through generating SSH keys on Windows using PowerShell, saving them to a custom folder, and using them to securely connect to a Linux system with a specified port.
Press Win + X.
Select `Terminal` from the menu.
Use the `ssh-keygen` command to generate an SSH key pair. Replace `your_email@example.com` with your email address and adjust the `file path` as needed.
Windows (PowerShell)
ssh-keygen -t rsa -b 4096 -C "your_email@example.com" -f "C:\Users\YourUsername\Desktop\CustomFolder\my_ssh_key"
You can set a passphrase for added security when prompted. This step is optional but recommended.
The generated keys will be saved in the specified folder:
my_ssh_key (private key)
my_ssh_key.pub (public key)
Use the `Get-Content` command to read and copy the public key content to the clipboard using `Set-Clipboard`.
Windows (PowerShell)
Get-Content "C:\Users\YourUsername\Desktop\CustomFolder\my_ssh_key.pub" | Set-Clipboard
Open PowerShell and connect to your Linux system using SSH. Replace `username`, `server_ip_address`, and `port_number` with your server's details.
Windows (PowerShell)
ssh -p port_number username@server_ip_address
On your Linux system, ensure that the `.ssh` directory exists by running the following command. This command creates the directory if it doesn't already exist (`-p` ensures any necessary parent directories are created).
Linux (Debian/Ubuntu)
mkdir -p ~/.ssh
Use a text editor like `nano` or `vim` to open or create the `authorized_keys` file:
Linux (Debian/Ubuntu)
nano ~/.ssh/authorized_keys
Paste the copied public key into the `authorized_keys` file. To paste in `nano`, `right-click` or use `Shift + Insert`.
Save and exit the editor. In `nano`, you can do this by pressing `Ctrl + X`, then `Y` to confirm changes, and `Enter` to exit.
Ensure the correct permissions are set for the `.ssh` directory and `authorized_keys` file:
Linux (Debian/Ubuntu)
chmod 700 ~/.ssh
chmod 600 ~/.ssh/authorized_keys
Apply the changes by restarting the SSH service on your Linux system:
Linux (Debian/Ubuntu)
sudo systemctl restart sshd
Use the `-i` option with the `ssh` command to specify the private key when connecting to the Linux system.
Windows (PowerShell)
ssh -i "C:\Users\YourUsername\Desktop\CustomFolder\my_ssh_key" -p port_number username@server_ip_address
Replace `username` with your server username, `server_ip_address` with the server's IP address or hostname, and `port_number` with the port number your SSH server is using.
By following these steps, you will successfully generate SSH keys, copy the public key to your Linux system, and configure your SSH client to use the private key for secure connections, specifying the port number each time.
Published: June 22, 2024
Have a question or suggestion? Want to request a tutorial or simply leave me a message? I'd love to hear from you! Join our community on Discord for exclusive content, engaging discussions, and more. Thank you! 🌟