Setting Up SSH Passwordless Login

For the purpose of automating CLI scripting

LAI TOCA
2 min readMay 16, 2024
Created by https://www.bing.com/images/create

Automatic scripted automation for accessing remote servers could be potentially necessary, such as when you have Kubernetes on the remote server that needs deployment and management. Passwordless login becomes the very first step in this process. This article will provide a step-by-step guide on how to set up SSH passwordless login.

Step-1: Generating an SSH key pair via PowerShell (private key: id_rsa, public key: id_rsa.pub)

> ssh-keygen

The keys are usually stored under the path: C:\Users\{USERNAME}.ssh for Windows OS.
Protecting the key with a passphrase would be necessary for security concerns.

Step-2: Creating an SSH directory on target remote server (if it does not exist) and uploading the public key to authorzied_keys file via PowerShell.

> type C:\Users\{USERNAME}\.ssh\id_rsa.pub | ssh user@linuxhost "umask 077; test -d .ssh || mkdir .ssh ; cat >> .ssh/authorized_keys || exit 1"

Note that we should append to the existing file rather than overwrite it. Otherwise, existing users might unintentionally lose access.

Step-3: Checking authorzied_keys on target remote server.

cat .ssh/authorzied_keys

Output: ssh-rsa AAAAB3NzaC1yc2EAAAABIwAAAQEAnvYlVooXXXXX…XXXXXX

Step-4: Enabling SSH agent. Start an OpenSSH Authentication Agent a and use the command below to guide your agent to keep your key and passphrase.

> ssh-add
Enable and start OpenSSH Authentication Agent
Enter the passphrase

With this step, we could avoid entering the SSH key passphrase every time we log in.

Step-5: Testing to see if we can access the remote server using SSH without a password.

> ssh user@linuxhost "ls -al /tmp/"

Step-6: Backup and safe-keeping your private key.

Reference

--

--

LAI TOCA

Coding for fun. (Either you are running for food or running for being food.)