Logo
  • Ubuntu
  • CentOS
  • Debian
  • Fedora
  • RedHat

Setup Passwordless SSH Login for Multiple Remote Servers Using Script - DesignLinux

Oct 05 2020
designlinux 0 Comments

SSH Key-based authentication (also known as public-key authentication) allows for password-less authentication and it is a more secure and a much better solution than password authentication. One major advantage of SSH password-less login, let alone security is that it allows for automation of various kinds of cross-server processes.

Related Read: How to Secure and Harden OpenSSH Server

In this article, we will demonstrate how to create an SSH key pair and copy the public key to multiple remote Linux hosts at once, with a shell script.

Create a New SSH Key in Linux

First, generate the SSH key pair (the private/identity key that an SSH client uses to authenticate itself when logging into a remote SSH server and the public key stored as an authorized key on a remote system running an SSH server) using the ssh-keygen command as follows:

# ssh-keygen
Generate SSH Key in Linux
Generate SSH Key in Linux

Create a Shell Script for Mulitple Remote Logins

Next, create a shell script that will help in copying a public key to multiple remote Linux hosts.

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

Copy and paste the following code in the file (replace the following variables accordingly USER_NAME – the username to connect with, HOST_FILE – a file which contains the list of hostnames or IP addresses, and ERROR_FILE – a file to store 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 found!"
        exit 1
fi

if [ ! -f $HOST_FILE ]; then
        echo "File '$HOST_FILE' not found!"
        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 successfully copied to $IP"
                echo ""
        else
                echo "$(cat  $ERROR_FILE)"
                echo 
                exit 3
        fi
        echo ""
done

Save the file and close it.

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

# chmod +x ssh-copy.sh

Now run the ssh-copy.sh script and specify your public key file as the first argument as shown in the screenshot:

# ./ssh-copy.sh /root/.ssh/prod-rsa.pub
Run SSH Copy Script
Run SSH Copy Script

Next, use ssh-agent to manage your keys, which holds your decrypted private key in memory and uses it to authenticate logins. After starting the ssh-agent, add your private key to it as follows:

# eval "$(ssh-agent -s)"
# ssh-add  ~/.ssh/prod_rsa
Start SSH Agent
Start SSH Agent

Login to Remote Linux Server without Password

Now you can log into any of your remote hosts without providing a password for SSH user authentication. This way, you can automate cross-server processes.

# ssh [email protected]
SSH Passwordless Login
SSH Passwordless Login

That’s all we had for you! If you have any contribution(s) to make particularly towards improving the shell script, let us know via the feedback form below.

Related

Tags: CentOS Tips, Linux Security Tips, RHEL Tips, SSH Tips, Ubuntu Tips

BackUp and Restore MySQL/MariaDB Database

Prev Post

Df Command in Linux (Check Disk Space)

Next Post
Archives
  • January 2023
  • December 2022
  • November 2022
  • October 2022
  • September 2022
  • July 2022
  • June 2022
  • April 2022
  • March 2022
  • February 2022
  • January 2022
  • December 2021
  • November 2021
  • October 2021
  • September 2021
  • August 2021
  • July 2021
  • June 2021
  • May 2021
  • April 2021
  • March 2021
  • February 2021
  • January 2021
  • December 2020
  • November 2020
  • October 2020
  • September 2020
  • August 2020
  • July 2020
  • June 2020
  • May 2020
Categories
  • AlmaLinux
  • Android
  • Ansible
  • Apache
  • Arch Linux
  • AWS
  • Backups
  • Bash Shell
  • Bodhi Linux
  • CentOS
  • CentOS Stream
  • Chef
  • Cloud Software
  • CMS
  • Commandline Tools
  • Control Panels
  • CouchDB
  • Data Recovery Tools
  • Databases
  • Debian
  • Deepin Linux
  • Desktops
  • Development Tools
  • Docker
  • Download Managers
  • Drupal
  • Editors
  • Elementary OS
  • Encryption Tools
  • Fedora
  • Firewalls
  • FreeBSD
  • FTP
  • GIMP
  • Git
  • Hadoop
  • HAProxy
  • Java
  • Jenkins
  • Joomla
  • Kali Linux
  • KDE
  • Kubernetes
  • KVM
  • Laravel
  • Let's Encrypt
  • LFCA
  • Linux Certifications
  • Linux Commands
  • Linux Desktop
  • Linux Distros
  • Linux IDE
  • Linux Mint
  • Linux Talks
  • Lubuntu
  • LXC
  • Mail Server
  • Manjaro
  • MariaDB
  • MongoDB
  • Monitoring Tools
  • MySQL
  • Network
  • Networking Commands
  • NFS
  • Nginx
  • Nodejs
  • NTP
  • Open Source
  • OpenSUSE
  • Oracle Linux
  • Package Managers
  • Pentoo
  • PHP
  • Podman
  • Postfix Mail Server
  • PostgreSQL
  • Python
  • Questions
  • RedHat
  • Redis Server
  • Rocky Linux
  • Security
  • Shell Scripting
  • SQLite
  • SSH
  • Storage
  • Suse
  • Terminals
  • Text Editors
  • Top Tools
  • Torrent Clients
  • Tutorial
  • Ubuntu
  • Udemy Courses
  • Uncategorized
  • VirtualBox
  • Virtualization
  • VMware
  • VPN
  • VSCode Editor
  • Web Browsers
  • Web Design
  • Web Hosting
  • Web Servers
  • Webmin
  • Windows
  • Windows Subsystem
  • WordPress
  • Zabbix
  • Zentyal
  • Zorin OS
Visits
  • 0
  • 606
  • 1,055,378

DesignLinux.com © All rights reserved

Go to mobile version