LinuxCommandLibrary

swapon

Activate swap space (paging) on Linux

TLDR

Show swap information

$ swapon
copy

Enable a given swap area
$ swapon [path/to/file]
copy

Enable all swap areas specified in /etc/fstab except those with the noauto option
$ swapon [[-a|--all]]
copy

Enable a swap partition by its label
$ swapon -L [label]
copy

SYNOPSIS

swapon [options] [device]
swapoff [options] [device]

PARAMETERS

-a, --all
    Enables all swap devices listed in /etc/fstab.

-d, --discard
    Enable discard (TRIM) of swap pages when they are no longer needed. Requires kernel support.

-D, --discard=policy
    Specify discard policy. Possible values: once, pages, always.

-e, --ifexists
    Silently skip devices that do not exist.

-h, --help
    Display help text and exit.

-L, --label
    Enables the swap device that has the specified label. Use with swapoff to disable by label.

-o, --options
    Specify swap options for the device. Common options include 'pri=', 'discard', 'nofail'.

-p, --priority
    Specify the priority of the swap device. Higher values indicate higher priority. Default is -1.

-s, --show
    Display swap usage summary (equivalent to swapon -s or cat /proc/swaps).

-U, --uuid
    Enables the swap device that has the specified UUID. Use with swapoff to disable by UUID.

-v, --verbose
    Enable verbose output.


    The device to enable/disable swap on (e.g., /dev/sda5, /swapfile).

DESCRIPTION

The swapon and swapoff commands are used to manage swap spaces on Linux systems. Swap space allows the operating system to use disk space as virtual memory when the physical RAM is exhausted. This prevents applications from crashing due to out-of-memory errors, although it comes at the cost of performance since disk access is much slower than RAM access. swapon enables swap space, allowing the kernel to begin using it. Multiple swap spaces can be active simultaneously, allowing for a larger total swap capacity. The system prioritizes swap spaces based on their assigned priorities; higher priority spaces are used before lower priority ones. swapoff disables swap space, preventing the kernel from using it for virtual memory. It can also be used to prepare a swap partition for other operations, such as resizing or reformatting. Typically swap spaces are defined inside /etc/fstab to allow the system to automatically activate them upon boot.

CAVEATS

Disabling swap space can cause applications to crash if the system runs out of physical memory. It is generally recommended to have some swap space configured, even on systems with large amounts of RAM. Discarding swap space can affect performance positively on SSD and NVMe drives. Running swapoff on a heavily used swap partition can take a significant amount of time.

FILES

/proc/swaps: Contains information about active swap spaces. /etc/fstab: Contains the list of swap devices to automatically activate upon boot.

EXIT STATUS

The swapon command returns 0 on success, and non-zero on failure. For example, if it fails to locate any devices inside /etc/fstab during swapon -a

HISTORY

The swapon command has been a part of the Linux kernel and the util-linux package for a very long time, dating back to the early days of Linux. Its purpose has remained consistent: to manage swap space for virtual memory. Over time, features like discard support and UUID/label-based activation have been added to enhance functionality and flexibility.

SEE ALSO

free(1), mkswap(8), fstab(5), vmstat(8), sysctl(8)

Copied to clipboard