Λειτουργικά συστήματα

* Γνώση Υπολογιστών >> Λειτουργικά συστήματα >  >> CentOS

Εγκατάσταση mailx στο RHEL/CentOS 7/8 για αποστολή email από τερματικό

Mailx στο RHEL/CentOS: Εγκατάσταση και ρύθμιση για αποστολή email από CLI

Το mailx (ή mail) είναι το κλασικό command-line email client για Unix/Linux. Χρησιμοποιείται σε scripts, cron jobs, monitoring alerts και automation για αποστολή emails χωρίς GUI.

Εγκατάσταση

# RHEL/CentOS 7
sudo yum install -y epel-release
sudo yum install -y mailx

# RHEL/CentOS 8 / Rocky / AlmaLinux
sudo dnf install -y epel-release
sudo dnf install -y mailx

# Verify
which mail
# /usr/bin/mail
mail --version
# mailx 12.5 7/5/10

Βασική χρήση

# Απλό email
echo "Body του email" | mail -s "Subject" user@example.com

# Από αρχείο
mail -s "Log report" admin@example.com < /var/log/syslog

# Με συνημμένο (χρειάζεται uuencode ή mutt)
# mailx δεν υποστηρίζει attachments native - χρησιμοποιήστε mutt ή swaks

Ρύθμιση SMTP (Απαραίτητη για εξερχόμενα emails)

Το mailx χρειάζεται MTA (Mail Transfer Agent) για να στέλνει emails. Επιλογές:

Επιλογή 1: Postfix (Συνιστάται - Local MTA)

sudo yum install -y postfix
sudo systemctl enable --now postfix

# Βασική ρύθμιση /etc/postfix/main.cf:
# myhostname = server.yourdomain.com
# myorigin = $myhostname
# inet_interfaces = loopback-only
# relayhost = [smtp.yourprovider.com]:587  # Αν χρησιμοποιείτε smart host
# smtp_sasl_auth_enable = yes
# smtp_sasl_password_maps = hash:/etc/postfix/sasl_passwd
# smtp_tls_security_level = encrypt

Επιλογή 2: msmtp (Lightweight SMTP Client)

sudo yum install -y msmtp msmtp-mta

# ~/.msmtprc ή /etc/msmtprc
cat > ~/.msmtprc <<'EOF'
defaults
auth           on
tls            on
tls_trust_file /etc/ssl/certs/ca-bundle.crt
logfile        /var/log/msmtp.log

account        gmail
host           smtp.gmail.com
port           587
from           your-email@gmail.com
user           your-email@gmail.com
password       your-app-password

account default : gmail
EOF

chmod 600 ~/.msmtprc
# Symlink για mailx
ln -sf /usr/bin/msmtp /usr/sbin/sendmail

Επιλογή 3: sSMTP (Simple - Deprecated αλλά ακόμα χρησιμοποιείται)

sudo yum install -y ssmtp

# /etc/ssmtp/ssmtp.conf
root=admin@example.com
mailhub=smtp.gmail.com:587
AuthUser=your-email@gmail.com
AuthPass=your-app-password
UseTLS=YES
UseSTARTTLS=YES
hostname=server.yourdomain.com

Δοκιμή αποστολής

# Test
echo "Test email from $(hostname) at $(date)" | mail -s "Test $(date)" admin@example.com

# Check logs
tail -f /var/log/maillog
# ή
journalctl -u postfix -f

Advanced: HTML Email με mailx

# mailx υποστηρίζει HTML με σωστά headers
cat <<'EOF' | mail -s "$(echo -e "HTML Report\nContent-Type: text/html")" admin@example.com


System Report

Server: $(hostname)

Date: $(date)

ServiceStatus
nginxRunning
postgresqlRunning
EOF

Integration με Cron & Scripts

# /etc/cron.daily/backup-report
#!/bin/bash
BACKUP_LOG="/var/log/backup.log"
if [ -f "$BACKUP_LOG" ]; then
    mail -s "Daily Backup Report - $(hostname)" admin@example.com < "$BACKUP_LOG"
fi

# Monitoring alert
if ! systemctl is-active --quiet nginx; then
    echo "NGINX is DOWN on $(hostname)!" | mail -s "ALERT: NGINX Down" admin@example.com
fi

Alternatives για Production

ToolProsCons
swaksSMTP testing, attachments, TLS, authenticationExtra install
muttFull MIME, attachments, IMAP/POP3Complex config
sendmail/postfixNative MTA, queue managementComplex setup
Python smtplibProgrammatic, flexibleRequires script

Troubleshooting

  • Email not delivered: Check spam folder, SPF/DKIM/DMARC records, provider limits
  • Connection timeout: Firewall blocks port 25/587/465, ISP blocks port 25
  • Authentication failed: Use App Passwords (Gmail), check 2FA settings
  • Local delivery only: Postfix configured as "Local only" - change inet_interfaces

Σχετικά άρθρα

Πνευματικά δικαιώματα © Γνώση Υπολογιστών Όλα τα δικαιώματα διατηρούνται.