Swap is an essential part of Linux systems, especially for systems with limited physical memory. It provides a backup for the RAM, ensuring that the system runs smoothly even when the RAM is fully utilized. Swap can exist as a file or a partition.

For performance reasons, a dedicated swap partition is preferred over a swap file, especially for systems with a large amount of RAM. This article details the steps to add a swap partition in Linux using existing partitions.

Using tools such as lsblk, you can get a list of existing partitions and decide which one to use or resize to make space for the swap partition. Before proceeding, ensure you've backed up any critical data, as adjusting partitions can lead to data loss.

Steps to add a swap partition in Linux:

  1. Check existing partitions using lsblk.
    $ lsblk
     sda 8:0 0 238.5G 0 disk
     ├─sda1 8:1 0 200M 0 part /boot/efi
     └─sda2 8:2 0 238.3G 0 part /

    Identify an unused partition or space where you'd like to create the wap partition.

  2. Convert the chosen partition to swap. Replace X with your actual partition number.
    $ sudo mkswap /dev/sdX
  3. Activate the new swap partition.
    $ sudo swapon /dev/sdX
  4. Update the fstab file to mount the swap partition during boot.
    $ echo "/dev/sdX none swap sw 0 0" | sudo tee -a /etc/fstab
  5. Confirm that the swap partition is active.
    $ cat /proc/swaps
    Filename Type Size Used Priority
    /dev/sdX partition 2097148 0 -1
  6. Check swap usage using the free command.
    $ free -h
    total used free shared buff/cache available
    Mem: 1.9G 155M 1.2G 8.8M 566M 1.6G
    Swap: 2.0G 0B 2.0G

    Ensure that the Swap line reflects the correct size and usage of your swap partition.

Now, your system is set up with a new swap partition, ensuring smoother performance even when RAM usage is high. Remember that the speed of accessing data from swap, especially if it's on a traditional HDD, is slower than RAM. Therefore, always monitor your system's RAM usage and consider adding more physical RAM if frequently full.

Discuss the article:

Comment anonymously. Login not required.