How to set up automatic updates on Ubuntu server

Search for a command to run...

No comments yet. Be the first to comment.
Introduction I run a large WhatsApp group and I often need to mention everyone in the group. But there are no real solutions for mentioning all members. WhatsApp does not natively do it, and there was a Firefox browser extension, but it was abandoned...

Introduction This past week I was toying around with the HTML5 canvas element, trying to join two images together. At first, it seemed fine, but when I tried to reload the website, it was a mess. One image would load, but the other wouldn't. Investig...

Introduction I have worked on projects that required the production and testing database to be the same. This could be because some features work in MariaDB, but not in SQLite. Or some bugs appear in MySQL, but not in PostgreSQL. When you're working ...

Introduction I'm going to explain how to build a free plan for your Laravel Spark Next application. I will be using Paddle as the payment gateway, but the steps are almost identical with Stripe. Best of all? No credit card required for the free plan....

Introduction Sometimes you end up deploying an application to Dokku and then realize that you want to revert the changes you made. In this tutorial we'll go over how to roll back a Dokku deployment. Before we start Preface Keep in mind that rolling ...

Setting up automatic updates can be a daunting task. But fear not, this tutorial will help you set up automatic updates correctly in less than 10 minutes.
While this tutorial is focused on Ubuntu Server, it can be used for many other distributions that use the same package manager, like Ubuntu Desktop, Debian, Linux Mint, etc.
First you'll have to update to the latest package repository definition.
sudo apt update
Then, we will need to install the package that does all the magic for us, unattended-upgrades.
sudo apt install -y unattended-upgrades
Chances are that you already have this package installed.
Next step is to configure the package, lets start by opening the configuration file in the nano text editor.
sudo nano /etc/apt/apt.conf.d/50unattended-upgrades
// ...
Unattended-Upgrade::Allowed-Origins {
"${distro_id}:${distro_codename}";
"${distro_id}:${distro_codename}-security";
// Extended Security Maintenance; doesn't necessarily exist for
// every release and this system may not have it installed, but if
// available, the policy for updates is such that unattended-upgrades
// should also install from here by default.
"${distro_id}ESMApps:${distro_codename}-apps-security";
"${distro_id}ESM:${distro_codename}-infra-security";
// "${distro_id}:${distro_codename}-updates";
// "${distro_id}:${distro_codename}-proposed";
// "${distro_id}:${distro_codename}-backports";
};
// ...
You should read the configuration file to understand what it is doing, don't worry if you don't understand most things.
The important step is to uncomment the following lines.
// Required, updates common software
"${distro_id}:${distro_codename}-updates";
// Optional, removes unused packages when updating
Unattended-Upgrade::Remove-Unused-Kernel-Packages "true";
Unattended-Upgrade::Remove-Unused-Dependencies "true";
// Optional, reboot automatically the system if needed at certain time
Unattended-Upgrade::Automatic-Reboot "true";
Unattended-Upgrade::Automatic-Reboot-Time "02:00";
You can search the file with
ctrl+w
Then exit nano with ctrl + x and press Y to save modifications.
Now that everything is set up, lets enable the automatic updates. For this, you will need to configure one last file.
sudo nano /etc/apt/apt.conf.d/20auto-upgrades
The file might be empty, in that case, just paste the following.
APT::Periodic::Update-Package-Lists "1";
APT::Periodic::Download-Upgradeable-Packages "1";
APT::Periodic::Unattended-Upgrade "1";
APT::Periodic::AutocleanInterval "7";
The values are specified in days, so auto-clean will happen every week and auto-updates every day.
Let's test if everything is set up correctly.
sudo unattended-upgrades --dry-run
This will run unattended-upgrades without making any real change, making sure everything is correctly set up.
That was it, easy right?
You might want to free up space on your server after all the updates are done!
If you have found this useful then you should follow me, I will be posting more interesting content! 🥰
Or support me financially. 💸
Congratulations, today you have learned how to set up and configure automatic updates on your Ubuntu server thanks to the unattended-upgrades package.
Let me know if the tutorial was useful to you in the comments!