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
- Check existing swap devices.
$ swapon --show NAME TYPE SIZE USED PRIO /var/lib/swap file 1024M 0B -2
- Prepare a small ext4 volume for the swap file (useful when the root filesystem does not support swap files directly).
$ sudo dd if=/dev/zero of=/root/sg-work/swapfs.img bs=1M count=128 $ sudo losetup --find --show /root/sg-work/swapfs.img /dev/loop0 $ sudo mkfs.ext4 -F /dev/loop0 Discarding device blocks: 0/32768 done Creating filesystem with 32768 4k blocks and 32768 inodes Allocating group tables: 0/1 done Writing inode tables: 0/1 done Creating journal (4096 blocks): done Writing superblocks and filesystem accounting information: 0/1 done $ sudo mount /dev/loop0 /mnt/data
Mounting the loop-backed ext4 image under /mnt/data provides a block-based location for the swap file.
- Create and secure the swap file on the mounted volume.
$ sudo dd if=/dev/zero of=/mnt/data/swapfile bs=1M count=64 $ sudo chmod 600 /mnt/data/swapfile
- Format the new file for use as swap space.
$ sudo mkswap /mnt/data/swapfile Setting up swapspace version 1, size = 64 MiB (67104768 bytes) no label, UUID=61eb20c2-5a21-4a87-ae1e-de28fe7d4e60
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.
$ swapon --show NAME TYPE SIZE USED PRIO /var/lib/swap file 1024M 0B -2 /mnt/data/swapfile file 64M 0B -3 $ free -h total used free shared buff/cache available Mem: 23Gi 1.3Gi 20Gi 13Mi 1.6Gi 21Gi Swap: 1.1Gi 0B 1.1GiThe 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.
- Append a persistent swap line and validate it.
$ echo /mnt/data/swapfile none swap sw 0 0 | sudo tee -a /etc/fstab /mnt/data/swapfile none swap sw 0 0 $ sudo mount -a $ grep swapfile /etc/fstab 2:/mnt/data/swapfile none swap sw 0 0
After confirming the entry, keep the /root/sg-work/swapfs.img loop device and /mnt/data mount available at boot so the swap file remains accessible.
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.
