Scripts to update the Raspberry Pi and Debian-based Linux Distros

UPDATE: From the comments, I ended up consolidating this update script into one big script. Therefore, I re-worked some of this blog post since it was originally written. Sorry for any confusions this causes!

If you are like me, you want to keep your software “topped-off” on the latest versions. Well, how do you do that? Well, if you have a Debian-based Linux distribution like:

  • Kali Linux
  • LinuxMint
  • Ubuntu Linux
  • Raspbian (running on Raspberry Pi, for example)
  • and many, many more

then these use the Apt package manager. So, I’ve created an update script that does:

  1. Updates the cache
  2. Upgrades all existing packages
  3. Upgrades the Linux distribution
  4. If it’s a Raspberry Pi device, go update the firmware
  5. Cleans up unnecessary and cached packages
  6. Prompts you to reboot.

This is how I keep all of my Debian-based servers and workstations current (including all Raspberry Pi’s running Raspbian).

What do the scripts do?
Here is a copy of the script which also updates the Raspberry Pi firmware:

#!/bin/bash

logger "Update Script: Starting..."

echo '[*] Refreshing repository cache...'
sudo apt-get update -y
echo '[*] Repository cache refreshed.'

echo '[*] Upgrading all existing packages...'
sudo apt-get upgrade -y
echo '[*] Existing packages upgraded.'

echo '[*] Upgrading Linux distribution (if available)...'
sudo apt-get dist-upgrade -y
echo '[*] Linux distribution upgrade processed.'

echo '[*] Clean up unused and cached packages...'
sudo apt-get autoclean -y
sudo apt-get autoremove -y
echo '[*] Package cleanup complete.'

if [ $(which raspi-config | wc -l) -gt 0 ]; then
        echo '[*] Raspberry Pi Detected.'
        echo '[*] Update the Raspberry Pi firmware to the latest (if available)...'
        sudo rpi-update
        echo '[*] Done updating firmware.'
fi

logger "Update Script: Done."

while true; do
        read -r -p "Do you wish to reboot? " choice
        case "$choice" in
                y|Y ) echo "[*] Rebooting..."; sudo reboot; break;;
                n|N ) echo "[*] Done."; break;;
                * ) echo "[-] Invalid response. Use 'y' or 'n'.";;
        esac
done

As you can see, this does all of the main things you’d want to do when updating a system. I call this “update.sh” – and I call it whenever I want to update the current system. The “logger” line writes a message to syslog. This can be useful for troubleshooting that you write an entry when you start this, and then you can see everything that happened in system up until when the script ends.

Where can I find the script?
I created two GitHub Gists here:

https://gist.github.com/RobSeder/24af1cd7c195a54fe8b0

You click click on the “Raw” link to get the raw text and save the file.

How can I easily install the script?
I was trying to find an easy way to get the scripts, and then mark them as executable. On Linux servers, you don’t run a GUI, so you can’t use a browser. So, you need some command-line way to retrieve the scripts.

I prefer wget over curl (for some unknown reason), so using wget, you can run this command to get the script and then mark it as executable:

$ # Getting: update.sh
$ cd ~
$ wget https://gist.githubusercontent.com/RobSeder/24af1cd7c195a54fe8b0/raw/fd06d9241965de54a6ad44a833a93226eabfcd69/update.sh
&& chmod +x ./update.sh

This changes you into your home directory, and then downloads the file into that folder. Then, the file is marked as executable. To run it, just do:

$ ./update.sh

When done, it will prompt if you want to reboot. One of the reasons I’m writing this blog post is that when I build a new Linux machine (which I am doing right now for a future post), I want to SSH into it, navigate to this web page, and copy/paste that wget line – then run ./update.sh to update the system.

Another way to do this, which might be easier to remember is:

$ cd ~$ wget https://tinyurl.com/debupdate-sh -O ./update.sh && chmod +x ./update.sh

Bottom line:
This is the script I use because I use Debian-based distributions almost-exclusively. So, this is a quick/easy way to get and run a script which will keep your Linux system updated to the very latest. If you have something else or ideas for improvements, please leave a comment below!

Posted in Computers and Internet, General, Infrastructure, Linux, Organization will set you free, Uncategorized
7 comments on “Scripts to update the Raspberry Pi and Debian-based Linux Distros
  1. […] Splunk on Ubuntu Linux:I spun up a new Ubuntu Server, patched it, and then went to download the .deb file. Since this is an Ubuntu Server, there is no GUI. So then, […]

    Like

  2. Binoj Daniel says:

    Excellent post, can we combine both scripts by detecting if we are running it on RPi and execute the RPi updates?

    Like

  3. Robert Seder says:

    Thanks – and interesting point! I initially looked at “uname -a” and “lsb_release -a” to go look for some markers, and there is nothing obvious or consistent. I mean, you could see if it’s ARM-based and Raspbian?

    Instead, I thought “the one thing that is consistent is that if it’s a Raspberry Pi, it has the raspi-config program”. So, I can use the “which” command to go find the path of that. If it’s not present, then “which” returns an empty line. So, I could pipe that to “wc” for word count (or line count). So, something like this:

    $ which raspi-config | wc -l

    and this returns 0 if raspi-config if not found, or 1 (or greater than 1) if one or more are found on disk. So to answer your question: YES! One could put an if statement and see if there is a “raspi-config” on the local system. If there is, then do the firmware upgrade.

    I’ll see if I can get that hammerred out this weekend and then I’ll update this post. Great idea – thanks!

    Like

    • Binoj Daniel says:

      Awesome! I was thinking along the same lines. I think the rasp-config grep should do. Let me know if you need any help from my side.

      Like

      • Robert Seder says:

        OK, here is the logic I added:

        if [ $(which raspi-config | wc -l) -gt 0 ]; then
        echo ‘[*] Raspberry Pi Detected.’
        echo ‘[*] Update the Raspberry Pi firmware to the latest (if available)…’
        sudo rpi-update
        echo ‘[*] Done updating firmware.’
        fi

        I renamed the old update.sh to update_old.sh, and now this integrated one is now called update.sh and you can get it from that same gist: https://gist.github.com/RobSeder/24af1cd7c195a54fe8b0

        Like

  4. […] Update your Raspberry Pi using the script by Rob Seder […]

    Like

  5. […] system for this – but I just made it one step easier. Whenever I install a Debian-based system, I follow my own directions from here and download my update.sh file. The trouble is, the URL is very long. So, I just created a […]

    Like

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

Archives
Categories

Enter your email address to follow this blog and receive notifications of new posts by email.

Join 2 other followers

%d bloggers like this: