Server Maintenance

Keep your Linux server healthy with these essential maintenance tasks every sysadmin should run regularly.

Contents

  1. System Updates
  2. Disk Usage & Cleanup
  3. Check System Health
  4. Review Logs
  5. User & Session Management
  6. Schedule Maintenance with Cron

System Updates

Keep all packages up to date and remove unused dependencies.

# Update package lists and upgrade all packages
sudo apt update && sudo apt upgrade -y

# Remove unused packages and clean cache
sudo apt autoremove -y && sudo apt autoclean

# Check if a reboot is needed
cat /var/run/reboot-required 2>/dev/null && echo "Reboot required" || echo "No reboot needed"

Disk Usage & Cleanup

Monitor disk usage and find space hogs before they fill your drive.

# Overall disk usage by filesystem
df -h

# Find the largest directories (from root)
du -sh /* 2>/dev/null | sort -rh | head -20

# Find files larger than 500 MB
find / -type f -size +500M -exec ls -lh {} \; 2>/dev/null

# Clean up old journal logs
sudo journalctl --vacuum-time=30d
sudo journalctl --vacuum-size=500M

Check System Health

Quick commands to assess CPU, memory, and service status.

# Interactive process viewer (install htop if needed)
htop

# Memory usage summary
free -h

# Load average: 1m / 5m / 15m
uptime

# List any failed systemd services
systemctl --failed

Review Logs

Regularly scanning logs helps catch intrusion attempts and misconfigurations early.

# Recent warnings and errors
journalctl -n 100 -p warning

# SSH authentication failures
grep "Failed password" /var/log/auth.log | tail -20

# Kernel messages (hardware issues, OOM killer)
dmesg | tail -30

User & Session Management

Audit who has access and review recent login activity.

# Show currently logged-in users
who

# List all local user accounts
cut -d: -f1 /etc/passwd

# Last 20 login events
last -20

# Lock a dormant account
sudo passwd -l username

Schedule Maintenance with Cron

Automate repetitive tasks so maintenance runs even when you’re not around.

sudo crontab -e

# Daily update at 3 AM
0 3 * * * apt update && apt upgrade -y >> /var/log/autoupdate.log 2>&1

# Weekly disk cleanup on Sunday at 4 AM
0 4 * * 0 apt autoremove -y && journalctl --vacuum-time=30d

Tip: Run sudo unattended-upgrades --dry-run first to preview what will be updated before applying changes automatically.