Introduction

Keeping your system up to date is crucial for security and performance. For small business owners, web developers, and freelancers using Ubuntu or Debian, automating updates via cron jobs can save time and ensure your systems are always running smoothly. This guide will walk you through setting up cron updates on Ubuntu/Debian in just 4 easy steps. By the end of this tutorial, you’ll have a system that updates itself on a schedule, giving you more time to focus on what you do best.

Prerequisites

Before starting, ensure you have:

  • A system running Ubuntu or Debian
  • Root or sudo access to the system
  • Basic knowledge of the terminal and editing text files

Total time: Approximately 10 minutes


Lets Go!


Step 1: Update Your System

Before automating updates, make sure your system is up to date.

  • Open your terminal
  • sudo not needed if you're logged in as root
  • Update the package list:
sudo apt-get update

Bash

  • Upgrade installed packages:
sudo apt-get upgrade

Bash

Step 2: Install Unattended Upgrades

unattended-upgrades is a package that automatically installs security updates.

  • Install the package:
sudo apt-get install unattended-upgrades

Bash

  • Enable automatic updates:
sudo dpkg-reconfigure --priority=low unattended-upgrades

Bash

Step 3: Configure Cron Job for Regular Updates

Cron jobs are used to schedule tasks at regular intervals.

  • Open the cron jobs file for editing:
sudo crontab -e

Bash

  • Add the following line to schedule updates every day at 2 AM:
    | minute | hour | day of month | month |day of week|
    | 0 | 2 | * | * | * | - This is every day at 2AM
    | 0 | 2 | 1 | * | * | - This is on the 1st day of every month at 2AM
0 2 * * * /usr/bin/apt-get update && /usr/bin/apt-get -y upgrade

Bash

 This line tells cron to run apt-get update and apt-get upgrade -y every day at 2 AM.

  • Save and close the file.
Step 4: Verify Your Configuration

Ensure everything is set up correctly.

  • Check your cron jobs:
sudo crontab -l

Bash

You should see the line you added in Step 3.

  • Test the configuration:
sudo unattended-upgrade --dry-run

Bash

This command simulates the upgrade process without making any changes.


Conclusion

Automating your updates on Ubuntu/Debian based  ensures your system stays secure and up-to-date without manual intervention. By following these 4 easy steps, you’ve set up a cron job to handle updates, freeing you to focus on more important tasks. Happy automating!

FAQ

Q1: Can I set the cron job to run at a different time?

  • Yes, you can modify the time in the cron job line. For example, to run at 3 AM, change 0 2 * * * to 0 3 * * *.

Q2: What if I want to update only specific packages?

  • You can specify packages by adding them to the apt-get upgrade command. For example: /usr/bin/apt-get upgrade -y package_name.

Q3: How can I disable automatic updates if needed?

  • You can comment out or delete the cron job line in the crontab file using sudo crontab -e.

Q4: Are there any risks to automating updates?

  • While automating updates is generally safe, there’s always a small risk that an update could cause compatibility issues. Regularly backup your system to mitigate this risk.

Q5: Can I receive notifications of updates?

  • Yes, you can configure unattended-upgrades to send email notifications by editing the configuration file at /etc/apt/apt.conf.d/50unattended-upgrades.


CREDIT: https://kyraweb.ca/automate-your-updates-how-to-setup-cron-update-on-ubuntu-debian-in-4-easy-steps/