DEV Community

John  Ajera
John Ajera

Posted on

Configure SSH Passwordless Login from Windows to Linux

Configure SSH Passwordless Login from Windows to Linux

Setting up SSH passwordless login allows you to connect from your Windows machine to a Linux server securely without typing your password every time. This is particularly useful for automation, scripting, or regular remote management tasks.


Step 1: Check and Install OpenSSH Client

  1. Open PowerShell with administrative privileges:

    • Press Win + S, type PowerShell, right-click on Windows PowerShell, and select Run as Administrator.
  2. Check if OpenSSH Client is installed:

   Get-WindowsCapability -Online | Where-Object Name -like '*OpenSSH.Client*'
Enter fullscreen mode Exit fullscreen mode

If it shows State : NotPresent, install it:

   Add-WindowsCapability -Online -Name OpenSSH.Client~~~~0.0.1.0
Enter fullscreen mode Exit fullscreen mode

Step 2: Generate an SSH Key Pair on Windows

  1. Open Command Prompt or PowerShell. Both tools can execute the ssh-keygen command; choose the one you're more comfortable with or have easy access to.
  2. Run the SSH key generation command:
   ssh-keygen -t rsa -b 4096
Enter fullscreen mode Exit fullscreen mode

This command generates a secure 4096-bit RSA key, which is recommended for modern usage due to its robust security against brute-force attacks.

  1. When prompted:

    • Save the key to the default location (e.g., C:\Users\YourUsername\.ssh\id_rsa) by pressing Enter.
    • Leave the passphrase empty for passwordless login (press Enter twice).
  2. Example output:

   Generating public/private rsa key pair.
   Enter file in which to save the key (C:\Users\YourUsername\.ssh\id_rsa):
   Enter passphrase (empty for no passphrase):
   Enter same passphrase again:
   Your identification has been saved in C:\Users\YourUsername\.ssh\id_rsa
   Your public key has been saved in C:\Users\YourUsername\.ssh\id_rsa.pub
Enter fullscreen mode Exit fullscreen mode

Step 3: Copy the SSH Public Key to the Linux Server

To enable passwordless login, your public key must be on the Linux server.

  1. Use the following command in PowerShell to append the public key directly to the authorized_keys file on the Linux server:
   type $env:USERPROFILE\.ssh\id_rsa.pub | ssh username@hostname-or-ip "mkdir -p .ssh && chmod 700 .ssh && cat >> .ssh/authorized_keys && chmod 600 .ssh/authorized_keys"
Enter fullscreen mode Exit fullscreen mode

Replace:

  • username with your Linux username.
  • hostname-or-ip with the IP address or hostname of your Linux server.
  1. Ensure the correct permissions for the .ssh directory and authorized_keys file are applied as part of the above command.

Step 4: Test the Passwordless SSH Connection

From your Windows machine, test the connection:

  1. Open Command Prompt or PowerShell.
  2. Run the SSH command:
   ssh username@hostname-or-ip
Enter fullscreen mode Exit fullscreen mode

You should log in without being prompted for a password.


Troubleshooting

If the connection still prompts for a password:

  1. Verify the SSH key is correctly copied to the Linux server:
   cat ~/.ssh/authorized_keys
Enter fullscreen mode Exit fullscreen mode

Ensure it contains the public key from your Windows machine.

  1. Check SSH server permissions and configuration:

    • Ensure ~/.ssh has 700 permissions and authorized_keys has 600.
    • Restart the SSH service on Linux:
     sudo systemctl restart sshd
    
  2. Check firewall or network configurations:

    • Ensure port 22 (the default SSH port) is open on the Linux server.
    • Verify there are no IP-based restrictions preventing your connection.
    • For cloud environments, confirm that the security group or network rules allow SSH access.
  3. Enable debugging during SSH login:

   ssh -vvv username@hostname-or-ip
Enter fullscreen mode Exit fullscreen mode

Analyze the output for errors.


Conclusion

Configuring SSH passwordless login from Windows to Linux improves efficiency and security for remote connections. By setting up SSH keys, you eliminate the need for passwords while maintaining secure access. This setup is particularly useful for frequent logins, automated scripts, and system management.

Feel free to leave a comment if you encounter any issues or have tips to share! 😊

Top comments (0)