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
- 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:
- Updates the cache
- Upgrades all existing packages
- Upgrades the Linux distribution
- If it’s a Raspberry Pi device, go update the firmware
- Cleans up unnecessary and cached packages
- 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:
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:
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
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!