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 user $ sudo -v
The sudo -v command refreshes cached credentials so subsequent privileged commands run without additional prompts for a short period.
- Display current swap usage to understand existing configuration.
$ swapon --show
If no swap areas are present, the command returns no output.
- List block devices to locate an unused partition that can become swap space.
$ lsblk NAME MAJ:MIN RM SIZE RO TYPE MOUNTPOINTS sda 8:0 0 238.5G 0 disk ├─sda1 8:1 0 512M 0 part /boot/efi └─sda2 8:2 0 238.0G 0 part / sdb 8:16 0 8G 0 disk └─sdb1 8:17 0 8G 0 part
In this example, /dev/sdb1 is available for swap because the MOUNTPOINTS column is empty; device names and sizes differ between systems.
- Format the selected partition as swap space using mkswap.
$ sudo mkswap /dev/sdb1 Setting up swapspace version 1, size = 8 GiB (8589930496 bytes) no label, UUID=2e347f4d-1f01-4b9c-8b2e-87a5a4108b1c
Running mkswap on a partition that contains data irreversibly destroys the existing contents of that partition.
- Activate the new swap partition immediately using swapon.
$ sudo swapon /dev/sdb1
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 /dev/sdb1 partition 8G 0B -2 $ cat /proc/swaps Filename Type Size Used Priority /dev/sdb1 partition 8388604 0 -2
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/sdb1 /dev/sdb1: UUID="2e347f4d-1f01-4b9c-8b2e-87a5a4108b1c" TYPE="swap"
The UUID is stable across reboots and preferred over raw device names in /etc/fstab entries.
- Open /etc/fstab in a text editor with root privileges.
$ sudo nano /etc/fstab
Incorrect entries in /etc/fstab can prevent the operating system from booting successfully.
- Add a swap entry line using the partition UUID.
# Existing file system entries UUID=2e347f4d-1f01-4b9c-8b2e-87a5a4108b1c none swap sw 0 0
Replace the example UUID with the value reported by blkid for the swap partition.
- Reload swap configuration from /etc/fstab to confirm that the new entry works.
$ sudo swapoff /dev/sdb1 $ 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.
$ free -h total used free shared buff/cache available Mem: 15Gi 2.0Gi 8.0Gi 512Mi 5.0Gi 12Gi Swap: 8Gi 0B 8.0GiThe 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.
Comment anonymously. Login not required.
