In a Linux system, swap space acts as a virtual extension of the physical memory (RAM). When RAM is fully utilized, the system moves less active processes to swap space, freeing up memory for more immediate tasks. This helps prevent system slowdowns and ensures smooth operation, especially when running memory-intensive applications. A common approach is to use a swap partition, but in many cases, adding a swap file provides a more flexible alternative.

A swap file functions similarly to a swap partition but is easier to manage. You can create, resize, or remove it without modifying disk partitions. For systems where a swap partition wasn’t created during installation, or where more memory is needed, a swap file offers a simple solution. Unlike fixed partitions, a swap file can be created dynamically based on current needs.

For users who didn’t initially configure a swap partition or require more swap space, creating a swap file in Linux is a straightforward process that allows for quick adjustments to the system's memory capacity. The following steps explain how to create, enable, and verify a swap file.

Steps to add a swap file in Linux:

  1. Open the terminal.
  2. Decide the size of the swap file to create.

    For example, for a 2GB swap file, you would specify the size in the next command using 2048MB.

  3. Create the swap file.
    $ sudo dd if=/dev/zero of=/swapfile bs=1M count=2048 status=progress
    2048000000 bytes (2.0 GB, 1.9 GiB) copied, 4 s, 500 MB/s
  4. Set the file permissions to secure the swap file.
    $ sudo chmod 600 /swapfile

    Setting the correct permissions ensures that only the root user can access and modify the swap file.

  5. Format the file for use as swap space.
    $ sudo mkswap /swapfile
    Setting up swapspace version 1, size = 2 GiB (2147479552 bytes)
    no label, UUID=e6c5b73a-92a7-444f-9b89-32d764b19a89
  6. Enable the swap file.
    $ sudo swapon /swapfile

    Activating the swap file with the swapon command will enable it for the current session. To make it persistent across reboots, continue with the next steps.

  7. Make the swap file permanent by adding it to fstab.
    $ sudo nano /etc/fstab
  8. Add the following line to the end of the file:
    /swapfile none swap sw 0 0
  9. Save and exit the editor.

    To save the changes in the nano editor, press Ctrl+O to write out, then Ctrl+X to exit.

  10. Verify that the swap file is active.
    $ free -h
                  total        used        free      shared  buff/cache   available
    Mem:          1.9Gi       256Mi       1.3Gi        17Mi       342Mi       1.4Gi
    Swap:         2.0Gi          0B       2.0Gi
  11. Adjust the swap usage settings (optional).
    $ echo vm.swappiness=10 | sudo tee -a /etc/sysctl.conf

    The swappiness parameter controls how aggressively the system uses swap space. A lower value makes the system prefer physical memory, while a higher value makes it use swap space more frequently.

Discuss the article:

Comment anonymously. Login not required.