In a home lab environment, one key feature of is programming the server to be autonomous. As in, menial tasks such as updating the system with the latest and greatest security patches and application versions is imperative. One of the best ways to do this is to write a program that will aid in applying the updates automatically. It also paramount to consider cleaning the system of older versions of depreciated software or packages to have an optimized system performance. There is no utility in having older versions of the kernel or program versions lurking around and taking up space.
It is to provide a code and commands in order to augment the process of the server being fully automatic.
For simplicity the commands and code should be copy and paste. Screenshots be will provided in order to aid in walking you through the process. This tutorial is based upon Fedora Linux install. Commands provided in this tutorial will be nuanced in comparison to other Operating Systems.
mkdir Programs && cd Programs
This command will create the folder and then “cd” means to "change directory." You will now in the folder.
sudo nano automated_upgrades.sh
You will be prompted to type in your password and press enter then terminal show a blank file. It’s name is "automated_upgrades.sh" which is at the top center of terminal window.
Copy and paste the code below to the file.
#!/bin/bash
# Update package list
dnf check-update
# List outdated packages (optionally)
outdated_packages=$(dnf repoquery --installed --latest-limit=1)
if [ -n "$outdated_packages" ]; then
# Remove outdated packages
dnf remove $(dnf repoquery --installed --latest-limit=1)
else
echo "No outdated packages found."
fi
Press "ctrl + X" to exit out of the file on the keyboard. You will be prompted to save the file by press "y" for yes. Then press "enter" key on the keyboard to confirm the name of the file.
ls -l
Type in the command below to give the file permission to be executable.
sudo chmod +x ~/Programs/automated_upgrades.sh && ls -l
You may be prompted to type in your password then press “Enter”
cd /etc/systemd/system && sudo nano automated_updates.service
Password may be required and press "Enter," then nano editor will show a blank file. The name of the file is "automated_updates.service"
Now, copy and paste the setting conditions below:
[Unit]
Description=Cleanup outdated packages
[Service]
Type=simple
User=root
Group=root
ExecStart= /home/**user/Programs/automated_upgrades.sh
The double asterisks (**) means to change this to your username.
Press "ctrl + X" to exit, then press "y" for yes. Confirm the name of the file by pressing "enter."
sudo nano automated_updates.timer
You will be prompted for you password and press "Enter." Copy and paste the contents below into the timer file.
[Unit]
Description=Run automatic_updates.sh every day
[Timer]
OnBootSec=10min
OnUnitActiveSec=30d
Unit=automated_updates.service
[Install]
WantedBy=timers.target
Press "ctrl + X" to exit, then press "y" for yes. Confirm the name of the file by pressing "enter."
sudo systemctl start automated_updates.timer && sudo systemctl enable automated_updates.timer
Type in your password if prompted and press "enter."
sudo systemctl daemon-reload
Type in your password if prompted and press "enter."
If there is not any feedback, then the command was successfully executed.
sudo systemctl status automated_updates.timer
The command should pull something fairly equivalent in the photo above. The key indicators is seeing the word active in a green font color in conjunction with the word “enabled” being in a green font color. This confirms the system is active and will run when it is time appropriate. The system updates will be applied upon next initialization.