Overview
A Bash script that connects to multiple remote Linux servers over SSH and runs the appropriate update commands based on each server’s distribution (CentOS/RHEL or Debian/Ubuntu).

Requirements
- Linux Server/Desktop manger (in this article we will use CentOS - we will call it MainServer) - Linux Servers/Desktops to manage (we will use CentOS and Ubuntu - names: ServerC, ServerU) - SSH Connection - Root Access
establish SSH connection without password between the MainServer and ServerC,ServerU
Generate id_rsa, id_rsa.pub in the MainServer:
ssh-keygen (do not use a password - just hit Enter)
Copy (and rename) id_rsa.pub to authorized_keys on Server
scp -r -P 22 /root/.ssh/id_rsa.pub ServerC:/root/.ssh/authorized_keys
scp -r -P 22 /root/.ssh/id_rsa.pub ServerU:/root/.ssh/authorized_keys
You can connect now as root between the servers without a password.
Create the manager script
Create the script to update multiple Linux servers, a file called updateall.sh:
Create a folder called /scripts/ (you can use whatever you want) We will store all files within that folder.
#!/bin/bash
workdir=/scripts
logfile=$workdir/updaterun.run
listfile=$workdir/updateall.lst
(
cd $workdir/
if [ -a $logfile ] ; then
echo ""
echo "Script Is Running! check " $logfile
echo ""
exit;
fi
echo "**************************************"
echo "* Starting update process..." $(date +%y%m%d) "*"
echo "**************************************"
cat $listfile | while read line
do
dist=$(echo $line | awk '{print $NF}')
case $dist in
'CentOS'|'RHEL')
server=$(echo $line | awk '{print($(NF-2))}')
port=$(echo $line | awk '{print($(NF-1))}')
echo ""
echo "**************************************"
echo "***** " $server " " $dist
echo "**************************************"
echo ""
ssh -n -l root -p $port $server 'yum update -y -y'
;;
'Debian'|'Ubuntu')
server=echo $line | awk '{print($(NF-2))}'
port=echo $line | awk '{print($(NF-1))}'
echo ""
echo "**************************************"
echo "* " $server " " $dist " *"
echo "**************************************"
echo ""
ssh -n -l root -p $port $server 'apt-get update -y'
ssh -n -l root -p $port $server 'apt-get upgrade -y'
;;
*)
echo "**************************************"
echo "* Unknown Linux Distribution *"
echo "**************************************"
echo ""
;;
esac
done
echo ""
echo "************************************************************"
echo "** Update multiple Linux servers and distributions Done **"
echo "************************************************************"
echo ""
) 2>&1 | tee -a $logfile
mv $logfile /scripts/log/update-$(date +%y%m%d)
save the file and give execute permissions
chmod +x updateall.sh
Create the list file, a file called ‘updateall.lst’ with the following structure:
hostnames/ip, port, Linux distribution of the servers (each hostname/ip in a separate row) for example:
ServerC 22 Ubuntu
ServerU 22 CentOS
Manage automatic updates
For example run the ‘updateall.sh’ script every Saturday at 01:05 :
crontab -e
1 5 * * 6 /scripts/updateall.sh
This is an explanation of crontab:
| Minute | Hour | Day of Month | Month | Day of Week | Command |
|---|---|---|---|---|---|
| (0-59) | (0-23) | (1-31) | (1-12 or Jan-Dec) | (0-6 or Sun-Sat) |
Don’t forget to check /scripts/log/ files to see that everything is in order once in a while.
