Following my previous post on creating a swap file, I realized it would be worthwhile to discuss another approach: adding an entirely new swap partition. While swap files are excellent for emergency situations, a dedicated swap partition often provides better performance for long-term use.

Prerequisites

Before we begin, you’ll need:

  • A separate hard drive that is either not currently mounted or can be safely unmounted using the standard umount command
  • Root/sudo access to your system

For this guide, I’ll assume the device is /dev/sdc, but your system might use a different device name (such as /dev/sdb, /dev/sda, /dev/hda1, etc.).

Step-by-Step Process

  1. Ensure the drive is unmounted If the drive already contains a swap partition that’s in use, disable it first:
    swapoff /dev/sdc
    If the drive is clean and doesn’t have any swap partitions, you can skip this step.
  2. Access the partition manager Launch the parted utility to manage the disk partitioning:
    parted /dev/sdc
    This will load the parted prompt, which provides an interface for manipulating disk partitions.
  3. View existing partitions At the parted prompt, enter:
    print
    This will display details of any existing partitions and available free space on the disk.
  4. Create the new swap partition Once you’ve identified how much space you want to allocate (at minimum, I recommend matching your RAM size), create the partition with:
    mkpartfs part-type linux-swap start end
    Replace start and end with appropriate values shown from the print command. For example, if the starting point is 1024MB and you want a 4GB swap partition, your end point would be 5120MB (1024 + 4096).
  5. Exit parted Type quit to exit the parted utility. The new partition will have been assigned a number, such as sdc2 (or higher if other partitions already exist).
  6. Format the new partition for swap Format the newly created partition as swap space:
    mkswap /dev/sdc2
    (Remember to replace sdc2 with your actual partition name)
  7. Activate the swap partition Enable the swap partition with:
    swapon /dev/sdc2

Making the Change Permanent

If you want the system to automatically use this swap partition on every boot, you need to add it to the file system table:

  1. Edit the fstab file
    vi /etc/fstab
  2. Add the swap entry Press Insert to enter edit mode, then add this line to the bottom of the file:
    /dev/sdc2 swap swap defaults 0 0
  3. Save and exit Press Escape, then type :wq or press Shift+Z twice to save and quit.

Verifying the New Swap Space

To confirm that your new swap partition has been successfully added and is active, you can check the system’s swap status:

cat /proc/swaps

This will display all active swap spaces on your system, including your newly created partition.

Final Thoughts

Adding a dedicated swap partition is generally considered a more efficient long-term solution than using a swap file, especially for systems that regularly require swap space. The performance difference is modest on modern systems, but can be noticeable under heavy load.

Remember that while swap space is important for system stability, it’s not a replacement for adequate RAM. If your system is consistently using large amounts of swap space, consider upgrading your physical memory for better performance.