This involves installing and configuring an MTA (Mail Transfer Agent) like Postfix, writing a script to collect system data, and using a tool like `mailx` or `sendmail` to send the email.
### Step 1: Install and Configure Postfix
1. **Install Postfix**:
sudo apt update
sudo apt install postfix mailutils -y
“`
2. **Configure Postfix**:
During installation, you'll be prompted to choose a configuration. Select “Internet Site”. If you miss this step, you can reconfigure it:
sudo dpkg-reconfigure postfix
“`
– **Mail name**: Enter your domain name or hostname.
– **Root and postmaster mail recipient**: Leave it blank.
– **Other destinations to accept mail for**: Leave default.
– **Force synchronous updates on mail queue**: No.
– **Local networks**: Leave default (usually `127.0.0.0/8`).
– **Mailbox size limit**: 0 (unlimited).
– **Local address extension character**: `+`.
– **Internet protocols to use**: All.
3. **Check Postfix Status**:
sudo systemctl status postfix
“`
### Step 2: Write a Script to Collect System Data
Create a script (e.g., `system_report.sh`) that gathers the necessary system data.
#!/bin/bash
# Gather system information
hostname=$(hostname)
uptime=$(uptime -p)
disk_usage=$(df -h /)
memory_usage=$(free -h)
cpu_load=$(top -bn1 | grep “load average:” | awk '{print $10 $11 $12}')
users=$(who)
# Format the report
report=”System Report for $hostname
Uptime:
$uptime
Disk Usage:
$disk_usage
Memory Usage:
$memory_usage
CPU Load:
$cpu_load
Logged-in Users:
$users
“
# Save the report to a file
report_file=”/tmp/system_report.txt”
echo “$report” > $report_file
# Email the report
recipient=”your-email@example.com“
subject=”System Report for $hostname”
mail -s “$subject” “$recipient” < $report_file
# Clean up
rm $report_file
“`
### Step 3: Make the Script Executable
chmod +x system_report.sh
“`
### Step 4: Schedule the Script with Cron
1. **Open Crontab**:
crontab -e
“`
2. **Schedule the Script**:
Add a line to execute the script at your desired interval. For example, to run the script daily at 8 AM:
0 8 * * * /path/to/system_report.sh
“`
### Step 5: Test the Setup
Run the script manually to ensure it works correctly:
./system_report.sh
“`
Check your email for the system report.
### Additional Notes:
– Ensure that the `mailutils` package is installed, which provides the `mail` command.
– Make sure your firewall allows outgoing connections on port 25 (SMTP) if you are sending emails directly from your server.
– If you're using a third-party SMTP server (like Gmail), you might need to configure Postfix to relay through that server. This typically involves editing `/etc/postfix/main.cf` and adding relayhost and authentication settings.