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
- Ensure the drive is unmounted
If the drive already contains a swap partition that’s in use, disable it first:
If the drive is clean and doesn’t have any swap partitions, you can skip this step.swapoff /dev/sdc
- Access the partition manager
Launch the
parted
utility to manage the disk partitioning:
This will load the parted prompt, which provides an interface for manipulating disk partitions.parted /dev/sdc
- View existing partitions
At the parted prompt, enter:
This will display details of any existing partitions and available free space on the disk.print
- 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:
Replacemkpartfs part-type linux-swap start end
start
andend
with appropriate values shown from theprint
command. For example, if the starting point is 1024MB and you want a 4GB swap partition, your end point would be 5120MB (1024 + 4096). - Exit parted
Type
quit
to exit the parted utility. The new partition will have been assigned a number, such assdc2
(or higher if other partitions already exist). - Format the new partition for swap
Format the newly created partition as swap space:
(Remember to replacemkswap /dev/sdc2
sdc2
with your actual partition name) - 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:
- Edit the fstab file
vi /etc/fstab
- 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
- Save and exit
Press
Escape
, then type:wq
or pressShift+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.