How to Create a Swap Partition in Linux

In this article, we are going to learn how to create a swap partition using the command fdisk, Before that let me explain to you what is the use of swap partition in Linux. SWAP Partition is also known as SWAP memory, It acts as a Virtual Memory in Linux. When the physical memory of the system is full then Swap memory helps to the system with a small amount of memory but we can’t consider it as a replacement of more memory.

How to Create a Swap Partition in Linux
How to Create a Swap Partition in Linux

Follow the steps to Create a Swap Partition in Linux:

Step: 1 Create a Partition

First, check the available free disks to create a new partition by the below command.

#  fdisk -l    # To list the available free disks and created partitions.
How to Create a Swap Partition in Linux
Partition List

Create a partition by the below command.

#  fdisk /dev/sdb    # To Create a new partition
How to Create a Swap Partition in Linux
Create a New Partition using fdisk

Here you can enter “n” to create a new Partition or enter “m” to go for help, We are going to create a new partition so enter “n“.

How to Create a Swap Partition in Linux
Select the Partition Type

Now we have two options i.e. Primary Partition by entering “p” or Extended Partition by entering “e“, So now we can select “p” and go for a Primary Partition.

How to Create a Swap Partition in Linux
Assign a Number to Partition

Here enter the available partition number for that disk and Press Enter.

How to Create a Swap Partition in Linux
Assign Cylinder to Partition

Now it’s asking for Cylinder, You can just press Enter to take the system default Cylinder.

How to Create a Swap Partition in Linux
Giving Size to Partition

As we can see System took its default Cylinder value as 1, now its time to give the Partition Size.

Here we can enter the partition size in two ways in GB’s or in MB’s, For Example, If you want to give the partition size as 1 GB then you can Enter +1G or +1024M.

So Let’s go with +1G and Press Enter.

How to Create a Swap Partition in Linux
We need to Change the Partition ID for SWAP Space

So we create the partition successfully but our goal is to create an additional swap partition, for that we need to change the partition ID for that we have to enter “t” or for help, we can go for “m“.

How to Create a Swap Partition in Linux
Partition Help Chart

As we can see in the help file “t” is for change the partition’s system ID, So let’s go for “t“.

How to Create a Swap Partition in Linux
Change the Partition ID

Now we have to enter the Partition code for the swap partition, for we can enter “L” to check the same.

How to Create a Swap Partition in Linux
Partition Code Chart

As we can see on the Partition code List “82” is the code of Linux swap.

How to Create a Swap Partition in Linux
Select Partition Code for Swap Partition

Now enter “w” to commit changes and save the partition table.

How to Create a Swap Partition in Linux
Save the Partition Table

In Linux based system after creating a partition, we suppose to restart the system to take effect but we can skip that by running “partprobe command”

#  partprobe /dev/sdb    # Changes take effect without restat the system

Also Read – How To Mount NTFS File System In Redhat/Centos

Step: 2 Create Swap Signature

Now Let’s create the swap Signature with “mkswap” command.

#  mkswap /dev/sdb1 -L newswap    # To make a swap partition
How to Create a Swap Partition in Linux
Create a Swap Partition with “mkswap”

L – To Label your Partition for Identification, This can be left as an optional but Highly recommended.

we can use “blkid” command to check the Labels given to File Systems.

How to Create a Swap Partition in Linux
Checking the Label of the Partition

Now enable the swapping and publish the swap space to the kernel by below command.

#  swapon /dev/sdb2    # Enable swapping on /dev/sdb2

OR we can use swapon -a to activate all swap partitions (Multiple swap Partitions) 

Step: 3 Mount the Swap Partition

Now It’s time to add the newly created swap partition on “/etc/fstab” file to start the partition at startup.

# vi /etc/fstab   # Edit the "fstab" file
How to Create a Swap Partition in Linux
Mounting SWAP Partition in fstab

We can check the swap status with the below command.

#  swapon -s

swapon -s” shows swap usage summary by device, Equivalent to “cat /proc/swaps

How to Create a Swap Partition in Linux
Checking SWAP Partition Usage

As we can see on the above snapshot we have two swap partitions, out of which highlighted one is recently created and currently it’s usage status is 0.

We have another command to check the status of the swap memory is “free -m

$  free -m    # Display amount of free and used memory in the system

Download Free Linux eBook HERE! Learn Linux in 5 Days - A Free Linux eBooks for Beginners

That’s all, In this article, we have explained How to Create a Swap Partition in Linux. I hope you enjoy this article. If you like this article, then just share it. If you have any questions about this article, please comment.

Share this:
WhatsApp Channel Join Now
Telegram Channel Join Now
Instagram Channel Join Now

Leave a Comment

This site uses Akismet to reduce spam. Learn how your comment data is processed.