How to Install and Configure msmtp on Kubuntu/Ubuntu (for Cron + Bash Alerts)
This guide shows how to install and configure msmtp on Kubuntu/Ubuntu so Bash scripts (including cron jobs) can send email alerts reliably.
It is written for a typical use case:
- You have a Bash monitoring script (e.g., uptime checks)
- You want to send email via Gmail SMTP using an App Password
- You want it to work both manually and from cron
1) Install msmtp and certificates
Update packages and install required components:
sudo apt update sudo apt install -y msmtp msmtp-mta ca-certificates Verify msmtp is installed:
msmtp --version 2) Create the msmtp configuration file
msmtp reads configuration from ~/.msmtprc (per-user). Create it:
nano ~/.msmtprc Paste this Gmail-based configuration and update your email and app password:
defaults auth on tls on tls_starttls on tls_trust_file /etc/ssl/certs/ca-certificates.crt logfile ~/.msmtp.log account gmail host smtp.gmail.com port 587 from your_email@gmail.com user your_email@gmail.com password YOUR_16_CHAR_APP_PASSWORD account default : gmail Notes:
- Use SMTP 587 + STARTTLS (recommended)
- Do not use your normal Gmail password; use an App Password
- The logfile helps with troubleshooting (
~/.msmtp.log)
3) Lock down file permissions
msmtp will refuse to use an insecure config file. Set permissions:
chmod 600 ~/.msmtprc Confirm:
ls -l ~/.msmtprc You should see something like:
-rw------- 1 youruser youruser ... .msmtprc 4) Create a Gmail App Password
Gmail SMTP typically requires:
- 2-Step Verification enabled
- An App Password (16 characters)
Steps (high level):
- Go to Google Account Security settings
- Enable 2-Step Verification
- Open App passwords
- Create an app password for Mail
- Copy the 16-character key and paste it into
~/.msmtprc
5) Test msmtp manually (must pass before cron)
Send a test email:
echo -e "To: your_email@gmail.com\nFrom: your_email@gmail.com\nSubject: msmtp test\n\nHello from msmtp" | msmtp your_email@gmail.com If you receive it, msmtp is working.
If it fails, check the log:
cat ~/.msmtp.log 6) Make sure cron can find msmtp (PATH + HOME)
Cron runs with a minimal environment. The two most common causes of “works manually but not via cron” are:
PATHmissing/usr/binHOMEnot set to your user home
Check msmtp location:
which msmtp Expected output:
/usr/bin/msmtp In your crontab (crontab -e), include:
PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin HOME=/home/youruser
Then call your script with an absolute path.
Example cron line:
* * * * * /home/youruser/scripts/uptimerbt/check_once.sh >> /home/youruser/scripts/uptimerbt/uptime_monitor.log 2>&1
7) Optional: Test sending from a cron-like environment
Run the command with a minimal env to simulate cron:
env -i HOME="$HOME" PATH="/usr/bin:/bin" bash -lc 'echo -e "To: your_email@gmail.com\nFrom: your_email@gmail.com\nSubject: cron-like test\n\nHello" | msmtp your_email@gmail.com' 8) Common errors and fixes
msmtp: cannot locate trust file
Install certificates:
sudo apt install -y ca-certificates msmtp: authentication failed
- Make sure you used an App Password
- Confirm 2-Step Verification is enabled
- Re-check
from,user, andpasswordentries
Works manually but not in cron
- Add
PATHandHOMElines in crontab (see above) - Use absolute paths in cron jobs
- Confirm the cron job runs as the same user who owns
~/.msmtprc
9) Recommended security practice
If you prefer not to store the password directly in ~/.msmtprc, you can use passwordeval with a secret manager (e.g., pass). That’s more secure, but slightly more complex. For a personal laptop, an app password with chmod 600 is typically acceptable.
Quick checklist
sudo apt install msmtp msmtp-mta ca-certificates~/.msmtprccreated and correctchmod 600 ~/.msmtprc- App Password used (not your Gmail password)
- Manual test email works
- Cron has
PATHandHOMEset (if needed)