LinuxCommandLibrary

dphys-swapfile

Create and manage swap file on Debian systems

TLDR

Disable the swap file

$ dphys-swapfile swapoff
copy

Enable the swap file
$ dphys-swapfile swapon
copy

Create a new swap file
$ dphys-swapfile setup
copy

SYNOPSIS

dphys-swapfile [setup|start|stop|status|resize|swapon|swapoff|uninstall]

PARAMETERS

setup
    Reread config, create or recreate swapfile if needed.

start
    Activate swapfile (includes setup if missing).

stop
    Deactivate and unmount swapfile.

status
    Show current swapfile status and usage.

resize
    Adjust swapfile size to config value (stops swap first).

swapon
    Directly run swapon on the swapfile.

swapoff
    Directly run swapoff on the swapfile.

uninstall
    Remove swapfile and clean up.

DESCRIPTION

dphys-swapfile is a lightweight shell script utility for managing swap files on Linux systems, especially Debian derivatives like Raspbian on Raspberry Pi. It automates creation, activation, resizing, and removal of a single swap file, ideal for systems lacking a swap partition. Configuration is stored in /etc/dphys-swapfile, where users set size (CONF_SWAPSIZE in MB) and location (CONF_SWAPFILE, default /var/swap). The tool uses fallocate for efficient allocation, mkswap for formatting, and swapon/swapoff for activation. Actions like setup prepare the file, resize adjusts size without reboot, and status reports usage. Commonly run as a systemd service at boot, it helps tune memory on low-RAM devices. Requires root and supports tmpfs checks to avoid issues.

CAVEATS

Requires root privileges.
Swapfile must use supported filesystems (ext4, etc.).
Permissions: owned by root, mode 0600.
Not for swap partitions; single file only.
Resizing needs swapoff first.

CONFIGURATION

Edit /etc/dphys-swapfile: CONF_SWAPSIZE=100 sets MB size; CONF_SWAPFILE=/var/swap sets path. Run setup after changes.

SERVICE MANAGEMENT

Systemd service: systemctl {start|stop|enable|status} dphys-swapfile. Starts at boot by default.

HISTORY

Developed by Adrian Midgley (dphys.com) around 2008 for embedded systems. Packaged in Debian since 2010, widely used in Raspbian/Raspberry Pi OS for dynamic RAM management.

SEE ALSO

swapon(8), swapoff(8), fallocate(1), mkswap(8), free(1)

Copied to clipboard