Setup Passwordless SSH Login for Multiple Remote Servers Using Script

Setup Passwordless SSH Login for Multiple Remote Servers Using Script

 

SSH Key-based authentication (also referred to as public-key authentication) permits for password-less authentication and it’s a safer and a a lot better resolution than password authentication. One main benefit of SSH password-less login, not to mention safety is that it permits for automation of varied sorts of cross-server processes.

On this article, we are going to show how one can create an SSH key pair and replica the general public key to a number of distant Linux hosts without delay, with a shell script.

Create a New SSH Key in Linux

First, generate the SSH key pair (the personal/identification key that an SSH consumer makes use of to authenticate itself when logging right into a distant SSH server and the general public key saved as a licensed key on a distant system working an SSH server) utilizing the ssh-keygen command as follows:

# ssh-keygen

Setup Passwordless SSH Login for Multiple Remote Servers Using ScriptGenerate SSH Key in Linux

Create a Shell Script for Mulitple Distant Logins

Subsequent, create a shell script that may assist in copying a public key to a number of distant Linux hosts.

# vim ~/.bin/ssh-copy.sh

Copy and paste the next code within the file (substitute the next variables accordingly USER_NAME – the username to attach with, HOST_FILE – a file which accommodates the record of hostnames or IP addresses, and ERROR_FILE – a file to retailer any ssh command errors).

#!/bin/bash
USER_NAME=”root”
HOST_FILE=”/root/hosts”
ERROR_FILE=”/tmp/ssh-copy_error.txt”
PUBLIC_KEY_FILE=”$1″

if [ ! -f $PUBLIC_KEY_FILE ]; then
echo “File ‘$PUBLIC_KEY_FILE’ not discovered!”
exit 1
fi

if [ ! -f $HOST_FILE ]; then
echo “File ‘$HOST_FILE’ not discovered!”
exit 2
fi

for IP in `cat $HOST_FILE`; do
ssh-copy-id -i $PUBLIC_KEY_FILE [email protected]$IP 2>$ERROR_FILE
RESULT=$?
if [ $RESULT -eq 0 ]; then
echo “”
echo “Public key efficiently copied to $IP”
echo “”
else
echo “$(cat $ERROR_FILE)”
echo
exit 3
fi
echo “”
achieved

Save the file and shut it.

Then make the script executable with the chmod command as proven.

# chmod +x ssh-copy.sh

Now run the ssh-copy.sh script and specify your public key file as the primary argument as proven within the screenshot:

# ./ssh-copy.sh /root/.ssh/prod-rsa.pub

Setup Passwordless SSH Login for Multiple Remote Servers Using ScriptRun SSH Copy Script

Subsequent, use ssh-agent to handle your keys, which holds your decrypted personal key in reminiscence and makes use of it to authenticate logins. After beginning the ssh-agent, add your personal key to it as follows:

# eval “$(ssh-agent -s)”
# ssh-add ~/.ssh/prod_rsa

Setup Passwordless SSH Login for Multiple Remote Servers Using ScriptBegin SSH Agent

Login to Distant Linux Server with out Password

Now you may log into any of your distant hosts with out offering a password for SSH consumer authentication. This fashion, you may automate cross-server processes.

# ssh [email protected]

Setup Passwordless SSH Login for Multiple Remote Servers Using ScriptSSH Passwordless Login

That’s all we had for you! When you have any contribution(s) to make notably in the direction of bettering the shell script, tell us by way of the suggestions type beneath.

If You Respect What We Do Right here On TecMint, You Ought to Think about:

TecMint is the quickest rising and most trusted neighborhood web site for any sort of Linux Articles, Guides and Books on the net. Thousands and thousands of individuals go to TecMint! to go looking or browse the hundreds of revealed articles accessible FREELY to all.

In case you like what you’re studying, please take into account shopping for us a espresso ( or 2 ) as a token of appreciation.

Setup Passwordless SSH Login for Multiple Remote Servers Using Script

We’re grateful on your by no means ending help.

ssh without password mac,ssh without password windows,passwordless ssh linux,ssh without password stack overflow,passwordless ssh raspberry pi,passwordless ssh not working,passwordless ssh digitalocean,passwordless ssh from windows to linux,passwordless ssh ansible,pssh ssh-copy-id,ssh key propagation,ssh-copy-id multiple servers,shell script for ssh copy id,echo password ssh copy id,ssh-copy-id equivalent in aix