Home > Bash, Scripting > Update remote RHEL and CentOS servers

Update remote RHEL and CentOS servers

November 22nd, 2010 Leave a comment Go to comments

terminal Update remote RHEL and CentOS servers

If you are using Red Hat Enterprise Linux probably you are also using RHN to manage updates and software your servers but the same does not hold true if you are using CentOS without tools like Puppet or Spacewalk, so how do you update remote servers without having to connect and update one by one?

The answer is pretty simple, BASH scripting. Below you can find a small and simple script that will take care of updating multiple RHEL/CentOS servers simultaneously without any intervention on your part. For this to work the only requirement is that you have configured SSH pass-wordless login as described in the lined post Cron SCP so that the script/machine will be able to login to the remote server to update automatically.

<pre>#!/bin/bash
# A very simple script used to update all remote RHEL/CentOS servers
# An example for a server using a custom SSH port has been included
# If you don't want to use root to connect to the remote machine
# An example on how to use sudo to accomplish this has been included as well
#
# -------------------------------------------------------------------------
# Copyright (c) 2010 Lethe's Tuxforge <http://blog.tuxforge.com>
# This script is licensed under GNU GPL version 2.0 or above
# -------------------------------------------------------------------------

# We define an array to store servers and commands to be executed
servers=(
	"ssh root@server1.tuxforge.com yum update -y"
	"ssh root@server2.tuxforge.com -p9999 yum update -y"
	"ssh root@server3.yourdomain.com yum update -y"
	"ssh lethe@X.X.X.X -t sudo  '/usr/bin/yum update -y' "
      )

# Cycle through the array items and execute the command

for s in "${servers[@]}"
do
	$s
done 

As an habit I put verbose comments in my scripts to document what they are doing and why so, I tend to forget stuff easily, but as you can see I’ve included examples for servers using custom SSH ports and also an example on how to use SUDO instead of having the server connect using the root user account (which I highly recommend).

Feel free to download the script and modify it to your needs, the script is pretty simple and basic so a good idea could be modifying it to make it more “complete” like for example having the script to main you the list of updates that have been installed for each of the servers so that you can take proper action like a reboot.

I hope you will find it useful if so please take a second to retweet the article or to leave a comment, if you have any suggestion or enhancement don’t hesitate to let me know and I will be more than glad to add it to the post!

Cheers Lethe.

VN:F [1.9.20_1166]
Rating: 5.0/5 (1 vote cast)
Update remote RHEL and CentOS servers, 5.0 out of 5 based on 1 rating

Incoming search terms:

Categories: Bash, Scripting