Debian Linux – 使用 gmail 來寄送 Server 的 log 檔

縣網可能因為資安的因素,所以預設並不允許 Server 可以外送信件,如果要打開這一項功能,要與資網聯繫,有些麻煩,因為學校的這些 Server,並沒有擔任 Mail Server 的工作,只是純粹要把系統的 log 檔案彙整到外部的信箱而已,所以改用 Google Mail 來外送信件。
參考網頁:
透過ssmtp與Gmail帳戶來做Mail自動通知 on CentOS 5.x | IT水電工-哆啦胖虎
Linux 使用 SSMTP 與 GMail 以指令或程式自動寄信教學 – G. T. Wang

1. 安裝 ssmtp 及 logwatch
# apt-get install ssmtp logwatch

2. 備份原設定檔 /etc/ssmtp/ssmtp.conf
# cp /etc/ssmtp/ssmtp.conf /etc/ssmtp/ssmtp.conf.$(date +%F)
[@more@]
3. 修改設定檔 /etc/ssmtp/ssmtp.conf
# vim /etc/ssmtp/ssmtp.conf
#root=postmaster

# The place where the mail goes. The actual machine name is required
# no MX records are consulted. Commonly mailhosts are named mail.domain.com
# The example will fit if you are in domain.com and you mailhub is so named.
mailhub=smtp.gmail.com:587
# Where will the mail seem to come from?
#rewriteDomain=y
# The full hostname
#hostname=root-chroot-copy
# 寄件者帳號
AuthUser=xxxx@gmail.com
# 寄件者密碼
AuthPass=xxxx
UseSTARTTLS=YES
FromLineOverride=yes #enables to use mail -r option

4. 更改檔案權限
# chmod 0600 /etc/ssmtp/ssmtp.conf

5. 備份原設定檔
/usr/share/logwatch/default.conf/logwatch.conf
# cp /usr/share/logwatch/default.conf/logwatch.conf /usr/share/logwatch/default.conf/logwatch.conf.$(date +%F)

6. 進行修改
# sed -i ‘s/MailTo = root/MailTo = xxxx@gmail.com/’ /usr/share/logwatch/default.conf/logwatch.conf
# sed -i ‘s/MailFrom = Logwatch/MailFrom = Logwatch@nxi.test.ilc.edu.tw/’
/usr/share/logwatch/default.conf/logwatch.conf
# sed -i ‘s/Output = stdout/Output = mail/’
/usr/share/logwatch/default.conf/logwatch.conf
# sed -i ‘s/Detail = Low/Detail = Med/’
/usr/share/logwatch/default.conf/logwatch.conf

7. 執行 logwatch 測試是否能收到信件
如果有無法收到信件,可要啟用 安全性較低的應用程式存取權