Update multiple Linux servers and distributions

Overview

Update multiple Linux servers and distributions using any Linux Distribution.

Update multiple Linux servers and distributions

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:

1
ssh-keygen (do not use a password - just hit Enter)
ssh-keygen (do not use a password - just hit Enter)

Copy (and rename) id_rsa.pub to authorized_keys on Server

1
2
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
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.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
#!/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)
#!/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

1
chmod +x updateall.sh
chmod +x updateall.sh

Create the list file, a file called ‘updateall.lst’ with the following structure:

hsotnames/ip, port, Linux distribution of the servers (each hostname/ip in a separate row)
for example:

1
2
ServerC 22 Ubuntu
ServerU 22 CentOS
ServerC 22 Ubuntu
ServerU 22 CentOS

 

Manage automatic updates

For example run the ‘updateall.sh’ script every Saturday at 01:05 :

1
2
crontab -e
1 5 * * 6 /scripts/updateall.sh
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.

Leave a Reply

Your email address will not be published. Required fields are marked *

*

This site uses Akismet to reduce spam. Learn how your comment data is processed.