Email Alerts

How to Set Up Email Alerts on CentOS VPS for Smart System Monitoring

Running a CentOS-based VPS means you’re in control — but with great power comes great responsibility. If something goes wrong — high CPU load, disk space running out, failed services — wouldn’t it be nice to get an email alert before disaster strikes?

In this guide, you’ll learn how to configure email notifications on CentOS so you can monitor your server’s health proactively.


🧰 What You’ll Need

  • A CentOS 7 or 8 VPS (or Stream)
  • Root or sudo access
  • A mail transfer agent (MTA) like mailx or Postfix
  • Basic knowledge of Linux shell

📧 Step 1: Install a Mail Utility

First, install mailx, a simple command-line email client:

bashCopyEditsudo yum install mailx -y

Then install Postfix as the MTA:

bashCopyEditsudo yum install postfix -y
sudo systemctl enable postfix
sudo systemctl start postfix

🛠️ Step 2: Configure Postfix for Outgoing Mail

Open the Postfix config:

bashCopyEditsudo nano /etc/postfix/main.cf

Add or modify these lines at the end:

iniCopyEditrelayhost = [smtp.yourmailserver.com]:587
smtp_use_tls = yes
smtp_sasl_auth_enable = yes
smtp_sasl_password_maps = hash:/etc/postfix/sasl_passwd
smtp_sasl_security_options = noanonymous

Then create the credentials file:

bashCopyEditsudo nano /etc/postfix/sasl_passwd

Add your SMTP credentials:

markdownCopyEdit[smtp.yourmailserver.com]:587 username:password

Secure and apply:

bashCopyEditsudo postmap /etc/postfix/sasl_passwd
sudo chmod 600 /etc/postfix/sasl_passwd /etc/postfix/sasl_passwd.db
sudo systemctl restart postfix

✅ Now your server can send emails through your external mail provider (Gmail, Mailgun, SendGrid, etc.).


🔔 Step 3: Test Email Delivery

Run:

bashCopyEditecho "Test email from CentOS server" | mail -s "Test Alert" [email protected]

Check your inbox. If nothing arrives, check:

bashCopyEditsudo tail -f /var/log/maillog

⚙️ Step 4: Set Up Automated System Alerts

📉 Example: Disk Usage Alert

Create a script:

bashCopyEditsudo nano /usr/local/bin/disk-alert.sh

Paste:

bashCopyEdit#!/bin/bash
THRESHOLD=90
EMAIL="[email protected]"

df -h | awk 'NR>1 {if (int($5) > '"$THRESHOLD"') system("echo Warning: "$1" is at "$5" | mail -s \"Disk Alert on $(hostname)\" '"$EMAIL"'")}'

Make it executable:

bashCopyEditchmod +x /usr/local/bin/disk-alert.sh

⏰ Add a Cron Job

Run crontab -e and add:

bashCopyEdit*/30 * * * * /usr/local/bin/disk-alert.sh

This checks disk usage every 30 minutes and sends an email if usage exceeds 90%.


🛡️ Step 5: Add More Alert Scripts

Some ideas:

  • CPU load alert
  • Memory usage alert
  • Service down alert (e.g., Apache/Nginx/MySQL)
  • Intrusion detection alert (fail2ban, SSH login)

You can even hook into tools like monit, logwatch, or Nagios for more robust monitoring.


🧠 Final Thoughts

Setting up email alerts on your CentOS VPS gives you an essential early warning system. Whether you’re running production apps or personal projects, being notified about issues in real time can save you from downtime, data loss, or security incidents.


🌿 Reliable VPS Monitoring with Green Host

At GreenHost, we help you stay ahead of issues with integrated system monitoring and custom alerting support for CentOS, Ubuntu, and more. Ask us about pre-configured alert templates, dashboards, and AI-based diagnostics.