Swap space on a Linux system extends physical memory by placing infrequently used data on disk instead of in RAM. When available memory becomes scarce, the kernel moves idle pages into swap so that active processes can keep running. Adequate swap capacity reduces out-of-memory conditions and helps maintain stability under heavy load.

Many installations use a dedicated swap partition, but a swap file on the main filesystem provides more flexibility. A swap file can be created, resized, or removed without repartitioning disks, which is particularly useful on virtual machines, cloud instances, and systems that were deployed without swap. When configured correctly, the kernel treats a swap file similarly to a swap partition in typical workloads.

Creating a swap file modifies low-level storage settings and adds an entry to /etc/fstab, so misconfiguration can affect boot behavior or performance. The procedure assumes sudo access on a Linux system with sufficient free disk space on the target filesystem. Careful editing of configuration files and validation with status commands prevents unexpected failures.

Steps to add a swap file in Linux:

  1. Open a terminal with access to sudo commands.
    $ whoami
    root
  2. Determine the desired swap file size in mebibytes or gibibytes.

    For a 256 MiB swap file, a value of count=256 with bs=1M creates a 256 MiB sparse-free file.

  3. Create the swap file on a filesystem with enough free space.
    $ sudo dd if=/dev/zero of=/mnt/data/swapfile bs=1M count=256
    256+0 records in
    256+0 records out
    268435456 bytes (268 MB, 256 MiB) copied, 0.541179 s, 496 MB/s

    Placing the swap file on a data filesystem such as /mnt/data keeps the swap isolated from the root volume.

  4. Restrict permissions on the swap file so only the root user can read or write it.
    $ sudo chmod 600 /mnt/data/swapfile

    Permissions of 600 prevent non-privileged processes from inspecting or altering swap contents.

  5. Format the new file for use as swap space.
    $ sudo mkswap /mnt/data/swapfile
    Setting up swapspace version 1, size = 256 MiB (268431360 bytes)
    no label, UUID=9233db23-5cad-46ff-8ec6-9f1a0141557a

    The mkswap command writes swap metadata so the kernel can recognize the file as usable swap space.

  6. Enable the swap file for the current session.
    $ sudo swapon /mnt/data/swapfile

    Activation takes effect immediately and does not require a reboot.

  7. Verify that the new swap space is active.
    $ sudo swapon --show
    NAME               TYPE  SIZE USED PRIO
    /var/lib/swap      file   4G   0B   -2
    /mnt/data/swapfile file  256M   0B   -3
    
    $ free -h
                   total        used        free      shared  buff/cache   available
    Mem:            11Gi       1.2Gi       9.5Gi        30Mi       1.2Gi        10Gi
    Swap:          4.2Gi          0B       4.2Gi

    The presence of /mnt/data/swapfile in swapon --show output confirms that the kernel is using the new swap file.

  8. Create a backup of /etc/fstab before adding a persistent swap entry.
    $ sudo cp /etc/fstab /etc/fstab.bak

    An incorrect line in /etc/fstab can prevent a system from booting; the backup simplifies recovery from a rescue shell.

  9. Open /etc/fstab in a text editor with sudo privileges.
    $ sudo nano /etc/fstab
  10. Add a line at the end of /etc/fstab describing the swap file entry.
    /mnt/data/swapfile none swap sw 0 0

    The sw options field enables swap with default behavior, and the trailing zeros disable dump and fsck checks for the swap file.

  11. Write the changes and exit the editor.

    In nano, press Ctrl+O to write the file, confirm the filename with Enter, then press Ctrl+X to return to the shell.

  12. Test the updated /fstab configuration.
    $ sudo mount -a

    The absence of error messages from mount -a usually indicates that all /fstab entries, including the swap file line, are valid.

  13. Confirm that the swap file remains configured after the /fstab update.
    $ sudo swapon --show
    NAME               TYPE  SIZE USED PRIO
    /var/lib/swap      file   4G   0B   -2
    /mnt/data/swapfile file  256M   0B   -3
  14. Display the current swappiness value that controls how aggressively swap is used.
    $ cat /proc/sys/vm/swappiness
    60

    Higher swappiness values cause the kernel to move pages to swap more readily, while lower values favor keeping data in RAM.

  15. Append a persistent swappiness setting to /etc/sysctl.conf if a different behavior is desired.
    $ echo vm.swappiness=10 | sudo tee -a /etc/sysctl.conf
    vm.swappiness=10

    An excessively low swappiness value can increase the risk of out-of-memory conditions under heavy load, while a high value can increase disk I/O and reduce responsiveness.

  16. Reload kernel parameters from the sysctl configuration.
    $ sudo sysctl -p /etc/sysctl.conf
    vm.swappiness = 10
  17. Verify the updated swappiness value.
    $ cat /proc/sys/vm/swappiness
    10