swapon

system administrationLinux/Unix
The swapon command is one of the most frequently used commands in Linux/Unix-like operating systems. swapon Enable devices and files for swapping

Quick Reference

Command Name:

swapon

Category:

system administration

Platform:

Linux/Unix

Basic Usage:

swapon [options] [arguments]

Common Use Cases

    Syntax

    swapon [options] [special]

    Options

    Option Description
    -a, --all Enable all swap areas in /etc/fstab
    -d, --discard Enable swap discards for SSDs
    -e, --ifexists Silently skip devices that do not exist
    -f, --fixpgsz Reinitialize the swap space if necessary
    -o, --options OPTIONS Comma-separated list of swap options
    -p, --priority PRIORITY Specify the priority of the swap area
    -s, --summary Display summary about used swap devices
    --show[=COLUMNS] Display summary in definable table
    --output-all Output all available columns
    --noheadings Don't print headings
    --raw Use raw output format
    --bytes Print sizes in bytes
    -L, --label LABEL Enable swap by LABEL
    -U, --uuid UUID Enable swap by UUID
    -v, --verbose Be verbose
    -h, --help Display help and exit
    -V, --version Display version information and exit

    Examples

    How to Use These Examples

    The examples below show common ways to use the swapon command. Try them in your terminal to see the results. You can copy any example by clicking on the code block.

    # Basic Examples Basic
    swapon /dev/sdb2
    Enable swapping on the /dev/sdb2 partition.
    swapon /swapfile
    Enable swapping on the file /swapfile.
    swapon -a
    Enable all swap areas mentioned in /etc/fstab. # Advanced Examples Advanced # Create a new swap file and enable it dd if=/dev/zero of=/swapfile bs=1M count=1024 chmod 600 /swapfile mkswap /swapfile swapon /swapfile # Check swap status after enabling swapon -s # or
    cat /proc/swaps
    # Enable all swaps with verbose output swapon -av # Set swap priority (higher value means higher priority) swapon -p 10 /dev/sdb2 swapon -p 5 /swapfile # Show summary of swap usage swapon --show # Activate swap with specific UUID swapon -U 1234-5678-90ab-cdef # Enable swap by label swapon -L SWAP1 # Enable all swaps except those with noauto option swapon --all --verbose # Enable swap with discard option (useful for SSDs) swapon --discard /dev/sdb2 # Enable swap in read-only mode for hibernation swapon --ifexists /dev/sdb2 # Check memory before and after enabling swap free -h swapon /swapfile free -h # Enable swap and show filesystem usage swapon /dev/sdb2 df -h

    Try It Yourself

    Practice makes perfect! The best way to learn is by trying these examples on your own system with real files.

    Understanding Syntax

    Pay attention to the syntax coloring: commands, options, and file paths are highlighted differently.

    Notes

    The `swapon` command is a system administration utility in Linux and Unix-like operating systems that enables swap space for use by the kernel. Swap space serves as an extension of the system's physical memory (RAM), allowing the operating system to move less frequently used pages of memory to disk, freeing up RAM for more active processes. This command is essentially the counterpart to the `swapoff` command, which disables swap spaces. Together, they provide administrators with the ability to dynamically manage the swap configuration of a system without requiring a reboot. Swap space in Linux can be configured in two primary ways: 1. **Swap Partitions**: Dedicated disk partitions that are formatted as swap space using the `mkswap` command. 2. **Swap Files**: Regular files that have been allocated, formatted with `mkswap`, and designated for use as swap space. The `swapon` command can activate both types of swap space. When invoked with the `-a` option, it activates all swap areas listed in the `/etc/fstab` file that don't have the `noauto` option set. This is typically done during system startup by initialization scripts. One important feature of `swapon` is the ability to set priorities for swap areas using the `-p` option. When multiple swap spaces are active, the kernel will use the higher-priority areas first before moving to lower-priority ones. This allows administrators to optimize performance by prioritizing faster storage devices for swap usage. For solid-state drives (SSDs), the `-d` or `--discard` option enables the TRIM command, which helps maintain SSD performance over time by allowing the drive to efficiently manage its storage blocks. The `swapon -s` or `swapon --summary` option provides a convenient way to view the current swap configuration, showing details about all active swap areas including their size and usage. Proper swap management is an important aspect of system administration, particularly for systems with limited physical memory or those running memory-intensive applications. While modern systems with large amounts of RAM may rely less on swap space, it remains an important component of memory management in Linux systems, especially for supporting features like hibernation which requires sufficient swap space to store the contents of RAM.

    Related Commands

    These commands are frequently used alongside swapon or serve similar purposes:

    Use Cases

    Learn By Doing

    The best way to learn Linux commands is by practicing. Try out these examples in your terminal to build muscle memory and understand how the swapon command works in different scenarios.

    $ swapon
    View All Commands