Open Source Ubuntu Update Script Version 1.0 Released

ubuntu-update.sh

A bash script to update your Ubuntu system.

Set the file to executable then run it with sudo as outlined below to update your Ubuntu system.

This project is in GitHub and can be found here

Background

I maintain several individual Ubuntu 16.04 Long Term Support (LTS) Servers and found myself typing sudo apt update && sudo apt upgrade -y && sudo apt full-upgrade -y often.

I made a script that did this, but thought it would also be fun to make it a bit more functional and make it available to everyone.

Also, I noticed that when these commands are run together, the output can get quite lengthy, and there’s no clear separation between the parts running, so I’d have to scroll back up through the output to see whether there was any output requiring action.

This script puts clear banners at the beginning of each activity and parses output in an attempt to present items of interest like warnings or reboot messages at the end of the output.

Project Goals

The goal of this project is to provide an easy to use, single command to automate the tasks routinely undertaken to update an Ubuntu system, and to provide the messages that require further action or inquiry at the end of the run.

Who This Is For

If, like me, you maintain several Ubuntu Linux systems, and you find yourself typing sudo apt update && sudo apt upgrade -y && sudo apt full-upgrade -y, this script is for you!

If you maintain a large Ubuntu Server infrastructure, you likely have a patch management system, so this probably isn’t for you.

You could, however, scavenge some useful features like using getopts to accept command line arguments for your other scripts!

Prerequisites

You must have sudo permissions to run this script.

You must set the file to executable.

Set it so it is executable using the command:

sudo chmod +x ubuntu-update.sh

being in the directory where the file resides when you run the command.

You will not have to set the file executable if you create a local git repository and clone from Master.

Warning

Be sure you have read and understand what this file does before running it.

You can read the man page for each command and option to see what it does.

Any time the creator of a script says it has to be run with sudo permissions or as root, inderstand why and use caution.

This script has to be run with sudo because the apt-get commands it uses must be run as root.

To understand getopts, you can type help getopts or just search the web for more info.

The other commands used are fairly straight forward.

What It Does

This script runs the following commands:

apt-get update
apt-get upgrade -y
apt-get dist-upgrade -y
apt-get autoremove

It also writes the output to a file, /tmp/update-output.txt, and to the screen with the tee command, then uses grep to parse the update-output.txt file for information you may want to be aware of, displaying any found at the end of the run.

It then deletes the /tmp/update-output.txt file that was created.

I chose to use apt-get instead of apt commands because apt-get provides more stable and reliable output than apt which makes it more script friendly.

Installing

ubuntu-update.sh does not require any special installation.

Either copy the repository from GitHub or simply copy the file using the download option from the green “Clone or download” link in the upper right of the github page.

To make a local copy of the git repo, run:

cd <directory where you want to create your git clone>
git init
git clone https://github.com/TedLeRoy/ubuntu-update.sh.git

To copy the file directly, type:

wget https://github.com/TedLeRoy/ubuntu-update.sh/archive/master.zip

being in the directory where you want the file downloaded, then type unzip master.zip to extract the files.

Usage

Just run as shown:

sudo bash ubuntu-update.sh

or

sudo ./ubuntu-update.sh

being in the directory where ubuntu-update.sh resides.

You may choose to skip certain commands by specifying one or more options at the command line.

Available options are as follows:

Usage: sudo bash ubuntu-update.sh [-ugdrh]
       No option - Run all options (recommended)
       -u Don't run apt-get update
       -g Don't run apt-get upgrade -y
       -d Don't run apt-get dist-upgrade -y
       -r Don't run apt-get auto-remove
       -h Display Usage and exit

Output will be the normal output to std-out of running the apt-get update, upgrade, dist-upgrade, and autoremove commands.

A video showing a sample run can be found at https://youtu.be/HCWUGxYAqjY

It is recommended that you run this script interactively instead of calling it with another script or process. You may be prompted for input for things like GRUB updates, or which version of a file to keep. This will hang the script unless you provide input.

Although these prompts could be overridden with export DEBIAN_FRONTEND=noninteractive, responding to the prompts provides greater control.

The script will also parse the output attempting to display lines that may require your attention at the end of the run, looking for words like warning and reboot.

Messages

Look for messages recommending a reboot or providing other warnings you may want to check out. These should be displayed at the end of the run but it may not catch all items of interest.

Troubleshooting

Here are a few common issues you may encounter and how to resolve them.

1. Forgetting to type sudo before the script name.

If you forget to type sudo before the script name, you’ll get output like the following:

Reading package lists...
W: chmod 0700 of directory /var/lib/apt/lists/partial failed - SetupAPTPartialDirectory (1: Operation not permitted)
E: Could not open lock file /var/lib/apt/lists/lock - open (13: Permission denied)
E: Unable to lock directory /var/lib/apt/lists/
W: Problem unlinking the file /var/cache/apt/pkgcache.bin - RemoveCaches (13: Permission denied)
W: Problem unlinking the file /var/cache/apt/srcpkgcache.bin - RemoveCaches (13: Permission denied)

Notice the statements like Operation not permitted and Permission denied. These indicate an issue with the permissions used while running. It must be run with sudo.

2. A collision of updates.

By default, Ubuntu 16.04 Server will install security updates automatically.

If another update is already running, you’ll see output like the following:

E: Could not get lock /var/lib/dpkg/lock - open (11: Resource temporarily unavailable)
E: Unable to lock the administration directory (/var/lib/dpkg/), is another process using it?

If this happens, just wait and try again later.

Updates

This project will go through some iterations as I think of things I’d like to add, or add features based on feature requests. You’ll want to keep your script up to date.

If you chose to create a local git repository, you can run one command to update to the latest version available.

git pull

from the directory where you created your git clone.

If you downloaded the file, you’ll have to download again after each update.

Author

Built With

bash script using apt on Ubuntu 16.04 LTS.

Future Improvements

The following additions or features are planned:

  • ~~Accept command line options to override default behavior. If the user wants to skip dist-upgrade, provide a command line option for doing so, for example.~~ [Done] – See Issue-1
  • ~~Implement versioning so people will know when there has been an update.~~ [Done] – Version 1.0 released on 16 May 2018.

Suggestions for features are welcome, provided they are in alignment with the Project Goals above.

License

This project is licensed under the GNU General Public License – see the LICENSE.md file for details.

Acknowledgements

  • Creators and maintainers of apt.
  • The Free and Open Source Software (FOSS) community and people on the Internet at large who provide their solutions to problems like this. This is really a Frankenscript of the work of many who created scripts or tutorials with features I was hunting for.
  • Shouts out to LinuxConfig.org and Kevin Sookocheff for their tutorials that helped me learn and integrate getopts into the script.
  • Shout out to kph for suggesting getopts.

Sample run

Here is what a sample run of the script looks like:

sudo ./ubuntu-update.sh
[sudo] password for user:

ubuntu-update.sh Copyright (C) 2018 Ted LeRoy
This program comes with ABSOLUTELY NO WARRANTY see
https://github.com/TedLeRoy/ubuntu-update.sh/blob/master/LICENSE.md
This is free software, and you are welcome to redistribute it
under certain conditions.
Again, see  https://github.com/TedLeRoy/ubuntu-update.sh/blob/master/LICENSE.md
for details.

#############################
#     Updating Data Base    #
#############################

Hit:1 http://us.archive.ubuntu.com/ubuntu xenial InRelease
Hit:2 http://us.archive.ubuntu.com/ubuntu xenial-updates InRelease
Hit:3 http://us.archive.ubuntu.com/ubuntu xenial-backports InRelease
Hit:4 http://security.ubuntu.com/ubuntu xenial-security InRelease
Reading package lists...

##############################
# Upgrading Operating System #
##############################

Reading package lists...
Building dependency tree...
Reading state information...
Calculating upgrade...
The following packages have been kept back:
  libdrm2
The following packages will be upgraded:
  apparmor apt apt-transport-https apt-utils base-files bsdutils
  cloud-guest-utils cloud-initramfs-copymods cloud-initramfs-dyn-netconf
  cryptsetup cryptsetup-bin distro-info-data dnsmasq-base dpkg
  friendly-recovery grub-common grub-legacy-ec2 grub-pc grub-pc-bin
  grub2-common initramfs-tools initramfs-tools-bin initramfs-tools-core
  iproute2 isc-dhcp-client isc-dhcp-common klibc-utils libapparmor-perl
  libapparmor1 libapt-inst2.0 libapt-pkg5.0 libaudit-common libaudit1
  libblkid1 libcryptsetup4 libfdisk1 libgcrypt20 libgnutls-openssl27
  libgnutls30 libklibc libmount1 libnuma1 libparted2 libplymouth4 libseccomp2
  libsmartcols1 libuuid1 linux-firmware logrotate lshw lxcfs lxd lxd-client
  mdadm mount open-vm-tools overlayroot parted plymouth
  plymouth-theme-ubuntu-text python-apt-common python3-apt python3-distupgrade
  resolvconf snapd sosreport squashfs-tools ubuntu-minimal
  ubuntu-release-upgrader-core ubuntu-server ubuntu-standard
  unattended-upgrades update-notifier-common util-linux uuid-runtime vlan
  xfsprogs
77 upgraded, 0 newly installed, 0 to remove and 1 not upgraded.
Need to get 79.0 MB of archives.
After this operation, 19.8 MB of additional disk space will be used.
Get:1 http://us.archive.ubuntu.com/ubuntu xenial-updates/main amd64 base-files amd64 9.4ubuntu4.6 [55.0 kB]
Get:2 http://us.archive.ubuntu.com/ubuntu xenial-updates/main amd64 bsdutils amd64 1:2.27.1-6ubuntu3.4 [51.5 kB]
Get:3 http://us.archive.ubuntu.com/ubuntu xenial-updates/main amd64 dpkg amd64 1.18.4ubuntu1.4 [2,088 kB]
Get:4 http://us.archive.ubuntu.com/ubuntu xenial-updates/main amd64 util-linux amd64 2.27.1-6ubuntu3.4 [849 kB]
Get:5 http://us.archive.ubuntu.com/ubuntu xenial-updates/main amd64 mount amd64 2.27.1-6ubuntu3.4 [121 kB]
Get:6 http://us.archive.ubuntu.com/ubuntu xenial-updates/main amd64 libapt-pkg5.0 amd64 1.2.26 [706 kB]
[Truncated for brevity...]
Preparing to unpack .../snapd_2.29.4.2_amd64.deb ...
Warning: Stopping snapd.service, but it can still be activated by:
  snapd.socket
[Truncated for brevity...]
Unpacking grub-legacy-ec2 (17.2-35-gf576b2a2-0ubuntu1~16.04.2) over (0.7.9-153-g16a7302f-0ubuntu1~16.04.2) ...
dpkg: warning: unable to delete old directory '/etc/kernel/kernel/postinst.d': Directory not empty
dpkg: warning: unable to delete old directory '/etc/kernel/kernel/postrm.d': Directory not empty
dpkg: warning: unable to delete old directory '/etc/kernel/kernel': Directory not empty
[Truncated for brevity...]
Processin triggers for ureadahead (0.100.0-19) ...
Processing triggers for resolvconf (1.78ubuntu6) ...

#############################
#  Starting Full Upgrade    #
#############################

Reading package lists...
Building dependency tree...
Reading state information...
Calculating upgrade...
The following NEW packages will be installed:
  libdrm-common
The following packages will be upgraded:
  libdrm2
1 upgraded, 1 newly installed, 0 to remove and 0 not upgraded.
Need to get 35.2 kB of archives.
After this operation, 39.9 kB of additional disk space will be used.
Get:1 http://us.archive.ubuntu.com/ubuntu xenial-updates/main amd64 libdrm-common all 2.4.83-1~16.04.1 [4,870 B]
Get:2 http://us.archive.ubuntu.com/ubuntu xenial-updates/main amd64 libdrm2 amd64 2.4.83-1~16.04.1 [30.4 kB]
Fetched 35.2 kB in 5s (6,572 B/s)
                                 Selecting previously unselected package libdrm-common.
(Reading database ... 91695 files and directories currently installed.)
Preparing to unpack .../libdrm-common_2.4.83-1~16.04.1_all.deb ...
Unpacking libdrm-common (2.4.83-1~16.04.1) ...
Preparing to unpack .../libdrm2_2.4.83-1~16.04.1_amd64.deb ...
Unpacking libdrm2:amd64 (2.4.83-1~16.04.1) over (2.4.76-1~ubuntu16.04.1) ...
Processing triggers for libc-bin (2.23-0ubuntu10) ...
Setting up libdrm-common (2.4.83-1~16.04.1) ...
Setting up libdrm2:amd64 (2.4.83-1~16.04.1) ...
Processing triggers for libc-bin (2.23-0ubuntu10) ...

#############################
#   Full Upgrade Complete   #
#############################


#############################
#    Starting Autoremove    #
#############################

Reading package lists...
Building dependency tree...
Reading state information...
0 upgraded, 0 newly installed, 0 to remove and 0 not upgraded.

#############################
#    Autoremove Complete    #
#############################


#####################################################
#   Checking for actionable messages from install   #
#####################################################

Warning: Stopping lxd.service, but it can still be activated by:
Warning: Stopping snapd.service, but it can still be activated by:
dpkg: warning: unable to delete old directory '/etc/kernel/kernel/postinst.d': Directory not empty
dpkg: warning: unable to delete old directory '/etc/kernel/kernel/postrm.d': Directory not empty
dpkg: warning: unable to delete old directory '/etc/kernel/kernel': Directory not empty
update-rc.d: warning: start and stop actions are no longer supported; falling back to defaults
Installation finished. No error reported.
update-rc.d: warning: start and stop actions are no longer supported; falling back to defaults

#############################
#    Cleaning temp files    #
#############################


#############################
#     Done with upgrade     #
#############################

This was run on an Ubuntu 16.04 LTS Server installation of Ubuntu.

Here are screenshots of a run:

Screenshot 1 of ubuntu-update.sh run
Screenshot 2 of ubuntu-update.sh run

Top 5 Free Tutorials for Linux Beginners

5 Best Free Linux Tutorials for Beginners

Linux tutorials are all over the Internet. Here are a five of my favorites for beginners.

I present different formats, because we all learn differently. There are books, courses, web sites, and combined media.

#1 Introduction to Linux, A Beginner’s Guide, by Machtelt Garrels (Tille), The Linux Documentation Project (tdlp).

Introduction-to-Linux-A-Beginners-Guide
Introduction to Linux

Clicking on the image takes you to a .pdf version of the Guide.

The web version starts here.

The .pdf version is entitled “Introduction to Linux, A Hands on Guide“, and that name is appropriate. Tille labored hard to provide an excellent, hands on “course in a book” that will get you understanding Linux quickly, even if you’ve never used it before.

Pro’s

  • Thorough
  • Well written
  • A lot of exercises
  • Generic (not centered on one or a few distro’s)

Con’s

  • Dated
  • Not a step-by-step hand holding guide
  • Generic (not centered on one or a few distro’s)

I list the fact that it’s generic as a pro and a con. It’s a plus, in that the commands she gives should work with any distribution. It’s a negative because you don’t get any direction about which distro to use, or specific instructions regarding that distro.

Overall, a great place to start!

#2 Intro to Linux, The Linux Foundation, LFS101x

The Linux Foundation’s stated goal is to;

build sustainable ecosystems around open source projects to accelare technology development and comercial adoption.

In fulfilling that goal, they want people to understand Linux, so they provide free and paid training.

One free course, Introduction to Linux,  is made avaiable through the online course retailer, edX. You’ll have to sign up for a free account to access the course.

The Linux Foundation Image
The Linux Foundation – Introduction to Linux Course

The course gives thorough coverage of three operating systems, including lessons covering the Graphical User Interfaces (GUI’s) for CentOS, OpenSUSE, and Ubuntu Linux in the form of text based and video lessons.

Pro’s

  • Thorough coverage
  • Starts with the basics and builds on them
  • Covers three top Linux distributions

Con’s

  • Many text based lessons (could be wordy if you don’t like that format)

#3 Linux Journey

The Linux Journey presents information in the form of a Learning Management System (LMS). LMS’s are systems designed to make learning modular and permit you to teach any topic with relative ease.

The Linux Journey screen capture
The Linux Journey LMS

It has a modern, clean design, and presents the information in an intuitive way for people who like a self paced reading focused learning environment.

It is divided into some high level sections based on the skill level of the student.

The levels are:

  • Grasshopper
  • Journeyman
  • Networking Nomad

You’ll often see references to Linux Fu, or Command Line Fu. It’s supposed to relate to the martial art, Kung Fu.

Grasshopper for the most basic level, was the nick name for Kwai Chang Kane when he was a student at the Shao Lin Temple in the 70’s televesion series, Kung Fu.

Here are the Pro’s and Con’s of this site.

Pro’s

  • Thorough coverage
  • Starts with basics and builds on them

Con’s

  • Completely text based

#4 Eli the Computer Guy’s YouTube Play List, Introduction to Linux

If you’re a more online video oriented person, as I am, you may like Eli the Computer Guy’s Introduction to Linux series.

Eli the Computer Guy YouTube Play List Intro to Linux
Eli The Comuter Guy – Introduction to Linux

Eli talks on the videos, does white board explanations, and shows things in action on the computer.

If you like the video format, this course may be for you.

Pro’s

  • Decent coverage
  • Logical flow

Con’s

  • Some video’s are quite lengthy (longest is 53 minutes!)
  • No other inteeraction like quizzes or activities (limitaiton of YouTube)

#5 The Linux Command Line, A Complete Introduction, by William E. Shotts Jr., No Starch Press

Once you have the basics down from one or several of the courses, sites, or books above, it’s good to get nice and cozy with the Linux command line.

If you manage Linux servers, you’ll rarely be managing the actual server via a Graphical User Interface or GUI.

Almost all of your work will be done at the command line.

This book provides an excellent command line foundation.

The Linux Command Line Book Cover
The Linux Command Line

Pro’s

  • Excellent coverage of the Linux command line
  • Format encourages you to follow along and try things while reading

Con’s

  • Focused solely on the command line and interacting with the shell (you may want to start with one of the other resources first)

You’ll eventually find much of the information in many of the formats mentioned on this site as it’s built.

Please provide input if there are topics you’d like to covered sooner.

I am including a plug for my paid Udemy course, Ubuntu Linux Fundamentals – Learn Linux Server with Ubuntu, as well as a coupon so you can take it for $24.99 here. List price at the time I’m posting this is $100.

Happy learning, and please leave comments on what resources you like!

Welcome!

Welcome to Linux Literacy. The place to learn Linux!

You’ll find blog posts with places to learn Linux, tips and tricks, and pages with lessons that will set you on the path to Linux enlightenment.

Linux is a flexible, stable operating system you can customize to meet almost any conceivable need, from running the computers in your car, to your smart watch or smart phone, to your smart television, to your web server, or your laptop!

There are distributions or distro’s for almost any need, and if you have a need that isn’t filled, you can make your own!

Linux is open source, meaning you can always view the code behind the scenes that makes your operating system run.

If you want to, and have the skills to or are willing to learn, you can tweak the source as you see fit to benefit you, provided what you create is also made available to everyone.

Linux is a powerful force in the world and this is your chance to learn to use it!

Have fun!