> ## Documentation Index
> Fetch the complete documentation index at: https://docs.digitalfyre.com/llms.txt
> Use this file to discover all available pages before exploring further.

# Linux Update Script

A single script that detects your distribution and runs the correct update commands. Supports Debian/Ubuntu, RHEL-family (AlmaLinux, Rocky, Fedora, CentOS, CloudLinux), Arch, openSUSE, and Alpine.

<Note>
  This script is for VPS and Bare Metal customers managing their own servers. Web Hosting and WordPress Hosting customers do not need to manage OS-level updates.
</Note>

## The Script

```bash update.sh expandable lines theme={"theme":{"light":"light-plus","dark":"ayu-dark"}}
#!/bin/bash
set -euo pipefail

# Detect distribution
if [ -f /etc/os-release ]; then
    . /etc/os-release
    DISTRO="$ID"
else
    echo "Cannot detect distribution. /etc/os-release not found."
    exit 1
fi

echo "Detected distribution: $DISTRO"
echo "Starting system update..."

case "$DISTRO" in
    debian|ubuntu)
        apt update
        apt upgrade -y
        apt autoremove -y
        apt autoclean
        ;;
    almalinux|rocky|centos|rhel|cloudlinux|fedora)
        if command -v dnf &>/dev/null; then
            dnf upgrade -y
            dnf autoremove -y
            dnf clean all
        else
            yum update -y
            yum autoremove -y
            yum clean all
        fi
        ;;
    arch|manjaro)
        pacman -Syu --noconfirm
        # Remove orphaned packages
        ORPHANS=$(pacman -Qdtq 2>/dev/null) || true
        if [ -n "$ORPHANS" ]; then
            pacman -Rns --noconfirm $ORPHANS
        fi
        ;;
    opensuse*|sles)
        zypper refresh
        zypper update -y
        zypper clean --all
        ;;
    alpine)
        apk update
        apk upgrade
        # Remove cached packages
        apk cache clean 2>/dev/null || rm -rf /var/cache/apk/*
        ;;
    *)
        echo "Unsupported distribution: $DISTRO"
        exit 1
        ;;
esac

echo "Update complete."

# Show if a reboot is required (Debian/Ubuntu)
if [ -f /var/run/reboot-required ]; then
    echo "*** A reboot is required to apply kernel updates. ***"
fi

# Show if a reboot is required (RHEL-family)
if command -v needs-restarting &>/dev/null; then
    if needs-restarting -r &>/dev/null; then
        true
    else
        echo "*** A reboot is required to apply kernel updates. ***"
    fi
fi
```

## Usage

```bash lines theme={"theme":{"light":"light-plus","dark":"ayu-dark"}}
# Download and run directly
curl -fsSL https://raw.githubusercontent.com/your-repo/update.sh | bash

# Or save locally and run
curl -fsSL https://raw.githubusercontent.com/your-repo/update.sh -o /usr/local/bin/update.sh
chmod +x /usr/local/bin/update.sh
update.sh
```

<Danger>
  Review any script before running it with `curl | bash`. You can inspect the script first by removing `| bash` from the command.
</Danger>

## Automating with Cron

To run updates automatically on a schedule:

```bash lines theme={"theme":{"light":"light-plus","dark":"ayu-dark"}}
crontab -e
```

```cron lines theme={"theme":{"light":"light-plus","dark":"ayu-dark"}}
# Run updates every Sunday at 3:00 AM
0 3 * * 0 /usr/local/bin/update.sh >> /var/log/auto-update.log 2>&1
```

<Tip>
  Automated updates are convenient but can introduce breaking changes. For production servers, consider running the script manually or scheduling it during a maintenance window where you can monitor the output.
</Tip>

## What Each Package Manager Does

| Distribution    | Update                          | Cleanup                            |
| --------------- | ------------------------------- | ---------------------------------- |
| Debian / Ubuntu | `apt update` + `apt upgrade`    | `apt autoremove` + `apt autoclean` |
| RHEL family     | `dnf upgrade` (or `yum update`) | `dnf autoremove` + `dnf clean all` |
| Arch            | `pacman -Syu`                   | Removes orphaned packages          |
| openSUSE        | `zypper update`                 | `zypper clean --all`               |
| Alpine          | `apk upgrade`                   | Clears APK cache                   |
