How to Create a Swap Partition using fdisk command
A swap partition in Linux acts like backup memory when RAM is full. It helps the system run smoothly, stay responsive, and handle heavy tasks better.
What is a Swap Partition?
A swap partition acts like extra memory on your hard drive when your RAM is full. When your RAM gets crowded, less-used data is moved to the swap partition, allowing the system to continue running smoothly. While it may seem like a quick fix for memory issues, it’s not as fast or efficient as adding more RAM.
Benefits of Swap Partition
A swap partition offers several benefits for Linux and other Unix-like systems:
- Extended Memory: It acts as extra memory when RAM is full, letting you run more programs without slowing down.
- Stability: It prevents system crashes by providing a backup when RAM is overloaded.
- Better Multitasking: Swap helps your system handle more apps without freezing or crashing.
- Hibernation: It stores data during hibernation, allowing you to resume work later.
- Prevents OOM: Helps avoid “Out of Memory” errors by swapping less important data.
- Crash Recovery: It can capture a memory dump after a crash, helping with troubleshooting.
- Wide Compatibility: Swap is supported across many operating systems, making it a dependable tool for memory management.
Disadvantages of Swap Partition
Swap partitions can be helpful, but they come with some downsides:
- Takes Up Disk Space: It occupies space, which can be an issue on systems with limited storage.
- Slower Performance: Writing and reading to disk is much slower than RAM, and too much swapping can slow down your system.
- Wear on SSDs: Frequent swapping can wear out SSDs faster due to their limited write cycles.
- Hides RAM Issues: Relying on swap can mask the need for more RAM or better software optimization, leading to slower performance.
- Harder to Manage: Managing a swap partition is more complex than a swap file, as it needs manual setup and adjustments.
- Fragmentation: Over time, swap partitions can get fragmented, slowing performance.
- Limited Flexibility: Changing swap size often requires reconfiguring your storage setup, which can be time-consuming.
- Security Risk: If not encrypted, sensitive data in swap space could be accessed by unauthorized users.
Check your Swap Partition
# swapon --show

Check /proc/swaps File
The /proc/swaps file shows details about the system’s swap space and how much of it is being used.
# cat /proc/swaps

Check free command
The free
command shows how much memory is being used on your system, including both physical and virtual memory.
# free -m

Create a Swap Partition using fdisk command
You can either create a new partition or use free space from an existing one for a swap file. To create a new partition, you can use tools like fdisk, gdisk, or parted. In this guide, we’ll use fdisk.
Check available disk space for swap partition
# fdisk -l

Take the disk you want to partition:
# fdisk /dev/sdb

Print use disk space
# Type p

Type n
and press Enter to create a new partition
# Command (m for help): n
Partition type
p primary (0 primary, 0 extended, 4 free)
e extended (container for logical partitions)
Select (default p): p
Partition number (1-4, default 1): 1
First sector (2048-4194303, default 2048):
Last sector, +/-sectors or +/-size{K,M,G,T,P} (2048-4194303, default 4194303):
Created a new partition 1 of type 'Linux' and of size 2 GiB.
Command (m for help): p
Disk /dev/sdb: 2 GiB, 2147483648 bytes, 4194304 sectors
Disk model: VMware Virtual S
Units: sectors of 1 * 512 = 512 bytes
Sector size (logical/physical): 512 bytes / 512 bytes
I/O size (minimum/optimal): 512 bytes / 512 bytes
Disklabel type: dos
Disk identifier: 0x77e88451
Device Boot Start End Sectors Size Id Type
/dev/sdb1 2048 4194303 4192256 2G 83 Linux

By default, the partition type is 83, while 82 is a partition type identifier for swap space.
Type t
and press Enter to change the partition type. Change the value to 82 and press Enter.
# Command (m for help): t
# Hex code or alias (type L to list all): 82
Changed type of partition 'Linux' to 'Linux swap / Solaris'.
Command (m for help): p
Disk /dev/sdb: 2 GiB, 2147483648 bytes, 4194304 sectors
Disk model: VMware Virtual S
Units: sectors of 1 * 512 = 512 bytes
Sector size (logical/physical): 512 bytes / 512 bytes
I/O size (minimum/optimal): 512 bytes / 512 bytes
Disklabel type: dos
Disk identifier: 0x77e88451
Device Boot Start End Sectors Size Id Type
/dev/sdb1 2048 4194303 4192256 2G 82 Linux swap / Solaris
Command (m for help):
To proceed with the changes, type w
, and press Enter.

Update the partition table
# partprobe /dev/sdb
Use the mkswap
command to set up the partition as swap space. It adds a swap signature, so the system can use it for virtual memory, while the rest of the space stores memory pages.
# mkswap -f /dev/sdb

After formatting, the swap partition is created and ready to use.
How to Enable a Swap Partition
Activate the Swap Partition
# swapon /dev/sdb
# swapon --show

The swap partition activates on boot and make it permanent.
# vim /etc/fstab
/dev/sdb none swap defaults 0 0

Check Existing Swap
# swapon --show
# free -h

Disable Swap
# swapoff -a
Enable Swap
# swapon -a