How to install software on multiple servers at the same time— Ubuntu/Centos/Redhat

Krishna Wattamwar
3 min readJun 10, 2021

What Does Shell Script Mean?

A shell script is small computer program that is designed to be run or executed by the Unix shell, which is a command-line interpreter. A shell script is basically a set of commands that the shell in a Unix-based operating system follows. Like actual programs, the commands in the shell script can contain parameters and subcommands that tell the shell what to do. The shell script is usually contained in a simple text file.

How to install software on multiple servers

you can set up passwordless access to each of the machines easily enough using this below approach:

Method — 1

SSHPASS

The sshpass utility is designed to run SSH using the keyboard-interactive password authentication mode, but in a non-interactive way.

  1. install sshpass on your local machine:
sudo apt-get install sshpass

This will allow you to pass the password as a command line argument:

sshpass -p '<password>' ssh user@server

2. Create an ssh-keygen

ssh-keygen -t rsa

You can simplify things by allowing an empty passphrase (the rest of this answer will assume you have done so, let me know if your security concerns prohibit this and I will modify accordingly).

3. Create a file with all the IPs you are interested in and their respective username and passwords, one per line (vi multiserver.txt)

<IP ADDRESS> <USERNAME> <PASSWORD>
11.22.33.44 hary harrys_password
12.22.25.55 bob bob_password

Now, use sshpass to copy your key files and as long as you have used an empty passphrase to allow passwordless access to all machines:

#! /bin/bashwhile read ip user pass; 
do
sshpass -p "$pass" ssh ssh-copy-id -i ~/.ssh/id_rsa.pub $user@$ip;
done < multiserver.txt

4. Now we have successfully done the passwordless access to all the listed servers, install your software on each machine (this assumes that $user can run apt-get, basically that $user is root):

#! /bin/bashwhile read ip user pass; do 
ssh $user@$ip "apt-get install package";
done < multiserver.txt

Method — 2

If you have already set password-less authentication then you can just use following script :

Create a file with all the IPs you are interested in and their respective username, one per line (vi multiserver.txt)

<USERNAME><IP ADDRESS>
sunil@11.22.33.44
krishna@92.27.28.05

Script as below:

#! /bin/bash

SERVER_LIST=/user/krishna/multiserver.txt
PackageName="PackageName"

for Host in $(< $SERVER_LIST )
do
echo "Installing package on $Host"
ssh "${Host}" apt-get -y install "${PackageName}"

done

If you don’t have password less authentication then you can use method-1

Conclusion

We have seen How to install software on multiple servers. You need to ensure that your script has execute permission.

--

--