Adding a swap partition is useful when a Linux system has unallocated disk space that should become persistent swap rather than a removable swap file. The main risk is choosing the wrong device because the partitioning command writes directly to the selected disk, and mkswap overwrites the target partition signature.
A swap partition is prepared in two layers. parted creates a partition entry in the disk partition table, and mkswap writes the swap signature that swapon can activate. Boot-time activation comes from /etc/fstab, where the partition's UUID is safer than a device name that can change after hardware or boot-order changes.
Assume the target disk already has unallocated space and that the selected range is not mounted, encrypted, or assigned to another volume manager. Back up important data and /etc/fstab before changing storage; an incorrect partition range can destroy data, and an invalid swap entry can slow or interrupt the next boot.
Steps to create and add a swap partition in Linux:
- Check current swap areas.
$ swapon --show NAME TYPE SIZE USED PRIO /var/lib/swap file 4G 0B -2
No output means the system has no active swap areas.
- Display free space on the target disk.
$ sudo parted /dev/sdb --script unit MiB print free Model: Example Virtual Disk (scsi) Disk /dev/sdb: 512MiB Sector size (logical/physical): 512B/512B Partition Table: gpt Disk Flags: Number Start End Size File system Name Flags 0.02MiB 1.00MiB 0.98MiB Free Space 1 1.00MiB 257MiB 256MiB data 257MiB 512MiB 255MiB Free Space
Replace /dev/sdb with the real disk path. Do not choose a mounted filesystem partition, and do not choose a free range that overlaps existing data.
- Create the swap partition in the selected free range.
$ sudo parted /dev/sdb --script mkpart primary linux-swap 257MiB 100%
Use the start and end values from the chosen Free Space row. On GPT disks, primary becomes the partition name; on MBR disks, it selects a primary partition.
- Confirm the new partition number.
$ sudo parted /dev/sdb --script unit MiB print Model: Example Virtual Disk (scsi) Disk /dev/sdb: 512MiB Sector size (logical/physical): 512B/512B Partition Table: gpt Disk Flags: Number Start End Size File system Name Flags 1 1.00MiB 257MiB 256MiB data 2 257MiB 511MiB 254MiB primary swap
The new partition in this example is /dev/sdb2. Use the partition number shown on your system in the remaining commands.
- Format the new partition as swap.
$ sudo mkswap /dev/sdb2 Setting up swapspace version 1, size = 254 MiB (266334208 bytes) no label, UUID=e4bea310-a336-49e6-9695-cb9474ecb633
mkswap erases the existing signature on the selected partition. Stop here if the partition path does not match the new swap partition.
- Enable the swap partition for the current session.
$ sudo swapon /dev/sdb2
- Verify that the swap partition is active.
$ swapon --show NAME TYPE SIZE USED PRIO /var/lib/swap file 4G 0B -2 /dev/sdb2 partition 254M 0B -3
- Read the swap partition UUID.
$ sudo blkid /dev/sdb2 /dev/sdb2: UUID="e4bea310-a336-49e6-9695-cb9474ecb633" TYPE="swap" PARTLABEL="primary" PARTUUID="640ff53b-4f40-43b5-8fca-7844ceccf3d8"
Use the UUID value, not PARTUUID, for the /etc/fstab swap entry.
Related: How to get a disk or partition UUID in Linux - Back up /etc/fstab.
$ sudo cp /etc/fstab /etc/fstab.bak
An incorrect /etc/fstab line can delay or interrupt the next boot. Keep the backup until the system has restarted cleanly.
- Add the persistent swap entry.
$ echo "UUID=e4bea310-a336-49e6-9695-cb9474ecb633 none swap sw 0 0" | sudo tee -a /etc/fstab UUID=e4bea310-a336-49e6-9695-cb9474ecb633 none swap sw 0 0
- Disable the direct activation before testing the /etc/fstab entry.
$ sudo swapoff /dev/sdb2
- Reactivate configured swap entries.
$ sudo swapon --all --verbose swapon /dev/sdb2
swapon –all reads the swap entries in /etc/fstab and skips swap areas that are already active.
- Confirm the swap partition is active after the /etc/fstab test.
$ swapon --show NAME TYPE SIZE USED PRIO /var/lib/swap file 4G 0B -2 /dev/sdb2 partition 254M 0B -3
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.