Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Passwordless ssh connection from Windows

Tags:

linux

windows

ssh

How can I create an ssh key from Windows and install it on a Linux host using OpenSSH to log in without a password for each connection?

like image 371
Majico Avatar asked Sep 05 '25 03:09

Majico


1 Answers

CREATE AND INSTALL SSH KEY

First of all, we need to create a new key in the Windows pc (where we start the connection) using:

ssh-keygen -t rsa

Don't change the default path or remember where you saved the key, it will be used for the next command. Press enter another two times to avoid using a passphrase (if you don't want it).

After that, if you haven't change the default path, the key will be created into {USERPROFILE}\.ssh\id_rsa.pub.

Now, you can usually use the command ssh-copy-id for installing the key on the remote host, but unfortunately this command is not available on Windows, so we have to install it using this command:

type $env:USERPROFILE\.ssh\id_rsa.pub | ssh {REMOTE_HOST} "cat >> .ssh/authorized_keys"

or if your key is not in the default path:

type {RSA_KEY_PATH} | ssh {REMOTE_HOST} "cat >> .ssh/authorized_keys"

and replace the {RSA_KEY_PATH} with your RSA path.

Replace {REMOTE_HOST} with the remote host IP/Name (like [email protected]), launch the command, insert the password if required, and the work is done!

IMPORTANT!


SETTING UP .ssh FOLDER

If the ~/.ssh folder is not existing in your remote host, you need to configure them, this is usually done by the command ssh-copy-id, but we can not access to this power from Windows! You need to connect to the remote host in ssh and create the .ssh directory and the authorized_keys file for the first time:

ssh {REMOTE_HOST}
  1. Create the .ssh directory:
mkdir ~/.ssh
  1. Set the right permissions:
chmod 700 ~/.ssh
  1. Create the authorized_keys file:
touch ~/.ssh/authorized_keys
  1. Set the right permissions:
chmod 600 ~/.ssh/authorized_keys

NOTE

The authorized_keys is not a folder, if you try to create it using mkdir, the SSH connection passwordless will not work, and if you debug the ssh on the host, you will notice an error/log similar to:

~/.ssh/authorized_keys is not a key file.


ADD YOUR SSH KEY ON YOUR AGENT

Run those two lines on your Windows pc to add the created key on your cmd/powershell:

ssh-agent $SHELL
ssh-add
like image 169
Majico Avatar answered Sep 08 '25 00:09

Majico



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!