Skip to content

Instantly share code, notes, and snippets.

@TechPreacher
Last active August 16, 2024 08:30
Show Gist options
  • Select an option

  • Save TechPreacher/c723d3f231d1e0e15002e9f8b27cc715 to your computer and use it in GitHub Desktop.

Select an option

Save TechPreacher/c723d3f231d1e0e15002e9f8b27cc715 to your computer and use it in GitHub Desktop.

Enable automatic updates on Raspberry Pi

Install and conigure unattended-upgrades

Sources:

To install the software, use the command below:

sudo apt-get install unattended-upgrades
# Following are additional software required
# we only need mailutils or bsd-mailx, choose 1
sudo apt-get install mailutils
sudo apt-get install bsd-mailx
sudo apt-get install update-notifier-common 

Next, we need to edit the configuration files:

sudo nano /etc/apt/apt.conf.d/50unattended-upgrades

The configuration should be similar as below:

Unattended-Upgrade::Origins-Pattern {
        // Codename based matching:
        // This will follow the migration of a release through different
        // archives (e.g. from testing to stable and later oldstable).
//      "o=Debian,n=jessie";
//      "o=Debian,n=jessie-updates";
//      "o=Debian,n=jessie-proposed-updates";
//      "o=Debian,n=jessie,l=Debian-Security";
        "o=${distro_id},n=${distro_codename}";
        "o=${distro_id},n=${distro_codename}-updates";
        "o=${distro_id},n=${distro_codename}-proposed-updates";
        "o=${distro_id},n=${distro_codename},l=Debian-Security";
// Archive or Suite based matching:
        // Note that this will silently match a different release after
        // migration to the specified archive (e.g. testing becomes the
        // new stable).
//      "o=Debian,a=stable";
//      "o=Debian,a=stable-updates";
//      "o=Debian,a=proposed-updates";
        "origin=Debian,codename=${distro_codename},label=Debian-Security";
};

Unattended-Upgrade::Mail "username@example.com";
Unattended-Upgrade::Automatic-Reboot "true";

Basically, we commented out those type of upgrade we want to apply. The second last line allows the system to email us the status. We must install mailutils or mailx first in Raspbian for the email notification to be effective. The last line allow the system to reboot automatically. Please also make sure that update-notifier-common has been installed.

There are more option that we can set such as reboot time and log file in the configuration file. Uncomment any option when necessary.

Next, we need to amend another auto upgrade configuration file.

sudo nano /etc/apt/apt.conf.d/20auto-upgrades

The configuration files should have the following:

APT::Periodic::Update-Package-Lists "1";
APT::Periodic::Download-Upgradeable-Packages "1";
APT::Periodic::Unattended-Upgrade "1";
APT::Periodic::Verbose "1";
APT::Periodic::AutocleanInterval "7";

Finally, we perform a test to see if the upgrade process works:

sudo unattended-upgrade -d -v --dry-run

To enable unattended-upgrade use the following command:

sudo dpkg-reconfigure --priority=low unattended-upgrades

Configure mailutils

Sources:

Install postfix and mailutil

sudo apt-get update sudo apt-get install postfix mailutils

Configure postfix to use Office365 in /etc/postfix/main.cf

mynetworks = 127.0.0.0/8
inet_interfaces = loopback-only

smtp_use_tls = yes
smtp_always_send_ehlo = yes
smtp_sasl_auth_enable = yes
smtp_sasl_password_maps = hash:/etc/postfix/sasl_passwd
smtp_sasl_security_options = noanonymous
smtp_sasl_tls_security_options = noanonymous
smtp_tls_security_level = encrypt
smtp_generic_maps = hash:/etc/postfix/generic
smtp_tls_CAfile = /etc/ssl/certs/ca-certificates.crt

Remove any domain you want to be able to send mail to from mydestination.

Configure mail credentials in /etc/postfix/sasl_passwd

[smtp.office365.com]:587 username@yourdomain.com:password

Update postfix maps

sudo chown root:root /etc/postfix/sasl_passwd
sudo chmod 0600 /etc/postfix/sasl_passwd
sudo postmap /etc/postfix/sasl_passwd

Configure mail address in /etc/postfix/generic

sudo nano /etc/postfix/generic
root@localdomain user@domain.com
@localdomain user@domain.com

Update postfix maps:

sudo chown root:root /etc/postfix/generic
sudo chmod 0600 /etc/postfix/generic
sudo postmap /etc/postfix/generic

Restart postfix

 sudo /etc/init.d/postfix restart

Send test mail using

echo "This is a test body" | mail -s "Relay Test Email" user@publicdomain -a "FROM:user@domain.com"

Send test mail using

--ssl-reqd \
--url 'smtp://smtp.office365.com:587' \
--user 'user@domain.com:password' \
--mail-from sender@domain.com \
--mail-rcpt recipient@domain.com \
--upload-file - <<EOF
From: Sender <sender@domain.com>
To: Recipient <recipient@domain.com>
Subject: Test mail
Content-Type: text/plain; charset="utf-8"
Content-Transfer-Encoding: quoted-printable
Content-Disposition: inline

This is a test mail sent from Linux.

EOF

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment