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.
Related: How to remove a swap file in Linux
Steps to add a swap file in Linux:
- Open a terminal with access to sudo commands.
$ whoami root
- 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.
- 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.
- 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.
- 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.
- Enable the swap file for the current session.
$ sudo swapon /mnt/data/swapfile
Activation takes effect immediately and does not require a reboot.
- 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.2GiThe presence of /mnt/data/swapfile in swapon --show output confirms that the kernel is using the new swap file.
- 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.
- Open /etc/fstab in a text editor with sudo privileges.
$ sudo nano /etc/fstab
- 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.
- 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.
- 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.
- 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
- 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.
- 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.
- Reload kernel parameters from the sysctl configuration.
$ sudo sysctl -p /etc/sysctl.conf vm.swappiness = 10
- Verify the updated swappiness value.
$ cat /proc/sys/vm/swappiness 10
Mohd Shakir Zakaria is a cloud architect with deep roots in software development and open-source advocacy. Certified in AWS, Red Hat, VMware, ITIL, and Linux, he specializes in designing and managing robust cloud and on-premises infrastructures.
