How to use yum-cron to automatically update RHEL/CentOS Linux 6.x / 7.x
The yum command line tool is used to install and update software packages under RHEL / CentOS Linux server. I know how to apply updates using yum update command line, but I would like to use cron to update packages where appropriate manually. How do I configure yum to install software patches/updates automatically with cron?
You need to install yum-cron package. It provides files needed to run yum updates as a cron job. Install this package if you want auto yum updates nightly via cron.
How to install yum cron on a CentOS/RHEL 6.x/7.x
Type the following yum command on:$ sudo yum install yum-cron
Turn on service using systemctl command on CentOS/RHEL 7.x:$ sudo systemctl enable yum-cron.service
$ sudo systemctl start yum-cron.service
$ sudo systemctl status yum-cron.service
If you are using CentOS/RHEL 6.x, run:$ sudo chkconfig yum-cron on
$ sudo service yum-cron start
yum-cron is an alternate interface to yum. Very convenient way to call yum from cron. It provides methods to keep repository metadata up to date, and to check for, download, and apply updates. Rather than accepting many different command line arguments, the different functions of yum-cron can be accessed through config files.
How to configure yum-cron to automatically update RHEL/CentOS Linux
You need to edit /etc/yum/yum-cron.conf and /etc/yum/yum-cron-hourly.conf files using a text editor such as vi command:$ sudo vi /etc/yum/yum-cron.conf
Make sure updates should be applied when they are availableapply_updates = yes
You can set the address to send email messages from. Please note that ‘localhost’ will be replaced with the value of system_name.email_from = root@localhost
List of addresses to send messages to.email_to = your-it-support@some-domain-name
Name of the host to connect to to send email messages.email_host = localhost
If you do not want to update kernel package add the following on CentOS/RHEL 7.x:exclude=kernel*
For RHEL/CentOS 6.x add the following to exclude kernel package from updating:YUM_PARAMETER=kernel*
Save and close the file in vi/vim. You also need to update /etc/yum/yum-cron-hourly.conf file if you want to apply update hourly. Otherwise /etc/yum/yum-cron.conf will run on daily using the following cron job (us cat command:$ cat /etc/cron.daily/0yum-daily.cron
Sample outputs:
That is all. Now your system will update automatically everyday using yum-cron. See man page of yum-cron for more details:$ man yum-cron
#!/bin/bash
LAST_KERNEL=$(rpm -q --last kernel | perl -pe 's/^kernel-(\S+).*/$1/' | head -1)
CURRENT_KERNEL=$(uname -r)
test $LAST_KERNEL = $CURRENT_KERNEL || shutdown -r
Taken from this thread https://serverfault.com/questions/122178/how-can-i-check-from-the-command-line-if-a-reboot-is-required-on-rhel-or-centos/311733#311733
Don't forget that you might need to reboot because of core library updates, at least if it is glibc. (And also, services may need to be restarted after updates).
If you install the yum-utils package, you can use a command called needs-restarting
.
You can use it both for checking if a full reboot is required because of kernel or core libraries updates (using the -r
option), or what services need to be restarted (using the -s
option.
needs-restarting -r
returns 0 if reboot is not needed, and 1 if it is, so it is perfect to use in a script.
An example:
root@server1:~> needs-restarting -r ; echo $?
Core libraries or services have been updated:
openssl-libs -> 1:1.0.1e-60.el7_3.1
systemd -> 219-30.el7_3.9
Reboot is required to ensure that your system benefits from these updates.
More information:
https://access.redhat.com/solutions/27943
1
https://access.redhat.com/solutions/10021