시스템을 다시 시작해야 할 때 Ubuntu 서버가 메일을 보내도록 허용

시스템을 다시 시작해야 할 때 Ubuntu 서버가 메일을 보내도록 허용

서버 재부팅이 필요한 업데이트가 설치된 경우 Ubuntu 서버 20.04 LTS가 특정 이메일 주소로 메일을 보낼 가능성이 있습니까?

예를 들어 /var/run/reboot-required.pkgs 파일이 존재하면 관리자가 재부팅할 수 있도록 메일을 관리자에게 보냅니다.

Google을 모두 살펴봤지만 이에 대한 쉬운 튜토리얼을 찾을 수 없습니다.

답변1

/etc/apt/apt.conf.d/50unattended-upgrades를 사용해보십시오.

// Send email to this address for problems or packages upgrades
// If empty or unset then no email is sent, make sure that you
// have a working mail setup on your system. A package that provides
// 'mailx' must be installed. E.g. "[email protected]"
Unattended-Upgrade::Mail "[email protected]";

// Set this value to "true" to get emails only on errors. Default
// is to always send a mail if Unattended-Upgrade::Mail is set
Unattended-Upgrade::MailOnlyOnError "false";

실제로는 쉬운 부분입니다. 다음으로, 전자 메일을 서버에서 사용자에게 전달하기 위해 경량 우편 시스템을 설정해야 합니다.

이를 수행하는 한 가지 방법은 다음과 같습니다.

sudo apt install msmtp msmtp-mta bsd-mailx

구성 파일:/root/.msmtprc

account        your_label_here
host           smtp.example.com
port           465
from           root@your_machine.your_domain
user           [email protected] (your email)
password       your_smtp_password
auth           on
tls            on
tls_starttls   off
tls_certcheck  off
logfile        /root/.msmtp.log
account default : your_label_here

테스트 명령:

echo "This is the email body" > /tmp/body.txt && sudo mailx -s "This is the subject" [email protected] < /tmp/body.txt; rm /tmp/body.txt

답변2

감사합니다. 튜토리얼을 따르고 이에 대한 cronjob을 편집했으므로 실제로 사용자 루트로서 구성 파일 /root/.msmtprc를 만들었습니다.

account        your_label_here
host           smtp.example.com
port           465
from           root@your_machine.your_domain
user           [email protected] (your email)
password       your_smtp_password
auth           on
tls            on
tls_starttls   off
tls_certcheck  off
logfile        /root/.msmtp.log
account default : your_label_here

그 후 /etc/cron.hourly/reboot-check에 파일을 만들었습니다.

#!/usr/bin/env bash

if [ -f /var/run/reboot-required ]; then
        echo "A reboot is required following updates to server <TestServer> the machine will auto reboot in 1h" > /tmp/body.txt && sudo mailx -s "Reboot Required" [email protected] < /tmp/body.txt; rm /tmp/body.txt
fi

그리고 머신에 재부팅이 필요한 업데이트가 있고 /var/run/reboot-required 파일을 생성하면 재부팅을 알리는 이메일이 나에게 전송됩니다.

관련 정보