A swap partition in Linux extends physical memory by providing disk-backed space for inactive pages, reducing the chance of out-of-memory conditions when applications allocate more RAM than is physically available. Swap space allows background workloads and bursty processes to continue running at reduced performance instead of being terminated abruptly.
The kernel treats swap areas as special block devices configured with mkswap and activated with swapon and swapoff, while activation at boot is controlled through entries in /etc/fstab. Using a dedicated swap partition avoids filesystem overhead and can provide more predictable performance than a swap file on heavily used volumes.
Configuring a swap partition requires root privileges and careful device selection because formatting the wrong partition irreversibly destroys data. Incorrect entries in /etc/fstab can also prevent the operating system from booting, so a verified backup and a clear understanding of the disk layout are strongly recommended before any changes are made.
Steps to create and add a swap partition in Linux:
- Open a terminal session with sudo privileges.
$ whoami root
- Display current swap usage to understand existing configuration.
$ swapon --show NAME TYPE SIZE USED PRIO /var/lib/swap file 1024M 0B -2
If no swap areas are present, the command returns no output.
- List block devices to confirm available loop devices for the swap partition.
$ lsblk NAME MAJ:MIN RM SIZE RO TYPE MOUNTPOINTS nbd0 43:0 0 0B 0 disk ##### snipped #####
- Create a raw block file and attach it as a loop device to act as the swap partition.
$ sudo dd if=/dev/zero of=/root/sg-work/loop14.img bs=1M count=128 $ sudo losetup --find --show /root/sg-work/loop14.img /dev/loop0
A loop-backed file is a safe way to demonstrate swap partition commands without touching real disks.
- Format the loop device as swap space using mkswap.
$ sudo mkswap /dev/loop0 Setting up swapspace version 1, size = 128 MiB (134213632 bytes) no label, UUID=6c2b5271-7333-4ce9-a493-21ac96c28bb0
Running mkswap on a device that contains data irreversibly destroys the existing contents of that device.
- Activate the new swap partition immediately using swapon.
$ sudo swapon /dev/loop0
The swapon command enables the new swap space immediately without requiring a reboot.
- Verify that the partition is active as swap space.
$ swapon --show NAME TYPE SIZE USED PRIO /var/lib/swap file 1024M 0B -2 /dev/loop0 partition 128M 0B -3 $ cat /proc/swaps Filename Type Size Used Priority /var/lib/swap file 1048572 0 -2 /dev/loop0 partition 131068 0 -3
The PRIO column controls which swap area is used first when multiple swap regions are active.
- Retrieve the UUID of the swap partition for use in /etc/fstab.
$ sudo blkid /dev/loop0 /dev/loop0: UUID="6c2b5271-7333-4ce9-a493-21ac96c28bb0" TYPE="swap"
The UUID is stable across reboots and preferred over raw device names in /etc/fstab entries.
- Add a swap entry line using the partition UUID and reload swap configuration.
$ echo UUID=6c2b5271-7333-4ce9-a493-21ac96c28bb0 none swap sw 0 0 | sudo tee -a /etc/fstab UUID=6c2b5271-7333-4ce9-a493-21ac96c28bb0 none swap sw 0 0 $ sudo swapoff /dev/loop0 $ sudo swapon -a
The combination of swapoff and swapon -a simulates a reboot for swap configuration without restarting the kernel.
- Verify total swap capacity and current usage.
$ swapon --show NAME TYPE SIZE USED PRIO /var/lib/swap file 1024M 0B -2 /dev/loop0 partition 128M 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 Swap line confirms that the configured swap capacity is available for memory paging.
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.
