For the Windows server:
- Open the Remote Desktop Connection (RDP) client and connect to your Windows server.
- Open the 'Microsoft Store' and search for 'Windows Terminal'. Install and open the Windows Terminal as admin.
- In the Windows Terminal, run the following command to open the SSH configuration file:
If the
notepad.exe %UserProfile%\.ssh\authorized_keys
.ssh
folder doesn't exist, create it first. - Copy the contents of your Linux client's
id_rsa.pub
file (the public key) and paste it into theauthorized_keys
file. - Save the
authorized_keys
file and close Notepad.
Now, you should be able to connect to your Windows server using SSH from your Linux client. Here's how:
- In the Linux terminal, run the following command:
Replace
ssh username@windows_server_ip
username
with the appropriate user account on the Windows server, andwindows_server_ip
with the IP address or hostname of your Windows server. - If you set a passphrase for your SSH key, you will be prompted to enter it. If not, you should be logged in automatically.
That's it! You have now set up SSH access from your Linux client to your Windows server using an SSH key pair. This method is more secure than using a password-based authentication, as it eliminates the need to remember and manage complex passwords.
First, install OpenSSH server inside your Linux Distro:
sudo apt install openssh-server
-
Open the terminal on your Linux machine.
-
Check if you already have an SSH key pair by running the following command:
ls -al ~/.ssh
If you see files named
id_rsa
andid_rsa.pub
, you already have a key pair. -
If you don't have a key pair, you can generate one by running the following command:
ssh-keygen -t rsa -f ~/.ssh/id_rsa
This will create a 4096-bit RSA key pair and associate it with the email address you provided.
-
When prompted, choose a location to save the key files and enter a passphrase (optional) or just press ENTER to skip passphrase.
-
Your public key (
id_rsa.pub
) is now ready to be copied to the Windows server. -
cat ~/.ssh/id_rsa.pub
-
copy the pub key (no whitespaces)
- open powershell as admin
- In the Terminal, run the following command to open the SSH configuration file:
notepad.exe %UserProfile%\.ssh\authorized_keys
If the .ssh
folder doesn't exist, create it first.
Copy the contents of your Linux client's id_rsa.pub
file (the public key) and paste it into the authorized_keys
file.
Save the authorized_keys
file and close Notepad.
Run the command below. The output shows True
when you're a member of the built-in Administrators group.
(New-Object Security.Principal.WindowsPrincipal([Security.Principal.WindowsIdentity]::GetCurrent())).IsInRole([Security.Principal.WindowsBuiltInRole]::Administrator)
-
If the output shows
True
you are an admin user. Copy the public key to administrators_authorized_keys:cp %UserProfile%\.ssh\authorized_keys "$env:programdata\ssh\administrators_authorized_keys"
or
cp $env:USERPROFILE\.ssh\authorized_keys "$env:programdata\ssh\administrators_authorized_keys"
_Please note administrators_authorized_keys is the file name without any extension.
`get-acl "$env:programdata\ssh\ssh_host_rsa_key" | set-acl "$env:programdata\ssh\administrators_authorized_keys"`
_If you don't have .ssh folder under $env:USERPROFILE folder then manually create it_
ssh your-windows-username@you-windows-pc-name (gets you into the windows machine) ssh your-wsl-username@ip (gets you into the wsl-linux environment
Here are the steps to create an SSH key pair and set up SSH access on a Windows client and a Linux server:
-
Open PowerShell or Command Prompt as an administrator.
-
Generate an SSH key pair:
ssh-keygen -t rsa -b 4096 -f %USERPROFILE%\.ssh\id_rsa
This will create the private key id_rsa
and the public key id_rsa.pub
in the %USERPROFILE%\.ssh
directory.
- Copy the contents of the public key file
%USERPROFILE%\.ssh\id_rsa.pub
.
-
Open a terminal and navigate to the
/home/username/.ssh
directory, whereusername
is the name of the user you want to grant SSH access to. -
If the
.ssh
directory doesn't exist, create it:
mkdir ~/.ssh
- Create or edit the
authorized_keys
file and paste the content of the public key you copied from the Windows client:
nano ~/.ssh/authorized_keys
-
Save the file and exit the text editor.
-
Set the correct permissions for the
.ssh
directory and theauthorized_keys
file:
chmod 700 ~/.ssh
chmod 600 ~/.ssh/authorized_keys
Now, the Linux server is configured to accept SSH connections from the Windows client using the provided SSH key pair.
To connect from the Windows client to the Linux server using the SSH key, use the following command:
ssh -i %USERPROFILE%\.ssh\id_rsa username@linux_server_ip
Replace username
with the username on the Linux server and linux_server_ip
with the IP address or hostname of the Linux server.
This setup allows you to connect to the Linux server from the Windows client without using a password, as the SSH key is used for authentication.