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:

  1. 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.

  2. 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.

  3. 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.

  4. 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.

  5. 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.

  6. Enable the swap partition for the current session.
    $ sudo swapon /dev/sdb2
  7. 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
  8. 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

  9. 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.

  10. 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
  11. Disable the direct activation before testing the /etc/fstab entry.
    $ sudo swapoff /dev/sdb2
  12. 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.

  13. 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