Skip to content
Search AlmaLinux

    Browse this documentation

    AlmaLinux 9: swap sizing and swapfile steps

    Executed in an almalinux:9 container (AlmaLinux 9.8): fallocate -l 64M /swapfile, chmod 600, mkswap, swapon, cat /proc/swaps. Log: plans/content-expansion/evidence/logs/alma-swap-sizing.log.

    swapon is refused in the container (Operation not permitted), so the “enable it” step is not verified here and must run on a real system.

    Terminal window
    cat /proc/swaps
    swapon --show
    Filename Type Size Used Priority
    /dev/zram0 partition 24639296 22432652 32767
    /dev/vdc partition 1048572 385292 1

    Output means swap is in use; no output means none is active.

    RAMSuggested swap
    ≤ 2 GB2× RAM
    2–8 GBEqual to RAM
    8–64 GBAt least 4 GB
    > 64 GBWorkload-dependent, at least 4 GB

    These are Red Hat’s recommended ranges (RHEL 9 storage docs, checked 2026-10-19); AlmaLinux follows the same guidance as a RHEL-compatible distribution. A starting point, not a law — databases and container hosts should size from real workload data.

    Terminal window
    sudo fallocate -l 4G /swapfile
    sudo chmod 600 /swapfile
    ls -l /swapfile
    sudo mkswap /swapfile
    -rw------- 1 root root 4294967296 ... /swapfile
    Setting up swapspace version 1, size = 4 GiB (4294963200 bytes)
    no label, UUID=...

    chmod 600 is mandatory: with 644 mkswap warns and swapon refuses, because a readable swapfile leaks memory contents.

    Terminal window
    sudo swapon /swapfile
    swapon --show
    echo '/swapfile none swap defaults 0 0' | sudo tee -a /etc/fstab

    Only when swapon --show lists /swapfile is it active; the /etc/fstab line keeps it after reboot.

    Container limitation: inside almalinux:9, fallocate, chmod and mkswap all succeed, but swapon returns Operation not permitted. That is a privilege limitation, not a configuration error; finish the enable and reboot check on a real system.

    • A filesystem without preallocation support: create the file with dd instead of fallocate;
    • “Higher swappiness is better” is a myth: database workloads usually lower it (e.g. 10) so the kernel prefers RAM;
    • zram and swapfile are not either/or: small hosts can prefer zram with a swapfile as a backstop, but both count in capacity planning.
    Terminal window
    sudo fallocate -l 4G /swapfile
    sudo chmod 600 /swapfile
    sudo mkswap /swapfile
    sudo swapon /swapfile
    echo '/swapfile none swap defaults 0 0' | sudo tee -a /etc/fstab

    Checks:

    1. swapon --show → a /swapfile entry of about 4G
    2. cat /proc/swaps | wc -l → one line more than before
    3. After reboot, swapon --show → still there
    4. sudo swapoff /swapfile && sudo swapon /swapfile → disables and re-enables cleanly

    常见问题

    Is `swapon: Operation not permitted` a permissions problem?

    In a container it is usually a capability limit rather than file permissions: fallocate, chmod and mkswap succeed and only swapon is refused. Retry on a real host or an environment with the needed privileges.

    Why must the swapfile be mode 600?

    Swap may contain sensitive in-memory data. With 644 mkswap warns and swapon refuses, because other users could read the file.

    Do I still need a swapfile if zram is enabled?

    It depends: zram compresses memory and suits small hosts, while a swapfile provides disk-backed overflow and supports hibernation. They can coexist, but plan capacity and IO for both.