Update your Debian based distributions in one step

Debian based distribution update

I cannot stress enough how important is to update your operating system. There are millions of reasons why to do it, and they are all pro, starting with security issues, stability of the system itself, feature upgrades and so on. Your operating system is like a plant; you need to water it on a daily basis and protect it from various parasites so it can grow and continue to be beautiful. This article will describe how to do your “tending and watering” with a help of a single script and all in one shot.

Introduction

Update your system! Right now, and make a habit of doing it. And do it at least once a week. You will be happy and your system will be grateful.

There are several ways you can update your operating system. If running a Desktop, there are a variety of GUI tools which can perform the task. Tools like Synaptic, Software Center, Kpackage, GNOME Software and so on. On server installations, there are no GUI tools, so you will use CLI tools like apt, apt-get, or aptitude which actually has a basic GUI and is also my preferred update tool. 

In this article I will describe how to write a small program which will update your system regardless of the distribution you use. Debian based of course.

First you need to download script here. Save it somewhere safe, and extract the archive content:

tar xzvf update_system.tar.gz

Initially, the script was made to “work” with kernel version 3.X, so if you have a newer distribution which uses kernel version 4.X, you will need to modify the script a bit before you put it to work. Open the script in your favorite text editor and locate the following lines:

dpkg -l | grep linux-image-extra-3 | tr -s " " | cut -d" " -f2 | sort -r | tail -n +2 | xargs aptitude purge -y | tee -a "upgrade.log"
dpkg -l | grep linux-image-3 | tr -s " " | cut -d" " -f2 | sort -r | tail -n +2 | xargs aptitude purge -y | tee -a "upgrade.log"
dpkg -l | grep linux-headers-3 | tr -s " " | cut -d" " -f2 | sort -r | tail -n +3 | tr "\n" " " | xargs aptitude purge -y | tee -a "upgrade.log"

Notice “linux-image-extra-3“, “linux-image-3” and “linux-headers-3“. Number 3 marks the kernel version, so if you’re using kernel version 4, just replace 3 with 4. Don’t know which kernel version you use? No problem, just execute the following in terminal:

sudo dpkg -l |grep linux-image
...
ii linux-image-4.4.0-83-generic
...

Now you can put the script to work, but please read the reminder of the article also 🙂

sudo ./update_system.sh

Update your system – Script overview

First block will execute the primary task which also is a topic of this guide; a full system update. It will also send the output of the script in a log file:

## Update system
aptitude update
aptitude full-upgrade --purge -y | tee -a "upgrade.log"

A log file is important for the following task this script will perform. It will analyze the log and check if new kernel is installed. If it detects a new kernel is installed, it will remove the old kernels:

## Check if kernel is upgraded
KERNEL_UPGRADE=$(cat upgrade.log |grep -q "linux-image-generic" ; echo $?)
if [ "$KERNEL_UPGRADE" == 0 ];
then
echo -e "${bldred}Kernel is upgraded. Will remove obsolete kernel and headers.${txtrst}"
dpkg -l | grep linux-image-extra-3 | tr -s " " | cut -d" " -f2 | sort -r | tail -n +2 | xargs aptitude purge -y | tee -a "upgrade.log"
dpkg -l | grep linux-image-3 | tr -s " " | cut -d" " -f2 | sort -r | tail -n +2 | xargs aptitude purge -y | tee -a "upgrade.log"
dpkg -l | grep linux-headers-3 | tr -s " " | cut -d" " -f2 | sort -r | tail -n +3 | tr "\n" " " | xargs aptitude purge -y | tee -a "upgrade.log"
else
echo -e "${bldgrn}No kernel upgrade, continuing.${txtrst}"
fi

Final block of the script will perform cleanup and finish the update task:

## CleanUp
rm -rf upgrade.log
aptitude clean

You can execute this script manually, or you can use cron and add a task which will perform the task automatically. Just make sure you run the script in regular intervals, like weakly at least. Of course, you can modify the script in any way you want. Thank you for reading.

Spread the knowledge...

Author: Zack

Zack can cook and administer systems. He likes to discuss both and share knowledge and wisdom to everyone interested. So go ahead and give him hello.

Leave a Reply

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